Create a simple user login form in ASP.Net

In this article we will create a simple user login form. First of all we will create a table in database and design a from.  There are many techniques through you can retrieve  data from different tables, here we will create a simple table for our form to get data from it.

Step-1: Open Server Explorer go to database and add new table, also insert the  following fields, ID, User_Name and Password. As shown below  in the demo.

login-tables

Step-2: Now select table and right click on the mouse, show table data, input values in the fields (User_Id, User_Name and password) as shown below. You can aslo insert data by creating a signup form and retrieve signin information from that  table. Here we are creating simple login form so we will insert values directly into the tables and get information from it for login form.

login-table

Step-3: In the last two steps we have created a table in the database and insert values in the fields. Now in this step we will create a form and make sure all the fields in the form should be same as defined in the table. Create a form now as shown in the demo below.

login-form

Step-4:  Our three steps are almost completed now we will work on coding phase, this phase is very important.  Now double on the login button as shown above demo figure, and add this code. Create Sql Connection and give your database source( highlight below), just change this path and give your database path.

{
SqlConnection conn = new SqlConnection(@”Data Source=.\SQLEXPRESS;AttachDbFilename=C:\USERS\AMIR\DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\WEBSITE1\APP_DATA\KIU.MDF;Integrated Security=True;User Instance=True”);
SqlCommand cmd;
try
{
cmd = new SqlCommand(“Select * from asp_staff Where email_address ='” + txtstfadrs.Text + “‘And password='” + txtpswrd.Text + “‘”, conn);
conn.Open();
cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
reader.Close();
conn.Close();
Session[“email_address”] = txtstfadrs;
Session.Timeout = 240;
Response.Redirect(“accounts/welcome-on-your-profile.aspx”);
}
else
{
lblstff.Text = “Incorrect password or email address”;
}
}
catch
{
conn.Close();
}

}

After completed all these steps debug your form and login to your form by using defined id, user name and password as you have inserted in the table. I am hoping you will successfully run your login form. Test it now, Best of Luck 😛

Leave a Reply

Your email address will not be published.