Hi, I have a DB connection that works within a class. What I would like to do is be able to create a class with the connection in it (myClass) and reference that connection whenever I need it.
I have the following that works:
Code:
PublicSub sqlCon()
'Uses this connection to access TCIS database
Dim Conn As New SqlClient.SqlConnection
Dim dt As New DataTable()
Conn.ConnectionString = "Server=myServer;Database=myDB; Integrated Security=SSPI;"
Conn.Open()
Try
'Insert code to process data.
Dim sqlCmd As New SqlCommand("sp_mySproc", Conn)
Dim sqlDa As New SqlDataAdapter(sqlCmd)
Dim i As Integer = 0
sqlDa.Fill(dt)
For i = 0 To dt.Rows.Count - 1
blah, blah
Next i
Catch ex As Exception
MessageBox.Show(ex.Message, "Warning Message", MessageBoxButtons.OK)
Finally
Conn.Close()
End Try
End Sub
That works fine but I do not want to write this out everytime I need to access the DB. What I now need to do is be able to reference the above code residing in the myClass Class.
I created the myClass class and tried calling the connection from my Windows form like so:
Code:
Dim nmyClass As New myClass
Dim sqlCmd As New SqlCommand("sp_mySproc", nmyClass.SqlCon)
But I get a squiggly line under
nmyClass.SqlCon that says:
"Expression does not produce a value".
Can anyone tell me where I am going wrong? Thanks!