Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Object Reference not set to an instance of an object

pg81pg81 Posts: 10Member
Hi!

I'm trying to do a login form where userid and password will match the respective textboxes and so that I can go to another form. Here is the code that I used for VB.NET. I know the VB6 code but don't know VB.NET. Here is the code:

Private Sub LoginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnlogin.Click

'Create a Command Object
Dim objCommand As OleDbCommand
Dim objCommand2 As OleDbCommand

'Call user and password
OleDbDataAdapter1.SelectCommand.Parameters("UserId").Value = UserText.Text & "%"
OleDbDataAdapter1.SelectCommand.Parameters("password").Value = PasswordText.Text & "%"

// This part is highlighted and "Object Reference not set to an instance of an object" pops up as error
If objCommand.Parameters("@UserID").Value = UserText.Text And
objCommand2.Parameters("@password").Value = PasswordText.Text Then
//

Dim mainfrm As New Form()
mainfrm.Show()
Else
MsgBox("Your Password or Username is incorrect. Please try Again")
UserText.Focus()
End If

End Sub

The Connection and CommandText I'm using the objects to do.

Can anyone please help me with this? Thanks a lot! I really need the help.

If there is any better way to write a login page, please email me and teach me if you can. I'll really appreciate the help. Thanks

Victor

Comments

  • Jon SummersJon Summers Posts: 2Member
    Victor ...

    The error message appears to be correct.

    : Private Sub LoginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnlogin.Click
    :
    : 'Create a Command Object
    : Dim objCommand As OleDbCommand
    : Dim objCommand2 As OleDbCommand
    :
    : 'Call user and password

    Is OleDbDataAdapter1 a control on your form?

    : OleDbDataAdapter1.SelectCommand.Parameters("UserId").Value = UserText.Text & "%"
    : OleDbDataAdapter1.SelectCommand.Parameters("password").Value = PasswordText.Text & "%"
    :
    : // This part is highlighted and "Object Reference not set to an instance of an object" pops up as error

    Where have you assigned objCommand and objCommand2? Not in this procedure! Try:
    Dim objCommand As New OleDbCommand

    : If objCommand.Parameters("@UserID").Value = UserText.Text And
    : objCommand2.Parameters("@password").Value = PasswordText.Text Then



  • michaelBmichaelB Posts: 30Member

    Using New on the two objCommand objects certainly could be done,
    but I don't see how these two newed objects are connected in any way to the actual DataAdapter! Or, do I see the DataAdapter fired in any way with a call to Fill, parameterized with some appropriate DataSet object such as OleDbDataAdapter1.Fill(someDataSet). Then you query the returned DataSet for the necessary variable values. Unless I am going blind here there appears to be several steps missing.
  • iwilld0itiwilld0it Posts: 1,134Member
    [b][red]This message was edited by iwilld0it at 2003-12-3 20:41:9[/red][/b][hr]
    : Hi!
    :
    : I'm trying to do a login form where userid and password will match the respective textboxes and so that I can go to another form. Here is the code that I used for VB.NET. I know the VB6 code but don't know VB.NET. Here is the code:
    :
    : Private Sub LoginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnlogin.Click
    :
    : 'Create a Command Object
    : Dim objCommand As OleDbCommand
    : Dim objCommand2 As OleDbCommand
    :
    : 'Call user and password
    : OleDbDataAdapter1.SelectCommand.Parameters("UserId").Value = UserText.Text & "%"
    : OleDbDataAdapter1.SelectCommand.Parameters("password").Value = PasswordText.Text & "%"
    :
    : // This part is highlighted and "Object Reference not set to an instance of an object" pops up as error
    : If objCommand.Parameters("@UserID").Value = UserText.Text And
    : objCommand2.Parameters("@password").Value = PasswordText.Text Then
    : //
    :
    : Dim mainfrm As New Form()
    : mainfrm.Show()
    : Else
    : MsgBox("Your Password or Username is incorrect. Please try Again")
    : UserText.Focus()
    : End If
    :
    : End Sub
    :
    : The Connection and CommandText I'm using the objects to do.
    :
    : Can anyone please help me with this? Thanks a lot! I really need the help.
    :
    : If there is any better way to write a login page, please email me and teach me if you can. I'll really appreciate the help. Thanks
    :
    : Victor
    :

    Personally, I would forget about using the data adapter and use the command objects ExecuteScalar function.

    [code]
    ' SQL Command May Look Like This
    ' ? = OleDb Parameter Placeholders
    Dim sql As String
    sql= "SELECT [id] FROM LoginTable WHERE UserName = ? AND Password = ?"

    ' Create Connection And Command
    Dim con As New OleDbConnection(connectString)
    Dim cmd As New OleDbCommand(sql, con)

    ' Add Parameters To Command
    With cmd.Parameters
    .Add(New OleDbParameter("@UserName", UserText.Text.Trim))
    .Add(New OleDbParameter("@Password", PasswordText.Text.Trim))
    End If

    ' Open Connection
    con.Open()

    ' ExecuteScalar - Returns First Column Of First Row Or Nothing
    If Not (cmd.ExecuteScalar() Is Nothing) Then
    ' Valid Login
    Else
    ' Invalid Login
    End If

    con.Close()
    [/code]

    I reserve DataAdapters for when I'm working strictly with DataSets and only when Filling data from a live datasource.
    I'm mainly a web programmer so I rarely see the need for Datasets.



  • pg81pg81 Posts: 10Member
    :
    : Using New on the two objCommand objects certainly could be done,
    : but I don't see how these two newed objects are connected in any way to the actual DataAdapter! Or, do I see the DataAdapter fired in any way with a call to Fill, parameterized with some appropriate DataSet object such as OleDbDataAdapter1.Fill(someDataSet). Then you query the returned DataSet for the necessary variable values. Unless I am going blind here there appears to be several steps missing.
    :

    Easy there Mr MichaelB. I'm just learning... no experience ok? :) Thanks for helping me out.

  • pg81pg81 Posts: 10Member
    : [b][red]This message was edited by iwilld0it at 2003-12-3 20:41:9[/red][/b][hr]
    : : Hi!
    : :
    : : I'm trying to do a login form where userid and password will match the respective textboxes and so that I can go to another form. Here is the code that I used for VB.NET. I know the VB6 code but don't know VB.NET. Here is the code:
    : :
    : : Private Sub LoginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnlogin.Click
    : :
    : : 'Create a Command Object
    : : Dim objCommand As OleDbCommand
    : : Dim objCommand2 As OleDbCommand
    : :
    : : 'Call user and password
    : : OleDbDataAdapter1.SelectCommand.Parameters("UserId").Value = UserText.Text & "%"
    : : OleDbDataAdapter1.SelectCommand.Parameters("password").Value = PasswordText.Text & "%"
    : :
    : : // This part is highlighted and "Object Reference not set to an instance of an object" pops up as error
    : : If objCommand.Parameters("@UserID").Value = UserText.Text And
    : : objCommand2.Parameters("@password").Value = PasswordText.Text Then
    : : //
    : :
    : : Dim mainfrm As New Form()
    : : mainfrm.Show()
    : : Else
    : : MsgBox("Your Password or Username is incorrect. Please try Again")
    : : UserText.Focus()
    : : End If
    : :
    : : End Sub
    : :
    : : The Connection and CommandText I'm using the objects to do.
    : :
    : : Can anyone please help me with this? Thanks a lot! I really need the help.
    : :
    : : If there is any better way to write a login page, please email me and teach me if you can. I'll really appreciate the help. Thanks
    : :
    : : Victor
    : :
    :
    : Personally, I would forget about using the data adapter and use the command objects ExecuteScalar function.
    :
    : [code]
    : ' SQL Command May Look Like This
    : ' ? = OleDb Parameter Placeholders
    : Dim sql As String
    : sql= "SELECT [id] FROM LoginTable WHERE UserName = ? AND Password = ?"
    :
    : ' Create Connection And Command
    : Dim con As New OleDbConnection(connectString)
    : Dim cmd As New OleDbCommand(sql, con)
    :
    : ' Add Parameters To Command
    : With cmd.Parameters
    : .Add(New OleDbParameter("@UserName", UserText.Text.Trim))
    : .Add(New OleDbParameter("@Password", PasswordText.Text.Trim))
    : End If
    :
    : ' Open Connection
    : con.Open()
    :
    : ' ExecuteScalar - Returns First Column Of First Row Or Nothing
    : If Not (cmd.ExecuteScalar() Is Nothing) Then
    : ' Valid Login
    : Else
    : ' Invalid Login
    : End If
    :
    : con.Close()
    : [/code]
    :
    : I reserve DataAdapters for when I'm working strictly with DataSets and only when Filling data from a live datasource.
    : I'm mainly a web programmer so I rarely see the need for Datasets.
    :
    :
    :
    :
    Thanks man... really appreciate the help :)

  • pg81pg81 Posts: 10Member
    : Victor ...
    :
    : The error message appears to be correct.
    :
    : : Private Sub LoginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnlogin.Click
    : :
    : : 'Create a Command Object
    : : Dim objCommand As OleDbCommand
    : : Dim objCommand2 As OleDbCommand
    : :
    : : 'Call user and password
    :
    : Is OleDbDataAdapter1 a control on your form?
    :
    : : OleDbDataAdapter1.SelectCommand.Parameters("UserId").Value = UserText.Text & "%"
    : : OleDbDataAdapter1.SelectCommand.Parameters("password").Value = PasswordText.Text & "%"
    : :
    : : // This part is highlighted and "Object Reference not set to an instance of an object" pops up as error
    :
    : Where have you assigned objCommand and objCommand2? Not in this procedure! Try:
    : Dim objCommand As New OleDbCommand
    :
    : : If objCommand.Parameters("@UserID").Value = UserText.Text And
    : : objCommand2.Parameters("@password").Value = PasswordText.Text Then
    :
    :
    :
    :
    Thank you... will work on it :)

  • michaelBmichaelB Posts: 30Member
    Sorry, I wasn't trying to sound harsh. I was actually scratching my head to honestly figure out if those missing steps might be being done at some other part of your code that I wasn't seeing.
Sign In or Register to comment.