Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have created a web form with the login control. Now I want to validate the UserName & the Password with the data on a table in a SQL database (username & password as the fields of the table). I am new to ASP.Net & I need your help thanks.

I am using VS 2010 .net Framework 4.0 in win 7 professional 64 bit! SQl server 2008 R2 Developer
Posted
Comments
[no name] 5-May-13 14:24pm    
Okay so connect to your database, write a query and execute it.
Chiranthaka Sampath 5-May-13 20:45pm    
Ok then what method of that login control to use?

You can have a look at How to implement forms authentication and Managing users with roles. There are functionalities available in Asp.NET for user authentication.

Possible method using database:

C#
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
conn.Open();
string uname = TextBox1.Text;
string ps = Textbox2.Text;
SqlCommand cmd = new SqlCommand("Select uname, pswd from emp_details where uname =@uname and pswd =@ps", conn);
cmd.Parameters.Add(new SqlParameter("@uname", "id here"));
cmd.Parameters.Add(new SqlParameter("@ps", "password here"));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
       /*Perform operations for authenticated users*/
}

-Additional note:

Its advisable to store password in encrypted format in database(Not as plain text). You can make use of algorithm. For example, have a look at Simple String Encryption and Decryption
 
Share this answer
 
v5
Comments
Chiranthaka Sampath 6-May-13 3:21am    
Ok I have visited that and then I want to validate that login control's username and the password from a table that in a SQL database. Then how am I going to do that? Please help me!
Thanks7872 6-May-13 3:29am    
Refer to updated answer. You can accept that answer if it helps you.
Chiranthaka Sampath 6-May-13 4:01am    
In here you have develop the code using some sort of new textboxes and buttons but in my case I want to use the Login control of the tools of the VS 2010 and I need the help to add the database to this Login control. Remember that this as ASP.Net form ASP.Net uses the Login control!
Thanks7872 6-May-13 4:43am    
Refer to this link Asp.net Login Controls
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SampleConnectionString"].ToString());
con.Open();
SqlCommand com = new SqlCommand("select password from Registration where username='" + Txtusername.Text + "'", con);
SqlDataReader dr = com.ExecuteReader();
if (!dr.Read())
{
labLogin.Text = ("Invalid User");
}
else
{
if (dr[0].ToString() == Txtpassword.Text)
{
Response.Redirect("Select.aspx");
}
else
{
labLogin.Text = ("Wrong Password");
Txtpassword.Focus();
}
}
}
catch (Exception ex)
{
labLogin.Text = "ex" + ex.Message;
}
 
Share this answer
 
For Your Question comment
>Ok I have visited that and then I want to validate that login control's username and >the password from a table that in a SQL database. Then how am I going to do that? >Please help me!
I guess is this ur requirement.


C#
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
conn.Open();
string username = TextBox1.Text;
string password = Textbox2.Text;
SqlCommand cmd = new SqlCommand("Select uname, pswd from emp_details where uname =@uname and pswd =@ps", conn);
cmd.Parameters.Add(new SqlParameter("@uname", "username"));
cmd.Parameters.Add(new SqlParameter("@ps", "password"));
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
       dr.close();
       //Your Homepage or Ur need;
//Eg:// Response.Redirect("Home.aspx");
// As a beginner the above one is enough in future u have to show the user name in the further pages like  Hi Chiranthaka Sampath in the next pages u have to move with Session.
Session["username"] = txtusername.Text;
 Session["password"] = txtpassword.Text;
//My means of Session wherever u show the name u can get from the Session..

}
else
{
 dr.close();
msg.Showalert("Invalid username or password");
}
con.close();
}
 
Share this answer
 
v5

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