I am trying to use the DataReader To Access Table Records. I have done this successfully before but, now I am getting an error. I have tried it both using SQqlServer and Access Databases, using SqlClient and OleDb respectively. The DataReader Variable I declare = Nothing when I try Calling the ExecuteReader event on the Command object. Simple Sample(The code is in the Load event of a MDI child form that contains a ListView. Am trying to get the UserName form the User table in a Sql Database):
Private Sub FormTest_Load(ByVal....
Dim strConnect As String = "SERVER=COMPUTER\SQLSERV;" & _
"User ID=sa;Password='';" & _
"Initial Catalog=CodeGenerator"
Dim strSQL As String = "SELECT * FROM User"
Dim con As New SqlConnection(scConnect)
con.Open()
Dim cmd As New SqlCommand(strSQL, cnn)
cmd.CommandType = CommandType.Text
Dim dr As SqlDataReader
dr = cmd.ExecuteReader
-The Above Statement is where I get the error. Error pops up says ther is an unhandled exception of type system.data.sqlclient.sqlexception occurred in system.data.dll When I break the Debug and go over the dr in dr = cmd.ExecuteReader it says dr = nothing. However when I go over the dr in the Dim statement it shows it is of class sqldatareader. If I place () at the end of cmd.ExecuteReader it tells me it can not convert the type to a 1 Dimensional Array.
I have tried not placing the code event in the load statement. Tried putting the code in its own class.
Anyone have any Idea why this is happening. I have seen a thread on this in another websites forum but with no replies.
Can Anyone Help?
Private Sub FormTest_Load(ByVal....
Dim strConnect As String = "SERVER=COMPUTER\SQLSERV;" & _
"User ID=sa;Password='';" & _
"Initial Catalog=CodeGenerator"
Dim strSQL As String = "SELECT * FROM User"
Dim con As New SqlConnection(scConnect)
con.Open()
Dim cmd As New SqlCommand(strSQL, cnn)
cmd.CommandType = CommandType.Text
Dim dr As SqlDataReader
dr = cmd.ExecuteReader
-The Above Statement is where I get the error. Error pops up says ther is an unhandled exception of type system.data.sqlclient.sqlexception occurred in system.data.dll When I break the Debug and go over the dr in dr = cmd.ExecuteReader it says dr = nothing. However when I go over the dr in the Dim statement it shows it is of class sqldatareader. If I place () at the end of cmd.ExecuteReader it tells me it can not convert the type to a 1 Dimensional Array.
I have tried not placing the code event in the load statement. Tried putting the code in its own class.
Anyone have any Idea why this is happening. I have seen a thread on this in another websites forum but with no replies.
Can Anyone Help?