Object reference not set to an instance of an object.

  • Thread starter Luis Esteban Valencia
  • Start date
L

Luis Esteban Valencia

Can somebody pllease tell me what is the error on this code. I got the error
on the executereader line.

Public Function verificarcedula(ByVal cedula As String) As Boolean

Dim mycmd As SqlCommand = New SqlCommand("select * from maestro where
cod_empl=@cedula", SqlConnection1)

mycmd.Parameters.Add("@cedula", cedula)

Dim myreader As SqlDataReader

Try

SqlConnection1.Open()

myreader = mycmd.ExecuteReader

If (myreader.Read) Then

Return True

Else

Return False

End If

Catch ex As Exception

Throw ex

Finally

myreader.Close()

SqlConnection1.Close()

End Try

End Function
 
G

Guest

Try creating the dataReader object with the new keyword
Dim myreader As New SqlDataReader
+++++++++++++++++++++++++++++++
 
S

Scott Allen

Well, the reader object reference is only being assigned to in this
line of code, so it should not be a problem (and the ExecuteReader
method will take care of creating the object in any case). If anything
it is the SqlCommand object that we need to be suspicious of, but it
looks as if it was New'ed appropriately.

Have you stepped through the code with the debugger Luis?
 
P

Patrice

Did you checked they are not Nothing ? I would check also SqlConnection1.

Patrice

--
 

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

Top