Wednesday, October 21, 2009

Validating User Credentials

Validating User Credentials
---------------------------------

Let us see how to validate the user credentials(login and password)

In aspx.cs under Button_Click() method write the following code

string email= txtemail.Text;
string password = txtpassword.Text;

SqlConnection con = new SqlConnection(connectionstring);
string str = “select * from UserLoginInfo where email=’ ” + email + “ ‘ and password = ‘ “ + password + “ ‘ “;
SqlCommand cmd = new SqlCommand(str, con);

try
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
bool userexists = false;

while(dr.Read())
{
userexists = true;
}
if(userexists)
{
Server.Transfer(“Nextpage.aspx”);
}
else
{
lblresult.Text = “ Please enter the valid email and password”;
}
catch(exception e)
{
throw e;
}
finally
{
con.Close();
}
}

Here we are taking the user entered values(textbox values) into strings and checking them with the values in table UserLoginInfo. If both the values matches then the variable userexists becomes true and you will be derected to another(in this case Nextpage.aspx) page otherwise user will get the message “please enter the valid email and password.

In .aspx page create 2 textboxes for email and password and 1 submit button. In button_click method write the above code.

No comments: