What am I doing wrong?

M

Mike Mahon

I get the error Object reference not set to an instance of an object.
on the Response.Write (ds.Tables("tusers").Rows(0).Item(0)) I know
this is an old way to do it but I can't remember how to write a
message to the output in VS.NET I think I have the ds instantiating.
But I am just screwing up some where stupid. It is a Monday of
course.

Imports System.Data
Imports System.Data.SqlClient

Public Class login
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents txtpwd As System.Web.UI.WebControls.TextBox
Protected WithEvents txtlogin As System.Web.UI.WebControls.TextBox
Protected WithEvents btnlogin As
System.Web.UI.WebControls.LinkButton
Protected WithEvents lblSystemErrorMsg As
System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the
Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Public Shared m_validUser As Boolean
Public Shared Property validuser() As Boolean
Get
Return m_validUser ' Return the value stored in the local
variable.
' Optionally, you can use the syntax One = PropVal to
return
' the property value.
End Get
Set(ByVal Value As Boolean)
m_validUser = Value ' Store the value in a local variable.
End Set
End Property


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnlogin.Click

validateUser(txtlogin.Text, txtpwd.Text)
Response.Write(validuser)
' If sc.validuser Then
' Response.Redirect("searchresumes.aspx")
' Else
' lblSystemErrorMsg.Text = "Either your user name or
password are incorrect."
' End If
End Sub

Private Function validateUser(ByVal id As String, ByVal pwd As
String)

Dim cn As New
SqlConnection(ConfigurationSettings.AppSettings("cn"))
Dim da As SqlDataAdapter = New SqlDataAdapter
Dim cmd As SqlCommand
Dim ds As DataSet
Dim sout As String

cmd = New SqlCommand("sp_chklogin", cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@usr", SqlDbType.VarChar, 20)
cmd.Parameters("@usr").Value = id
cmd.Parameters.Add("@pwd", SqlDbType.VarChar, 20)
cmd.Parameters("@pwd").Value = pwd

'Try
da.SelectCommand = cmd
ds = New DataSet
cn.Open()
da.Fill(ds, "tusers")
'
' If ds.Tables("users").Rows(0).Item(0) = 1 Then
' sout = ds.Tables("users").Rows(0).Item(0)
' Else
Response.Write(ds.Tables("users").Rows(0).Item(0))
' End If

'Catch ex As Exception

'Finally
cn.Close()
'nd Try

End Function

End Class
 
J

Jose Marcenaro

Other than the typo in
da.Fill(ds, "tusers")
..
Response.Write(ds.Tables("users").Rows(0).Item(0))

("tusers" in one case, "users" in the other) I see nothing wrong in your
code..

Regards
Jose.
 
J

Juan Romero

Damn, I was writing you like 200 lines of code explaining how to debug when
I realized that THE NAME OF THE TABLE IS TUSERS, NOT USERS, lol.

You are right, its monday....
 
K

Kevin Spencer

In the statement:

Response.Write (ds.Tables("tusers").Rows(0).Item(0))

There are precisely 5 object references which may or may not be set to an
instance of an object:

1. Response (I will assume that this is indeed present)
2. ds (DataSet) - Is it initialized?
3. Tables("tusers") - Does this DataTable exist in the DataSet? How are you
sure?
4. Rows(0) - Are you sure that Tables("tusers") has rows? If there are no
rows, this would cause an error.
5. Item(0) - If there is a row, there is at least one item in it, so we will
assume this is not a problem.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

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