Wednesday 9 January 2013

Login example using stored procedure in asp.net Part-2.


In the previous  I had described tables and stored procedure in” Login example using stored procedurein asp.net Part-1.
In this part I will describe the remaining part . In this section create a separate connection class and add four web page in your project. Create the hierarchy like this..
 

Code for Connection class:-
Connection.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Connection
/// </summary>
public class Connection
{
    public SqlConnection con = new SqlConnection("server=SUSHIL-PC;database=Login ;Integrated Security=true;");
                public Connection()
                {
                                //
                                // TODO: Add constructor logic here
                                //
                }
    public  void ConnOpen()
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        
    }
    public void ConnClose()
    {
        try
        {
            if (con.State == ConnectionState.Open)
            {
                con.Dispose();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
    }
}

Design Login Page Like this.



Login .aspx.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Security;
using System.Web.Security;

public partial class Login : System.Web.UI.Page
{
    Connection sqlConn = new Connection();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
       
        SqlCommand com = new SqlCommand("sp_checkUser",sqlConn.con); // sp_checkUser is stored procedure
        com.CommandType = CommandType.StoredProcedure;
        SqlParameter p1 = new SqlParameter("userName",txtUseName.Text);
        SqlParameter p2 = new SqlParameter("pass", txtPass.Text);
        com.Parameters.Add(p1);
        com.Parameters.Add(p2);
        sqlConn.ConnOpen();
        SqlDataReader dr = com.ExecuteReader();
        try
        {
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Response.Redirect("Home.aspx");
                }
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = "You are Existing User";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
           sqlConn.ConnClose();
        }
    }
}

Design NewUser page like this:-



NewUser.aspx.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    Connection sqlConn = new Connection();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void txtUserName_TextChanged(object sender, EventArgs e)
    {
        if (txtUserName.Text != string.Empty)
        {
            SqlCommand com = new SqlCommand("sp_userNameAvailability",sqlConn.con);
            com.CommandType = CommandType.StoredProcedure;
            SqlParameter p1 = new SqlParameter("userName", txtUserName.Text);
            //p1.Value = txtUserName.Text.Trim().ToString();
            com.Parameters.Add(p1);
            sqlConn.ConnOpen();
            SqlDataReader dr = com.ExecuteReader();
           // int result = Convert.ToInt32(com.ExecuteScalar());
           
            if (dr.HasRows)
            {
                dr.Read();
                lblMessage.Visible = true;
                lblMessage.Text = "User Name not Available";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = " Available";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            sqlConn.ConnClose();
            
        }
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        SqlCommand com = new SqlCommand("sp_newUser", sqlConn.con);
        com.CommandType = CommandType.StoredProcedure;
        SqlParameter p1 = new SqlParameter("userName", txtUserName.Text);
        SqlParameter p2 = new SqlParameter("firstName", txtFirstNAme.Text);
        SqlParameter p3 = new SqlParameter("lastName", txtLastName.Text);
        SqlParameter p4 = new SqlParameter("pass", txtPass.Text);
        SqlParameter p5 = new SqlParameter("emailId", txtEmailId.Text);
        SqlParameter p6 = new SqlParameter("securityQuestion", txtSecurityQuestion.Text);
        SqlParameter p7 = new SqlParameter("answer", txtAnswer.Text);

       
        com.Parameters.Add(p1);
        com.Parameters.Add(p2);
        com.Parameters.Add(p3);
        com.Parameters.Add(p4);
        com.Parameters.Add(p5);
        com.Parameters.Add(p6);
        com.Parameters.Add(p7);
        sqlConn.ConnOpen();

                int res = com.ExecuteNonQuery();
       
        sqlConn.ConnClose();
        if (res > 0)
        {
            lblRegister.Visible = true;
            lblRegister.Text = "Your Registration Successfully";
            lblRegister.ForeColor = System.Drawing.Color.Green;
            Response.Redirect("Registration_Confirm.aspx");
        }
        else
        {
            lblRegister.Visible = true;
            lblRegister.Text = "Registration Unsuccessfull";
            lblRegister.ForeColor = System.Drawing.Color.Red;
        }
    }
}

Add two more page just for giving a message to user like regiatration confirmation and Home page.

When you click on login button and after successful  login it will redirect to page Home.aspx. If  click on register button then it will redirect to registration_confirm.aspx  .

If any problem regarding this post free to contact me . My email id sushilct86@gmail.com .

Click Here to download the code.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.