Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help With Sql/OleDbDataReader??

Status
Not open for further replies.

tfayer

Technical User
Aug 4, 2002
41
US
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?
 
Hi,

Dim con As New SqlConnection(scConnect)
con.Open()
Dim cmd As New SqlCommand(strSQL, cnn)

From the code above, actually your connection object is con and while creating SQLCommand you are passing in a different connection object (cnn). Is this a typo?

-Kris
 
The error says that it is a unhandled exception because you are not catching it. If you dont you get that annoying green bar that tells you nothing.
To get Database error messages you have to trap them

Try

'Put code here

Catch sqlEX as System.Data.Oledb.Oledbexception (or System.Data.SqlClient.Sqlexception)

Messagebox.show(sqlEX.toString)

End Try

This will give you an error you can read.

Add this to your code and tell me what it says

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb

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

If you can't explain it simply, you don't understand it well enough.
- A. Einstein





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top