Click here to Skip to main content
11,705,753 members (45,963 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Visual-Studio , +
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 5-May-13 6:35am
Comments
ThePhantomUpvoter at 5-May-13 14:24pm
   
Okay so connect to your database, write a query and execute it.
Chiranthaka Sampath at 5-May-13 20:45pm
   
Ok then what method of that login control to use?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:

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
  Permalink  
v5
Comments
Chiranthaka Sampath at 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!
Rohan Leuva at 6-May-13 3:29am
   
Refer to updated answer. You can accept that answer if it helps you.
Chiranthaka Sampath at 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!
Rohan Leuva at 6-May-13 4:43am
   
Refer to this link Asp.net Login Controls
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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;
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.


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();
}
  Permalink  
v5

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

  Print Answers RSS
0 OriginalGriff 213
1 Sergey Alexandrovich Kryukov 170
2 Inimicos 60
3 ProgramFOX 50
4 F-ES Sitecore 45
0 OriginalGriff 9,037
1 Sergey Alexandrovich Kryukov 8,417
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,626


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 8 Aug 2015
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