Visual Basic - SQL password validation using BindingSource

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sampak
    New Member
    • Apr 2013
    • 3

    Visual Basic - SQL password validation using BindingSource

    I need help. Need to do password validation and have no idea why it doesn't work. Could someone tell me what my code is missing or what is actually wrong with my way of thinking? I am connecting it to dataset2 where is the table called Users2. Table consists of two columns: 'Username' and 'Password'. Users2BiningSou rce.Find should be returning value of an index where a current query is. The returned value is always -1, what I guess means that is not found (false). I am using Microsoft Visual Studio 2012 along with SQL server 2008 SP3, all on windows 7. Thank you for your time and help!



    Code:
    Imports Hotel_Booking_System.DataSet2TableAdapters
    
    
    
    Public Class Logging
    
    
    Public Sub check_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Check_Details()
    End Sub
    
    
    Private Sub Quit_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
    
    Public Sub Logging_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Users2TableAdapter.Fill(Me.DataSet2.Users2)
        'TODO: This line of code loads data into the 'DataSet2.Users2' table. You can move, or remove it, as needed.
    
    End Sub
    
    Public Sub Check_Details()
    
        Me.Users2BindingSource.AddNew()
    
        Dim Usrnm As String = ""
        Usrnm = txtUsername.Text
        Dim Pswrd As String = ""
        Pswrd = txtPassword.Text
        Dim Found As Integer
        Found = Users2BindingSource.Find("Username", Usrnm)
        Dim Found2 As Integer
        Found2 = Users2BindingSource.Find("Password", Pswrd)
    
    
        If Found >= 0 And Found2 >= 0 And Usrnm = "Admin" Then
            Me.Hide()
            Booking.Show()
        ElseIf Found >= 0 And Found2 >= 0 Then
            Me.Hide()
            Rooms.Show()
        Else
            MsgBox("Wrong username or password. Please try again", MsgBoxStyle.DefaultButton1, "SOMETHING WENT WRONG")
        End If
        Label1.Text = Found 'checking the value of found
        Label2.Text = Found2 ' checking the value of found2
    End Sub
    
    End Class
    Last edited by Rabbit; Apr 7 '13, 11:00 PM. Reason: Please use code tags when posting code.
  • vijay6
    New Member
    • Mar 2010
    • 158

    #2
    Hey sampak, alter your code as follows and try again.

    Code:
    Dim username As String = txtUsername.Text
    Dim password As String = txtPassword.Text
    
    Dim found1 As Integer = bs.Find("Username", username)
    Dim found2 As Integer = bs.Find("Password", password)
    
    'MsgBox(found1 + " " + found2);
    
    If found1 <> -1 AndAlso found2 <> -1 Then
    	If found1 = found2 Then
    		'MsgBox("Username and Password matching")
    
    		If username.Equals("Admin") Then
    			Me.Hide()
    			Booking.Show()
    		Else
    			Me.Hide()
    			Rooms.Show()
    		End If
    	ElseIf found2 > found1 Then
    		MsgBox("Invalid Username or Password")
    	Else
    		bs.MoveFirst()
    
    		For i As Integer = 0 To found1 - 1
    			bs.RemoveCurrent()
    		Next
    
    		If bs.Find("Password", password) <> 0 Then
    			MsgBox("Invalid Username or Password")
    		Else
    			'MsgBox("Username and Password matching")
    
    			If username.Equals("Admin") Then
    				Me.Hide()
    				Booking.Show()
    			Else
    				Me.Hide()
    				Rooms.Show()
    			End If
    		End If
    	End If
    Else
    	MsgBox("Invalid Username or Password")
    End If

    All i want to say is one thing, your method for Username and Password verification is too complex or i maybe wrong. Why you're using 'BindingSource' for Username and Password verification?

    Comment

    • sampak
      New Member
      • Apr 2013
      • 3

      #3
      The reason I am using BindingSource is I have tried so many different ways, and nothing works... :/ So I am trying with binding source... I want to create simple logging in structure connected with sql database... it is so shit... I ve tried sth like that as well:


      Code:
          Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      
              Dim login = Me.Users2TableAdapter.UsernamePassword(txtUsername.Text, txtPassword.Text)
              If login Is Nothing Then
                  MsgBox("wrong details", , "INCORRECT LOGIN OR PASSWORD")
              Else
                  MsgBox("welcome")
      
      
              End If
      I have built a simple query where it counter returns number of rows that matches with the password and username have been entered. But it doesnt work as well... all my connections seem to be fine. Why is so hard to work with database? And all my project is database based on. Your solution is not working either.
      bs - is it a name of BindingSource yes? it returns me an error saying Properties of DataMemeber 'Username' cannot be found in DataSource element. so frustrating.
      Last edited by Rabbit; Apr 8 '13, 04:50 PM. Reason: Please use code tags when posting code.

      Comment

      • sampak
        New Member
        • Apr 2013
        • 3

        #4
        So this:
        Code:
        Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        
        Dim login = Me.Users2TableAdapter.UsernamePassword(txtUsername .Text, txtPassword.Text)
        If login Is Nothing Then
        MsgBox("wrong details", , "INCORRECT LOGIN OR PASSWORD")
        Else
        MsgBox("welcome")
        
        
        End If
        work with Access database, but i want to use sql... what is the difference? I am using sql query anyway, so can someone explain me why i doesn't work?
        Last edited by Rabbit; Apr 8 '13, 04:50 PM. Reason: Please use code tags when posting code. Second warning.

        Comment

        • vijay6
          New Member
          • Mar 2010
          • 158

          #5
          Hey sampak, this is the simple Username and Password verification method. If you want you can try this method,

          Code:
          Private Sub button1_Click(sender As Object, e As EventArgs)
          	Dim con As New SqlConnection("Your Database Connection String")
          	Dim cmd As New SqlCommand("SELECT COUNT(*) FROM Table_Name WHERE Username = '" + Me.txtUsername.Text.Trim() + "' AND Password = '" + Me.txtPassword.Text.Trim() + "'", con)
          	Try
          		con.Open()
          		If Convert.ToInt32(cmd.ExecuteScalar()) > 0 Then
          			If Me.txtUsername.Text.Trim().Equals("Admin") Then
          				' Do the respective work for Admin user
          			Else
          				' Do the respective work for Non-Admin user
          			End If
          		Else
          			MessageBox.Show("Invalid Username or Password")
          		End If
          	Catch ex As Exception
          		Console.WriteLine(ex.ToString())
          	Finally
          		con.Close()
          	End Try
          End Sub

          Note: In line number 3 'Username' and 'Password' are refers to the fields in 'Table_Name' table.

          In my earlier post 'bs' is 'BindingSource' and 'Username' is one of the field of my table which i fetched into 'bs'.

          Comment

          Working...