Click here to Skip to main content
13,054,779 members (67,465 online)
Rate this:
Please Sign up or sign in to vote.
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 21:36pm
jibesh 11-Jan-13 3:54am
what's the problem in comparing the data fetched from the database with user input?
Rate this: bad
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()
           sqlcon = New OleDbConnection(constr)
           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()
               Session("username") = Login1.UserName
               getrole = (sqlrdr.Item("role"))
               Session("userrole") = getrole
               getemail = (sqlrdr.Item("emailid"))
               Session("reqemailid") = getemail
               'Response.Write("you are valid " & Session("username") & " with role " & getrole)
               '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
Sandeep Mewara 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 11-Jan-13 4:09am
thanks for pointing that out, I have added the password check in that now
Sandeep Mewara 11-Jan-13 4:12am
Looks good now. :thumbsup:
Snehasish Nandy 11-Jan-13 4:22am
but how i will get session in windows app...
joe_j 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.
store these data into a txt file or an xml file and different forms can read it from there.
Rate this: bad
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:
  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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2017
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