SQLDataReader Error

D

Damian

Hi everyone !

I am having a problem getting the datareader to work. It seems that it
doesnt get instantiated when i Dim it. The locals window when i run
the program has myReader ( the sqldatareader) set to Nothing and it
doesnt change. I tried adding the New keyword but it doesnt Build the
app , it comes up with an error saying it is not accessible in this
context because it is "Private".

Here is the code:

Dim err As Exception
Dim sConnString As String
Dim sql As String
Dim nwindConn As New SqlClient.SqlConnection
Dim authorsCMD As New SqlClient.SqlCommand
Dim myReader As SqlClient.SqlDataReader

sConnString = "Data
Source=sydsql;uid=DBDeveloper;pwd=password;Initial Catalog=northwind;"
sql = "Select au_fname,au_lname from Authors"

Try
nwindConn.ConnectionString = sConnString
nwindConn.Open()
authorsCMD.Connection = nwindConn
authorsCMD.CommandText = sql
myReader = authorsCMD.ExecuteReader() <--- crashes on
this line
If myReader.HasRows Then
Do While myReader.Read()
cboAuthors.Items.Add(myReader.GetString(0))
Loop
End If
Catch
MsgBox("Error occurred: " & err.ToString)
End Try

I am able to connect to the database.The code crashes on the following
line:
myReader = authorsCMD.ExecuteReader(). Says MyReader is set to
nothing.

Any ideas ?

Any help would be appreciated !
 
M

Miha Markic [MVP C#]

Hi Damian,

Are you sure about the row?
Seems odd that on assignment it would say that the myReader is nothing.
 
J

Jim Hughes

Unless you've changed something, Authors is in the Pubs database, not
Northwind!

Change your connection string, instead of Initial Catalog=northwind, use
Initial Catalog=Pubs

Miha Markic said:
Hi Damian,

Are you sure about the row?
Seems odd that on assignment it would say that the myReader is nothing.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Damian said:
Hi everyone !

I am having a problem getting the datareader to work. It seems that it
doesnt get instantiated when i Dim it. The locals window when i run
the program has myReader ( the sqldatareader) set to Nothing and it
doesnt change. I tried adding the New keyword but it doesnt Build the
app , it comes up with an error saying it is not accessible in this
context because it is "Private".

Here is the code:

Dim err As Exception
Dim sConnString As String
Dim sql As String
Dim nwindConn As New SqlClient.SqlConnection
Dim authorsCMD As New SqlClient.SqlCommand
Dim myReader As SqlClient.SqlDataReader

sConnString = "Data
Source=sydsql;uid=DBDeveloper;pwd=password;Initial Catalog=northwind;"
sql = "Select au_fname,au_lname from Authors"

Try
nwindConn.ConnectionString = sConnString
nwindConn.Open()
authorsCMD.Connection = nwindConn
authorsCMD.CommandText = sql
myReader = authorsCMD.ExecuteReader() <--- crashes on
this line
If myReader.HasRows Then
Do While myReader.Read()
cboAuthors.Items.Add(myReader.GetString(0))
Loop
End If
Catch
MsgBox("Error occurred: " & err.ToString)
End Try

I am able to connect to the database.The code crashes on the following
line:
myReader = authorsCMD.ExecuteReader(). Says MyReader is set to
nothing.

Any ideas ?

Any help would be appreciated !
 
J

Jim Hughes

Also, consider using Exception and SQLException instead of the Err object

Catch ex As SqlException

MsgBox("Error occurred: " & ex.Message)

Catch ex As Exception

MsgBox("Error occurred: " & ex.Message)

The first message box now shows "Invalid object name 'Authors'" when using
Northwind


Miha Markic said:
Hi Damian,

Are you sure about the row?
Seems odd that on assignment it would say that the myReader is nothing.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Damian said:
Hi everyone !

I am having a problem getting the datareader to work. It seems that it
doesnt get instantiated when i Dim it. The locals window when i run
the program has myReader ( the sqldatareader) set to Nothing and it
doesnt change. I tried adding the New keyword but it doesnt Build the
app , it comes up with an error saying it is not accessible in this
context because it is "Private".

Here is the code:

Dim err As Exception
Dim sConnString As String
Dim sql As String
Dim nwindConn As New SqlClient.SqlConnection
Dim authorsCMD As New SqlClient.SqlCommand
Dim myReader As SqlClient.SqlDataReader

sConnString = "Data
Source=sydsql;uid=DBDeveloper;pwd=password;Initial Catalog=northwind;"
sql = "Select au_fname,au_lname from Authors"

Try
nwindConn.ConnectionString = sConnString
nwindConn.Open()
authorsCMD.Connection = nwindConn
authorsCMD.CommandText = sql
myReader = authorsCMD.ExecuteReader() <--- crashes on
this line
If myReader.HasRows Then
Do While myReader.Read()
cboAuthors.Items.Add(myReader.GetString(0))
Loop
End If
Catch
MsgBox("Error occurred: " & err.ToString)
End Try

I am able to connect to the database.The code crashes on the following
line:
myReader = authorsCMD.ExecuteReader(). Says MyReader is set to
nothing.

Any ideas ?

Any help would be appreciated !
 
D

Damian

Jim Hughes said:
Also, consider using Exception and SQLException instead of the Err object

Catch ex As SqlException

MsgBox("Error occurred: " & ex.Message)

Catch ex As Exception

MsgBox("Error occurred: " & ex.Message)

The first message box now shows "Invalid object name 'Authors'" when using
Northwind


Miha Markic said:
Hi Damian,

Are you sure about the row?
Seems odd that on assignment it would say that the myReader is nothing.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Damian said:
Hi everyone !

I am having a problem getting the datareader to work. It seems that it
doesnt get instantiated when i Dim it. The locals window when i run
the program has myReader ( the sqldatareader) set to Nothing and it
doesnt change. I tried adding the New keyword but it doesnt Build the
app , it comes up with an error saying it is not accessible in this
context because it is "Private".

Here is the code:

Dim err As Exception
Dim sConnString As String
Dim sql As String
Dim nwindConn As New SqlClient.SqlConnection
Dim authorsCMD As New SqlClient.SqlCommand
Dim myReader As SqlClient.SqlDataReader

sConnString = "Data
Source=sydsql;uid=DBDeveloper;pwd=password;Initial Catalog=northwind;"
sql = "Select au_fname,au_lname from Authors"

Try
nwindConn.ConnectionString = sConnString
nwindConn.Open()
authorsCMD.Connection = nwindConn
authorsCMD.CommandText = sql
myReader = authorsCMD.ExecuteReader() <--- crashes on
this line
If myReader.HasRows Then
Do While myReader.Read()
cboAuthors.Items.Add(myReader.GetString(0))
Loop
End If
Catch
MsgBox("Error occurred: " & err.ToString)
End Try

I am able to connect to the database.The code crashes on the following
line:
myReader = authorsCMD.ExecuteReader(). Says MyReader is set to
nothing.

Any ideas ?

Any help would be appreciated !

Yeah thanks i used the wrong database...duh! Thanks for tge tip on error handling !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

sql Connection Error 1

Top