Welcome

Welcome to CMS n Web. Learn & Study completely Free Articles\Training about Web Designing\Development and CMS.

Search This Web

Wednesday, July 3, 2013

Simple Registration or Signup & Login or Signin Form using Asp.Net, C#.Net & Sqlserver

Introduction
This article explains about creating a simple registration or signup and login or sign in form using asp.net, c#.net and sqlserver.
Follow the steps as described below to create the registration form and login form as shown above.
Creating Database:
  1. Create a databse in sqlserver with name 'TestDB'
  2. Next create a table with fields as shown in picture.

Creating Registration or signup form:
  1. Now create a new website using visual studio 2008 0r 2010.
  2. Design the registraion form and login form as shown in Below pictures. In these two forms, validation controls are used to validate the fields as per requirement. To know about validation controls in asp.net read the following article.


    1. Add connection string property in web.config file as shown below.
      <configuration>
        <connectionStrings>
          <add name="CM_Connection" connectionString="data source=localhost;Initial Catalog=TestDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
        </connectionStrings>
        <system.web>
      Note:In this web.config file, the connection string name is 'CM_Connection'. You can change data source to your local server name (here it is localhost).

      1. Next in the 'CreateUser' button click event write the following code.
        if (Page.IsValid)
                {
                    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CM_Connection"].ConnectionString))
                    {
                        SqlCommand cmd = new SqlCommand();
                        Guid guid;
                        guid = Guid.NewGuid();
                        string sql = "INSERT INTO TestDB_RegisterUser (registeruser_id,registeruser_username,registeruser_email,registeruser_password";
                        sql += " ,registeruser_createdon,registeruser_modifiedon,registeruser_rowstate)";
                        sql += "VALUES (@ID,@Username,@Email,@Password,@Createdon,@Modifiedon,@Rowstate)";
                        cmd.Parameters.AddWithValue("@ID", guid);
                        cmd.Parameters.AddWithValue("@Username", UserName.Text.Trim());               
                        cmd.Parameters.AddWithValue("@Email", Email.Text.Trim());
                        cmd.Parameters.AddWithValue("@Password", Password.Text);
                        cmd.Parameters.AddWithValue("@Createdon", DateTime.Now);
                        cmd.Parameters.AddWithValue("@Modifiedon", DateTime.Now);
                        cmd.Parameters.AddWithValue("@Rowstate", 1);                
                        cmd.Connection = con;
                        cmd.CommandText = sql;
                        con.Open();
                        try
                        {
                            cmd.ExecuteNonQuery();
                            ErrorMessage.Text = "Registered successfully.";                    
                        }
                        catch(Exception ex)
                        {
                            throw new Exception(ex.Message);
                        }
                    }
                }
        Note: You can check the validity of a Page by checking the Page.IsValid property. For this property to return true, all validation server controls in the current validation group must validate successfully. You should check this property only after you have called the Page.Validate method, or set the CausesValidation property to true in the OnServerClick event handler for an ASP.NET server control that initiates form processing. In simple words the Page.IsValid property tells you whether the validation succeeded or not.
      In the above method after Page.IsValid, insert the record into database as shown. This finishes the registration form.
      Creating Signin or Login form: In the 'Log In' button click event write the code as shown below.
       stringemail = UserName.Text.Trim();
      string pwd = Password.Text;
      DataTable dt = new DataTable();
      using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CM_Connection"].ConnectionString))
      {
          SqlCommand cmd = new SqlCommand();
          cmd.Connection = con;
          // cmd.CommandType = CommandType.StoredProcedure;
          cmd.CommandText = "SELECT registeruser_id,registeruser_username, registeruser_email,registeruser_password FROM TestDB_RegisterUser where registeruser_email='" + email + "' and registeruser_password='" + pwd + "' and registeruser_rowstate<3 ";
          con.Open();
          SqlDataAdapter da = new SqlDataAdapter();
          da.SelectCommand = cmd;
          da.Fill(dt);
      
          if (dt.Rows.Count > 0)
          {
              Session.Add("UserID", dt.Rows[0][0].ToString());
              Session.Add("Username", dt.Rows[0][1].ToString());
              Session.Add("UserEmail", dt.Rows[0][2].ToString());
              Response.Redirect("~/Default.aspx", false);
          }
          else
          {
              FailureText.Text = "Invalid username or password";            
          }
      }
      The above method gets the record that matches with useremail and password. Otherwise the datatable will be empty which is used to show 'Invalid username or password' to the user.
      Thus you can create a registration form and log in form using asp.net

      Source Code can be download from here :