By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,599 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

Visual Basic - SQL password validation using BindingSource

P: 3
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'. Users2BiningSource.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!



Expand|Select|Wrap|Line Numbers
  1. Imports Hotel_Booking_System.DataSet2TableAdapters
  2.  
  3.  
  4.  
  5. Public Class Logging
  6.  
  7.  
  8. Public Sub check_Click(sender As Object, e As EventArgs) Handles Button1.Click
  9.     Me.Check_Details()
  10. End Sub
  11.  
  12.  
  13. Private Sub Quit_Click(sender As Object, e As EventArgs) Handles Button2.Click
  14.     Me.Close()
  15. End Sub
  16.  
  17. Public Sub Logging_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  18.     Me.Users2TableAdapter.Fill(Me.DataSet2.Users2)
  19.     'TODO: This line of code loads data into the 'DataSet2.Users2' table. You can move, or remove it, as needed.
  20.  
  21. End Sub
  22.  
  23. Public Sub Check_Details()
  24.  
  25.     Me.Users2BindingSource.AddNew()
  26.  
  27.     Dim Usrnm As String = ""
  28.     Usrnm = txtUsername.Text
  29.     Dim Pswrd As String = ""
  30.     Pswrd = txtPassword.Text
  31.     Dim Found As Integer
  32.     Found = Users2BindingSource.Find("Username", Usrnm)
  33.     Dim Found2 As Integer
  34.     Found2 = Users2BindingSource.Find("Password", Pswrd)
  35.  
  36.  
  37.     If Found >= 0 And Found2 >= 0 And Usrnm = "Admin" Then
  38.         Me.Hide()
  39.         Booking.Show()
  40.     ElseIf Found >= 0 And Found2 >= 0 Then
  41.         Me.Hide()
  42.         Rooms.Show()
  43.     Else
  44.         MsgBox("Wrong username or password. Please try again", MsgBoxStyle.DefaultButton1, "SOMETHING WENT WRONG")
  45.     End If
  46.     Label1.Text = Found 'checking the value of found
  47.     Label2.Text = Found2 ' checking the value of found2
  48. End Sub
  49.  
  50. End Class
Apr 7 '13 #1
Share this Question
Share on Google+
4 Replies


100+
P: 158
Hey sampak, alter your code as follows and try again.

Expand|Select|Wrap|Line Numbers
  1. Dim username As String = txtUsername.Text
  2. Dim password As String = txtPassword.Text
  3.  
  4. Dim found1 As Integer = bs.Find("Username", username)
  5. Dim found2 As Integer = bs.Find("Password", password)
  6.  
  7. 'MsgBox(found1 + " " + found2);
  8.  
  9. If found1 <> -1 AndAlso found2 <> -1 Then
  10.     If found1 = found2 Then
  11.         'MsgBox("Username and Password matching")
  12.  
  13.         If username.Equals("Admin") Then
  14.             Me.Hide()
  15.             Booking.Show()
  16.         Else
  17.             Me.Hide()
  18.             Rooms.Show()
  19.         End If
  20.     ElseIf found2 > found1 Then
  21.         MsgBox("Invalid Username or Password")
  22.     Else
  23.         bs.MoveFirst()
  24.  
  25.         For i As Integer = 0 To found1 - 1
  26.             bs.RemoveCurrent()
  27.         Next
  28.  
  29.         If bs.Find("Password", password) <> 0 Then
  30.             MsgBox("Invalid Username or Password")
  31.         Else
  32.             'MsgBox("Username and Password matching")
  33.  
  34.             If username.Equals("Admin") Then
  35.                 Me.Hide()
  36.                 Booking.Show()
  37.             Else
  38.                 Me.Hide()
  39.                 Rooms.Show()
  40.             End If
  41.         End If
  42.     End If
  43. Else
  44.     MsgBox("Invalid Username or Password")
  45. 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?
Apr 8 '13 #2

P: 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:


Expand|Select|Wrap|Line Numbers
  1.     Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.  
  3.         Dim login = Me.Users2TableAdapter.UsernamePassword(txtUsername.Text, txtPassword.Text)
  4.         If login Is Nothing Then
  5.             MsgBox("wrong details", , "INCORRECT LOGIN OR PASSWORD")
  6.         Else
  7.             MsgBox("welcome")
  8.  
  9.  
  10.         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.
Apr 8 '13 #3

P: 3
So this:
Expand|Select|Wrap|Line Numbers
  1. Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.  
  3. Dim login = Me.Users2TableAdapter.UsernamePassword(txtUsername .Text, txtPassword.Text)
  4. If login Is Nothing Then
  5. MsgBox("wrong details", , "INCORRECT LOGIN OR PASSWORD")
  6. Else
  7. MsgBox("welcome")
  8.  
  9.  
  10. 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?
Apr 8 '13 #4

100+
P: 158
Hey sampak, this is the simple Username and Password verification method. If you want you can try this method,

Expand|Select|Wrap|Line Numbers
  1. Private Sub button1_Click(sender As Object, e As EventArgs)
  2.     Dim con As New SqlConnection("Your Database Connection String")
  3.     Dim cmd As New SqlCommand("SELECT COUNT(*) FROM Table_Name WHERE Username = '" + Me.txtUsername.Text.Trim() + "' AND Password = '" + Me.txtPassword.Text.Trim() + "'", con)
  4.     Try
  5.         con.Open()
  6.         If Convert.ToInt32(cmd.ExecuteScalar()) > 0 Then
  7.             If Me.txtUsername.Text.Trim().Equals("Admin") Then
  8.                 ' Do the respective work for Admin user
  9.             Else
  10.                 ' Do the respective work for Non-Admin user
  11.             End If
  12.         Else
  13.             MessageBox.Show("Invalid Username or Password")
  14.         End If
  15.     Catch ex As Exception
  16.         Console.WriteLine(ex.ToString())
  17.     Finally
  18.         con.Close()
  19.     End Try
  20. 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'.
Apr 9 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.