Sunday, 2 March 2014

Session Using DataSet

DAL
public DataSet check_Sess_DSet(Prop_Sess pro)
    {
        con.Open();
        cmd = new SqlCommand("SP_REG_PER", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@uname", pro.Cname);
        cmd.Parameters.AddWithValue("@pwd", pro.Pwd);
        cmd.Parameters.AddWithValue("@Email", pro.Email);
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        con.Close();
        return ds;
    }
BLL
public DataSet check_Sess_DSet(Prop_Sess _Pro)
    {
        DataSet ds = d.check_Sess_DSet(_Pro);
        return ds;
    }
Application Layer
 protected void btn_Log_Click(object sender, EventArgs e)
    {
        Label1.Visible = false;
        DataSet ds = null;
        _Pro.Cname = txt_Uname.Text;
        _Pro.Pwd = txt_Pwd.Text;
        _Pro.Email = txt_Email.Text;
        ds = b.check_Sess_DSet(_Pro);


        if (ds.Tables[0].Rows.Count > 0 && txt_Uname.Text == ds.Tables[0].Rows[0]["uname"].ToString() && txt_Pwd.Text == ds.Tables[0].Rows[0]["pwd"].ToString())
        {
            Session["Reg_ID"] = ds.Tables[0].Rows[0]["reg_ID"];
            Session["FirstName"] = ds.Tables[0].Rows[0]["fname"];
            Session["LastName"] = ds.Tables[0].Rows[0]["lname"];
            Session["UserName"] = ds.Tables[0].Rows[0]["uname"];
            Session["Password"] = ds.Tables[0].Rows[0]["pwd"];

            Response.Redirect("Redirect.aspx", true);
        }
        else
        {
            Label1.Visible = true;
            Label1.ForeColor = System.Drawing.Color.Red;
            Label1.Text = "Invalid Login Credentials";
        }

    }
Redirect Page
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Reg_ID"] != null)
        {
            txt_REgID.Text = Session["Reg_ID"].ToString();
        }
        if (Session["FirstName"] != null)
        {
            txt_FName.Text = Session["FirstName"].ToString();
        }
        if (Session["LastName"] != null)
        {
            txt_LName.Text = Session["LastName"].ToString();
        }
        if (Session["UserName"] != null)
        {
            txt_UName.Text = Session["UserName"].ToString();
        }
        if (Session["Password"] != null)
        {
            txt_Pwd.Text = Session["Password"].ToString();
        }
       
    }

Session Using DataTable

DAL
 public DataTable check_Sess_DTable(Prop_Sess pro)
    {
        con.Open();
        cmd = new SqlCommand("sp_Reg", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@uname", pro.Cname);
        cmd.Parameters.AddWithValue("@pwd", pro.Pwd);
        da = new SqlDataAdapter(cmd);
        dt = new DataTable();
        da.Fill(dt);
        con.Close();
        return dt;


    }
BLL
    public DataTable check_Sess_DTable(Prop_Sess _Pro)
    {
       DataTable dt= d.check_Sess_DTable(_Pro);
       return dt;
    }

Application Layer
protected void Btn_Login_Click(object sender, EventArgs e)
    {
        Label1.Visible = false;
        DataTable dt = null;
        _Pro.Cname = txt_UName.Text;
        _Pro.Pwd = txt_Pwd.Text;
        dt = b.check_Sess_DTable(_Pro);
        if (dt.Rows.Count > 0 &&  txt_UName.Text.ToString()== dt.Rows[0][3].ToString() && txt_Pwd.Text.ToString() == dt.Rows[0][5].ToString())
        {
            Session["Reg_ID"] = dt.Rows[0][0].ToString();
            Session["FirstName"] = dt.Rows[0][1].ToString();
            Session["LastName"] = dt.Rows[0][2].ToString();
            Session["UserName"] = dt.Rows[0][3].ToString();
            Session["Password"] = dt.Rows[0][5].ToString();

            Response.Redirect("Redirect.aspx",true);
        }
        else
        {
            Label1.Visible = true;
            Label1.ForeColor = System.Drawing.Color.Red;
            Label1.Text = "Invalid Login Credentials";
        }
       }
Redirect Page
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Reg_ID"] != null)
        {
            txt_REgID.Text = Session["Reg_ID"].ToString();
        }
        if (Session["FirstName"] != null)
        {
            txt_FName.Text = Session["FirstName"].ToString();
        }
        if (Session["LastName"] != null)
        {
            txt_LName.Text = Session["LastName"].ToString();
        }
        if (Session["UserName"] != null)
        {
            txt_UName.Text = Session["UserName"].ToString();
        }
        if (Session["Password"] != null)
        {
            txt_Pwd.Text = Session["Password"].ToString();
        }
       

    }