How can I do this?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajay Bhalala
    New Member
    • Nov 2014
    • 119

    How can I do this?

    I have created the login form in vb.net

    In this form there are 2 labels : Username & Password
    and 2 Text-boxes for enter the username and password and 1 button "Login"

    I have created the ms access database named "Project.md b" and in this database, I have created 1 table named "UserAccoun t" and it's fields are "ID", "UserName" & "Password"

    I have write the code as following...
    Code:
    Imports System.Data.OleDb
    
    Public Class Login
    
        Public con As New OleDbConnection("Provider=microsoft.jet.oledb.4.0 ; Data Source=E:\Brinda & Jay\Brinda\02 S.Y\VB.NET\My Project\Project.mdb")
        Public qry As String
        Public dr As OleDbDataReader
    
    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            con.Open()
            qry = "select * from UserAccount where UserName like ' " & TextBox1.Text & " ' and Password like ' " & TextBox2.Text & " ' "
            Dim cmd As New OleDbCommand(qry, con)
            cmd.CommandText = qry
            cmd.Connection = con
            dr = cmd.ExecuteReader
            If dr.Read = True Then
                MsgBox("Accessed")
            Else
                MsgBox("Not Accessed")
            End If
            con.Close()
        End Sub
    End Class
    there is no error in this code, but when I run this form and though I enter the right username and password but "Not Accessed" msgbox is displays. I can't understand What changes I have to do in this code?

    Which changes I have to do in this code? Please help.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Output the query that it's actually running. You put in extra spaces before and after the username and password.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Use the OleDbDataReader .HasRows Property instead of OleDbDataReader .Read Method.

      Please also read the documentation about using OleDbCommand.Pa rameters because it will avoid any SQL insertion attacks.

      -Frinny

      Comment

      • Ajay Bhalala
        New Member
        • Nov 2014
        • 119

        #4
        Thank you so much for the help to both Rabbit and Frinavale. I will try it. Ok

        Comment

        Working...