Click here to Skip to main content
11,495,869 members (57,586 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Windows MySQL VB.NET
Hi all

I wrote a select query to get the username and password from database

now i want to check whether the username and password is correct or not and if it is correct then will do some operation

please tell me how to write the code

Thank you
Posted 10-Jan-13 22:36pm
Code-Hunt7.1K
Comments
jibesh at 11-Jan-13 3:54am
   
what's the problem in comparing the data fetched from the database with user input?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can do something like the below
Select the Username and PWD into variables and then validate them

First check if the Username exists in the the database,
If YES, the get all items like ROLE, EMAIL ID ETC and the store them into a Session variable so that they can be used through out the website.

you can modify the below to suit you need.

Sub validateuser()
       Try
           sqlcon = New OleDbConnection(constr)
           sqlcon.Open()
           sqlcmd = New OleDbCommand("SELECT COUNT(*) FROM att_userrole WHERE username = '" + Login1.UserName + " AND password = '" + Login1.Password + "'", sqlcon)
           retval = sqlcmd.ExecuteScalar
           If retval = 1 Then
               sqlcmd = New OleDbCommand("SELECT role, emailid FROM att_userrole WHERE username = '" + Login1.UserName + "'", sqlcon)
               sqlrdr = sqlcmd.ExecuteReader()
               sqlrdr.Read()
               Session("username") = Login1.UserName
               getrole = (sqlrdr.Item("role"))
               Session("userrole") = getrole
               getemail = (sqlrdr.Item("emailid"))
               Session("reqemailid") = getemail
               Response.Redirect("~\Home.aspx")
               'Response.Write("you are valid " & Session("username") & " with role " & getrole)
           Else
               'IF USERNAME IS INVALID
               '-----YOUR CODE GOES HERE----
              lbl_msg.Text = ("Invalid login attmept")
 
           End If
       Catch ex As Exception
           lbl_msg.Text = ex.Message.ToString
       End Try
   End Sub

Hope this helps
  Permalink  
v3
Comments
Sandeep Mewara at 11-Jan-13 3:58am
   
Which part of your answer above deals with validation of password?

Sorry, not an answer. Above code checks if username exists or not. Thats it. Does not authenticate.
joe_j at 11-Jan-13 4:09am
   
thanks for pointing that out, I have added the password check in that now
Sandeep Mewara at 11-Jan-13 4:12am
   
Looks good now. :thumbsup:
Snehasish Nandy at 11-Jan-13 4:22am
   
but how i will get session in windows app...
joe_j at 11-Jan-13 4:29am
   
oops, if its a windows app then you do not need a session.
just store it into a global/public variable, then you can pass it on to any form.
OR
store these data into a txt file or an xml file and different forms can read it from there.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You should never retrieve the password from DB (i.e. doing a SELECT and getting it to frontend code behind)! Always pass on the entered username & password to server side which will pass them to DB in a query and get back the result if it matches. Something like:
SELECT
  Count(ID)
FROM
  MstUsers
WHERE
  Username = @username AND
  Password = @password
If the count is greater then zero, you know the combination is correct. You can retrieve any other information other then count if you need.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 692
1 CHill60 318
2 RyanDev 230
3 Sascha Lefèvre 205
4 OriginalGriff 186
0 Sergey Alexandrovich Kryukov 10,372
1 OriginalGriff 8,871
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100