accessing db without calling a function?

S

Sean

Hi There,

I am making the trasition from asp to asp .net, I have a page below which
has 3 user controls on it. The 3rd usercontrol (<UserControl3:terms
runat="server" />) calls a stored procedure to return one row to display on
a page, everything works fine except no matter what I do the function is
called first with the Sub Page_Load and the results end up in the top
lefthand corner.

What I want to know is how I can access the db without having the code in a
sub routine? I have tried to remove the sub calls but then I get an error
saying "myConnection " is not defined. Could someone help me out with some
code?

Sean - thanks in advance

!--- usercontrol page

<%@ Control Language="vb" EnableViewState="False" %>

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>

<script language="vb" runat="server">

Dim drRead As SqlDataReader
Dim strTermsConditions as String

Dim myConnection as New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))


myConnection.Open()

Dim myCommand As New SqlCommand("GetListingStates", myConnection)
myCommand.CommandType = CommandType.StoredProcedure

drRead = myCommand.ExecuteReader()

While drRead.Read ( )

strTermsConditions = drRead(0)

End While



</script>


!-- aspx page


<%@ Register TagPrefix="UserControl1" TagName="Menu"
Src="includes/_Menu.ascx" %>
<%@ Register TagPrefix="UserControl2" TagName="Header"
Src="includes/_Header.ascx" %>
<%@ Register TagPrefix="UserControl3" TagName="terms"
Src="includes/_terms_content.ascx" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="distribution" content="global">
<meta name="revisit-after" content="14 days">
<meta name="ROBOTS" content="ALL">

<title>title</title>
</head>

<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">
<table width="640" border="0" cellpadding="0" cellspacing="0">
<td colspan="2" valign=top><UserControl2:Header runat="server" /></td>
<tr>
<td width="100" valign=top><UserControl1:Menu runat="server" /></td>
<td width="563" valign=top><UserControl3:terms runat="server" /></td>
</tr>

<TR>
<td colspan=2>&nbsp;footer</td>
<td>
</tr>
</table>
</body>
</html>
 
S

Scott M.

You should really try to fully use the .NET paradigm and move away from
Classic ASP structure.

User controls (.ascx) files have a code-behind page (.ascx.vb) where the
compiled code goes. No script tags or script delimiters <% %> are used (as
this is VB .NET, not VBScript).

As for your specific problem...I wouldn't be using a User Control at all for
this, since your code does not produce any UI. I would make a class that
has a method that connects to the DB and returns the data. This class can
be instantiated from whatever event you want and you can place the return
value wherever you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top