3 Replies - 1261 Views - Last Post: 08 August 2012 - 05:44 AM Rate Topic: -----

#1 Grant22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-August 12

'Login' SQL Query not working (Access Database)

Posted 07 August 2012 - 02:05 PM

I wrote a SQL statement named 'Login' for a Login Page to my program and it worked with an MDF database but isn't working when I switched to an Access database. I am using visual studio 2010.

SELECT        Username, [Password]
FROM            Users
WHERE        (Username = '@Username') AND ([Password] = '@[Password]')



When I call the query in code, it won't accept the arguments from the username and password text boxes to check them against my Username and Password fields from the 'Users' database. Can anyone point me in the right direction as to what I might be doing wrong? Thanks!

Public Class LoginForm1

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Try
            ' This line is giving the error:
            Me.UsersTableAdapter.Login(UsernameTextBox.Text, PasswordTextBox.Text)
            Me.Close()
        Catch ex As Exception
            MsgBox("Invalid Username or Password", MsgBoxStyle.Exclamation, "Validation Failed")
            PasswordTextBox.Text = String.Empty
            PasswordTextBox.Focus()
        End Try
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
        AboutBox1.Hide()
        MainMenu.Close()
    End Sub

    Private Sub LoginForm1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.UsersTableAdapter.Fill(EmployeesDataSet.Users)
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: 'Login' SQL Query not working (Access Database)

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 6490
  • View blog
  • Posts: 23,579
  • Joined: 12-June 08

Re: 'Login' SQL Query not working (Access Database)

Posted 07 August 2012 - 02:08 PM

You don't seem to have a coherent structure of accessing the data.

1. connection string.
2. sql statement.
3. open connection
4. adapter or sql command
5. execute the adapter by filling a dataset or 'executing a non query'...
6. close connection.



OleDb Basics in VB.Net Rate Topic

A Really Simple Database Create a Database using Access & VB.net Express 2008
Was This Post Helpful? 0
  • +
  • -

#3 bambi1  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 11
  • Joined: 09-March 12

Re: 'Login' SQL Query not working (Access Database)

Posted 07 August 2012 - 05:46 PM

This is the one I use, hope it helps.
Imports System.Data.OleDb
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:\1\1\PWord.mdb")
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT usernamefield,passwordfield FROM Table1 where usernamefield=? and passwordfield=?", con)
        cmd.Parameters.AddWithValue("usernamefield", TextBox1.Text)
        cmd.Parameters.AddWithValue("passwordfield", TextBox2.Text)
        Try
            con.Open()
            Dim read As OleDbDataReader = cmd.ExecuteReader()
            If read.HasRows Then
                read.Read()
                If TextBox1.Text = read.Item("usernamefield").ToString And TextBox2.Text = read.Item("passwordfield").ToString Then
                    MsgBox("Login successful")
                    Form2.Show()
                    Me.Hide()
                Else
                    MsgBox("Login unsuccessful, username and passwords are case sensitive")
                End If
            Else
                MsgBox("Login unsuccessful")

            End If
            Me.TextBox1.Text = My.Resources.TextFile1
            read.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            con.Close()
        End Try
    End Sub
    
End Class


Was This Post Helpful? 0
  • +
  • -

#4 Grant22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-August 12

Re: 'Login' SQL Query not working (Access Database)

Posted 08 August 2012 - 05:44 AM

I figured out what it was finally. I was using the wrong type of filtering in my sql statements. Since it was an access db, it needed to be Username=? instead of Username = @Username. Once i worked that bit out, everything fell into place. Thanks for the solution Bambi, that will be helpful on the next project I have to make.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1