Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
Jibesh 11-Jan-13 3:54am    
what's the problem in comparing the data fetched from the database with user input?

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.

VB
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
 
Share this answer
 
v3
Comments
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:
[no name] 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.
OR
store these data into a txt file or an xml file and different forms can read it from there.
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:
SQL
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900