473,473 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Visual Basic - SQL password validation using BindingSource

3 New Member
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
4 2694
vijay6
158 New Member
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
sampak
3 New Member
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
sampak
3 New Member
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
vijay6
158 New Member
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

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

Similar topics

4
by: Corepaul | last post by:
I am a beginner, using Access 2000. I am having difficulty with the Visual Basic Help. Specifically, using the Index tab, when I enter "OpenRecordset" in the keyword field and click the Search...
5
by: Microsoft | last post by:
Hi, I have Visual Basic .net 2003 (Standard Edition) & SQL Server 2000 Developer Edition. When trying to create a connection in the server explorer from the .net IDE I get a number of problems;...
27
by: code_wrong | last post by:
Visual Basic (not dot net) what is the best way to check the User has entered an integer into an InputBox? isNumeric() checks for a numeric value .. but does not notify of numbers with decimal...
1
by: marknoten | last post by:
I have an excel file with columns A, B, C, D and E that look like this: A B C D E ----------------------------------------------------- N 23,179 15,744...
11
DUNXALEARE
by: DUNXALEARE | last post by:
Hello everyone! I have a new assignment. I just dont know how to create a data report using ms excell. I need to search particular group of data/records using Visual basic 6. in MS Access and...
5
by: AjGrace | last post by:
Hi! Im a Beginner in SQL server and I need to know how to connect to it using Visual Basic 6.0 using Ado connection..Thanks In Advance guys!
0
by: shangardezi | last post by:
hi im a beginner with visual basic. I was wondering can someone tell me how i can somehow do a thing that when someone presses ok on the LOGIN FORM, the username and password is sent to my email. im...
6
by: ashokbio | last post by:
I want to create an associative array, similar to PERL language, using Visual Basic 6. Example: Using PERL %A = ("A"=>"Apple","B"=>"Banana"); i.e., A is associated to the word Apple and B...
3
gnawoncents
by: gnawoncents | last post by:
Greetings, I have a database which uses VBA to dynamically create a report and add VB coding to the on close and on open events. All this works fine, however, when the report closes, the Visual...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.