tfayer Posted October 24, 2003 Posted October 24, 2003 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? Quote
Moderators Robby Posted October 24, 2003 Moderators Posted October 24, 2003 You declared con as SqlConnection but later used cnn in the SqlCommand Quote Visit...Bassic Software
tfayer Posted October 25, 2003 Author Posted October 25, 2003 Sorry, that is a typo. Even with the correction it is still throwing an error Quote
*Experts* jfackler Posted October 26, 2003 *Experts* Posted October 26, 2003 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 Quote
*Gurus* Derek Stone Posted October 26, 2003 *Gurus* Posted October 26, 2003 SqlExceptions are only thrown when there's a problem accessing SQL Server or one of its objects. There's either something wrong with your connection string, an object name, or the database server can't be found. Quote Posting Guidelines
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.