Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • *Experts*
Posted

Try this:

Make sure you Import System.data

Dim dr As SqlClient.SqlDataReader

Might seem like a ridiculous question, but are you certain your initial catalog: CodeGenerator and your table: User, exist and have at least one row of data?

 

If you weren't getting an exception when you tried to open the connection before the correction Robby pointed out, was cnn previously declared?

Do you have option strict and option explicit turned on? If not, turn them on.

 

The .ExecuteReader is a method and so should have () at it's end.

 

 

Jon

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...