Thanks for the reply. In my case the connection string will be the same in
all cases so I would declare the connection object and set the connection
string property too, all outside any subs.
1) do I need to be concerned about the resource usage doing this? for
example, I currently declare the connection, open it, use it, close it, and
then set it equal to Nothing to release it's resources. If I declare outside
all subs then I can't set it to Nothing right? Or am a missing a fundemental
of a page's lifecycle here? Does every post back create a new page with all
new instances of these variables?
2) I just realized another detail: I already have the connection open in the
calling sub. The called sub also needs to open a connection to the database
though. Can the called sub just use the existing open connection from the
calling sub? Would it be better to just have the called sub create it's own
new connection that it opens and closes when it's done (resulting in 2 open
connections to the same database at one time)?
thanks again for the input. Any more is welcome.
"Chad Devine" <su*****@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Yes, you assume correctly that you can just declare outside the subs
and the variables will work within all subs.
An example:
Dim Conn As SqlConnection
Dim cmdSelect As SqlCommand
Dim SelectStr As String
Sub Page_Load(Sender As Object, E As EventArgs)
Conn = New
SqlConnection("server=SQLServer;trusted_connection =true;database=MyDatabase"
) If Not IsPostBack Then
BindDataGrid
End IF
End Sub
Sub BindDataGrid
cmdSelect = New SqlCommand( "Select * from Servers ORDER BY ServerName
Asc;", Conn)
Conn.Open()
dgrdServers.DataSource = cmdSelect.ExecuteReader()
dgrdServers.DataBind()
Conn.Close()
End Sub