Please look at the following code. I know I am missing something simple, but I can't seem to figure it out.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds As New DataSet
Dim conn As New SqlConnection("Data Source=(local);Initial Catalog=Permits;user id=sa; password=[password]"
Dim cmd As New SqlCommand("pUser_LoadBy_UserName", conn)
Dim adpt As New SqlDataAdapter(cmd)
Try
Label1.Text = ""
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@UserName", SqlDbType.NText).Value = TextBox1.Text
cmd.
adpt.Fill(ds, "Results"
Catch ex As SqlException
Label1.Text = ex.Message
End Try
Dim dt As DataTable = ds.Tables("Results"
If Not (dt.Rows.Count = 0) Then
If (TextBox2.Text = (CStr(dt.Rows(0)("PassCode"
))) Then 'Verify Passcodes match
Label1.Text = "Password invalid."
Else
Label1.Text = "Login Successful " & TextBox1.Text
End If
End If
End Sub
When I run this and enter a username and password, I get the "Object reference not set to an instance of an object" error. I debugged and found that my data table = nothing. I think perhaps it's because my data set = nothing. I'm not sure. I've never worked with SQL Server before and I'm trying to use a stored procedure in the database through my code.
I would appreciate any advice...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds As New DataSet
Dim conn As New SqlConnection("Data Source=(local);Initial Catalog=Permits;user id=sa; password=[password]"
Dim cmd As New SqlCommand("pUser_LoadBy_UserName", conn)
Dim adpt As New SqlDataAdapter(cmd)
Try
Label1.Text = ""
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@UserName", SqlDbType.NText).Value = TextBox1.Text
cmd.
adpt.Fill(ds, "Results"
Catch ex As SqlException
Label1.Text = ex.Message
End Try
Dim dt As DataTable = ds.Tables("Results"
If Not (dt.Rows.Count = 0) Then
If (TextBox2.Text = (CStr(dt.Rows(0)("PassCode"
Label1.Text = "Password invalid."
Else
Label1.Text = "Login Successful " & TextBox1.Text
End If
End If
End Sub
When I run this and enter a username and password, I get the "Object reference not set to an instance of an object" error. I debugged and found that my data table = nothing. I think perhaps it's because my data set = nothing. I'm not sure. I've never worked with SQL Server before and I'm trying to use a stored procedure in the database through my code.
I would appreciate any advice...