Connection Problem

  • Thread starter Thread starter Wayne Wengert
  • Start date Start date
W

Wayne Wengert

I suspect I am missing the obvious here but I don't see it. I am trying to
establish a connection to an SQL Server from an ASP.NET page

I have defined the subroutines shown below, I have myConn defined at the top
of the page as:

Friend myConn As SqlClient.SqlConnection

and then when I try to execute the following code I get an error that
"Object reference not set to an instance of an object"
-------------------------------------------------------
CreateConnection()

myConn.Open()

---------------------------------------------------------------------

I must be missing something basic here?



Wayne



================ SUB & FUNC ==================

' Create Connection

Private Sub CreateConnection()

Dim myConn As New SqlConnection(GetSQLConnectionString())

End Sub

' Returns database connection string

Private Function GetSQLConnectionString() As String

Return "Data Source = sql.myhost.com; Initial Catalog = wengert; User ID =
myid; Password=xxxx"

End Function
 
Hi Wayne,

what happening here is you are creating two instances of SqlConnections, one
with the friend modifier which is visible thoroout the assembly, and another
local one with the CreateConnection subroutine which goes out of scope after
your call returns from the sub, hence the global one didn't get instanciated
at all. Change the line in CreateConnection as

myConn = New SqlConnection(GetSQLConnectionString())

and it will work as usual.

Regards
Joyjit

myConn = New SqlConnection(GetSQLConnectionString())
 
Thanks Joyjit;

I got all hung up with something else and missed the obvious

Wayne
 
Back
Top