Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dt = nothing (object ref not set)

Status
Not open for further replies.

kristinac

Programmer
Jul 18, 2003
98
US
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...
 
It was apparently

cmd.Parameters.Add("@UserName", SqlDbType.NText).Value = TextBox1.Text

I should have had SqlDbType.varchar instead of ntext.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top