Click here to Skip to main content
15,902,917 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
'Im using vb.net with access database ehhehe

VB
Try
    strSQL = "SELECT * FROM User WHERE (Username LIKE ?) AND (Password LIKE ?)"

    Dim dbCmd As New OleDbCommand(strSQL, conn)

    dbCmd.Parameters.AddWithValue("@Username", txtUsername.Text)
    dbCmd.Parameters.AddWithValue("@Password", txtPassword.Text)
    dbReader = dbCmd.ExecuteReader()


    Using dbReader
        If dbReader.Read() Then
            Dim strResult1 As String = dbReader.Item("Username")
            Dim strResult2 As String = dbReader.Item("Password")

            If chkRememberUsername.Checked = True Then
                My.Settings.Username = txtUsername.Text
                My.Settings.Save()
                My.Settings.Reload()

            End If

            Me.Close()
            mediaSoundPlayer.Stream = My.Resources.tada
            mediaSoundPlayer.Play()

            frmLoading.ShowDialog()

            'frmMain.LogOffToolStripMenuItem.Text = "Log Off"
            'frmMain.SettingToolStripMenuItem.Enabled = True
            frmMain.LogOffToolStripMenuItem.Text = "Log Off"
            Me.Close()
            frmMain.Show()
        Else

            intResult = MessageBox.Show("Invalid Username Or Password", "Login", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error)


            If intResult = DialogResult.Cancel Then
                Me.Close()

            Else
                intResult = DialogResult.Retry
                Me.Show()

            End If
            frmMain.SettingsToolStripMenuItem.Enabled = False
            frmMain.LogOffToolStripMenuItem.Text = "Login"


        End If


    End Using
    conn.Close()

Catch ex As Exception
    MsgBox("System Error", MsgBoxStyle.Critical, "Error in login")
End Try
Posted
Updated 17-Feb-10 11:46am
v3

If you're using question marks, it should be formatted more like:
VB
cmd1.CommandText = "SELECT ID FROM Employee "
                 + "WHERE id BETWEEN ? AND ?";
OleDbParameter p1 = new OleDbParameter();
OleDbParameter p2 = new OleDbParameter();
cmd1.Parameters.Add(p1);
cmd1.Parameters.Add(p2);
p1.Value = "01";
p2.Value = "03";


If you're using AddWithValue, it should be like:
VB
Dim SqlString As String = "Select * From Contacts Where FirstName = ? And LastName = ?"

Using conn As New OleDbConnection(ConnString)

  Using cmd As New OleDbCommand(SqlString, conn)

    cmd.CommandType = CommandType.Text

    cmd.Parameters.AddWithValue("FirstName", txtFirstName.Text)

    cmd.Parameters.AddWithValue("LastName", txtLastName.Text)


If you want to use the @ symbol, then it's set up differently...
Dim SqlString As String = "Select * From Contacts Where FirstName = @FirstName And LastName = @LastName"

Using conn As New OleDbConnection(ConnString)

  Using cmd As New OleDbCommand(SqlString, conn)

    cmd.CommandType = CommandType.Text

    cmd.Parameters.AddWithValue("FirstName", txtFirstName.Text)

    cmd.Parameters.AddWithValue("LastName", txtLastName.Text)


Some of this is from: Parameter Queries in ASP.NET with MS Access[^]
and some is from: Pass Parameter to OleDbCommand[^]

Hopefully the others didn't confuse you, but you're mixing the styles around...as were they.
 
Share this answer
 
Oh...and I also should say...you're misusing the Try/Catch block badly. It's not meant to be used like the old VB6 On Error. If you want to do it that way, .NET still lets you.

A Try/Catch block should be targeted to a single line of code. The majority of code that we write will never throw an exception if we write it properly. By encapsulating everything in a single Try/Catch, you're not getting the information you really need. I'm going to put asterisks in front of all of the lines that don't need to be in a Try/Catch block

VB
Try
* strSQL = "SELECT * FROM User WHERE (Username LIKE ?) AND (Password LIKE ?)"
  Dim dbCmd As New OleDbCommand(strSQL, conn)
* dbCmd.Parameters.AddWithValue("@Username", txtUsername.Text)
* dbCmd.Parameters.AddWithValue("@Password", txtPassword.Text)
  dbReader = dbCmd.ExecuteReader()

  Using dbReader
*   If dbReader.Read() Then
*     Dim strResult1 As String = dbReader.Item("Username")
*     Dim strResult2 As String = dbReader.Item("Password")
*     If chkRememberUsername.Checked = True Then
*       My.Settings.Username = txtUsername.Text
*       My.Settings.Save()
*       My.Settings.Reload()
*     End If
*     Me.Close()
*     mediaSoundPlayer.Stream = My.Resources.tada
*     mediaSoundPlayer.Play()
*     frmLoading.ShowDialog()
*     'frmMain.LogOffToolStripMenuItem.Text = "Log Off"
*     'frmMain.SettingToolStripMenuItem.Enabled = True
*     frmMain.LogOffToolStripMenuItem.Text = "Log Off"
*     Me.Close()
*     frmMain.Show()
*   Else
*     intResult = MessageBox.Show("Invalid Username Or Password", "Login", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error)

*     If intResult = DialogResult.Cancel Then
*       Me.Close()
*     Else
*       intResult = DialogResult.Retry
*       Me.Show()
*     End If
*     frmMain.SettingsToolStripMenuItem.Enabled = False
*     frmMain.LogOffToolStripMenuItem.Text = "Login"
*   End If

* End Using
* conn.Close()
Catch ex As Exception
  MsgBox("System Error", MsgBoxStyle.Critical, "Error in login")
End Try
 
Share this answer
 
What's desperately wrong is that you didn't tell us what the problem is. Did you want us to guess ?
 
Share this answer
 
Im still confuse...
here the link I've upload my project together with the 3rd party software so you can run it..http://www.megaupload.com/?d=3K3MFJI0[^]

Can you help me in coding guys???
 
Share this answer
 
I assume you are getting the error when the AddWithValue is executed? Try adding the '@' to the "Username" and "Password" in your select statement...
 
Share this answer
 
you mean here in this line of code?

strSQL = "SELECT * FROM User WHERE (Username LIKE ?) AND (Password LIKE ?)"

So ill make this as

strSQL = "SELECT * FROM User WHERE (@Username LIKE ?) AND (@Password LIKE ?)"

if I remove the try catch the error is in the dbReader = dbCmd.ExecuteReader()

But before that It was working well until I add a column in the database it was originaly only 2 columns in the database the username and password column only until I add new column to that table I begins to give me error messages... This is a big problem because this is my thesis hope someone here could help me with coding my project...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900