I want a solution Object reference not set to an instance of an object

Advertisements
Dim connstring As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim conn As New SqlConnection(connstring)
conn.Open()
Dim cmd As New SqlCommand("select * from Table_1 where date_register = #" & TextBox1.Text & "#", conn)
Dim myreader As SqlDataReader
'If Not (myreader.IsDBNull(myreader.GetOrdinal("date_register"))) Then
Dim datereg As DateTime = myreader("date_register")
TextBox1.Text = datereg.ToString("MM/dd/yyyy")
'Else
'    TextBox1.Text = ""
'End If
conn.Close()

enter image description here

Hello, is there a solution for the error, thank you

>Solution :

You did not execute your SQL command and before loading data into your data reader variable, you are trying to use it which is causing that error. Simply do this:

Dim myreader As SqlDataReader = cmd.ExecuteReader

Leave a ReplyCancel reply