Wednesday, 25 July 2012

UPDATE USING 3 TIER ARCHITECTURE (CASCADING DROPDOWN)

Appropriate coding for tat file is:

Update.cs

public partial class member_updateprofile : System.Web.UI.Page
{
    BAL b = new BAL();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {

            ddlState.DataSource = b.GetState();
            ddlState.DataBind();
            ddlState.Items.Insert(0, new ListItem("Select", "0"));
            System.Data.DataTable dt = b.selectupdate();
            foreach (System.Data.DataRow r in dt.Rows)
            {
                lbl_Username.Text = dt.Rows[0][0].ToString();
                lbl_Email.Text = dt.Rows[0][1].ToString();
                txt_Name.Text = dt.Rows[0][2].ToString();
                ddlState.Items.FindByValue(dt.Rows[0][7].ToString());
                ddlState.DataBind();
                ddl_City.DataSource = b.GetCitiesByStateId(Convert.ToInt32(ddlState.SelectedValue));
                ddl_City.DataBind();
                ddlState.Items.Insert(0, new ListItem("Select", "0"));
                ddl_City.Items.FindByValue(dt.Rows[0][8].ToString());
                ddl_City.Items.Insert(0, new ListItem("Select", "0"));
                txt_Phone.Text = dt.Rows[0][4].ToString();
                txt_About.Text = dt.Rows[0][5].ToString();
                txt_Interest.Text = dt.Rows[0][6].ToString();
                rb_Individual.Checked = dt.Rows[0][3].ToString() == "1" ? true : false;
                rb_Business.Checked = dt.Rows[0][3].ToString() == "2" ? true : false;
            }


            b.username = lbl_Username.Text;
            b.email = lbl_Email.Text;
            b.name = txt_Name.Text;
            b.stateid = Convert.ToInt32(ddlState.SelectedValue);
            b.cityid = Convert.ToInt32(ddl_City.SelectedValue);

            b.phone1 = Convert.ToInt32(txt_Phone.Text);
            b.aboutMe = txt_About.Text;
            b.interests = txt_Interest.Text;

        }

    }


    protected void btnSave_Click(object sender, EventArgs e)
    {

        b.name = txt_Name.Text;
        b.stateid = Convert.ToInt32(ddlState.SelectedValue);
        b.cityid = Convert.ToInt32(ddl_City.SelectedValue);
        if (rb_Business.Checked == true)
        {
            b.type = 2;
        }
        else
            b.type = 1;
        b.phone1 = Convert.ToInt32(txt_Phone.Text);
        b.aboutMe = txt_About.Text;
        b.interests = txt_Interest.Text;
        b.updateprofile();
    }


    protected void ddlState_SelectedIndexChanged1(object sender, EventArgs e)
    {
        DataTable dt = b.GetCitiesByStateId(Convert.ToInt32(ddlState.SelectedValue));
        ddl_City.DataSource = dt;
        ddl_City.DataBind();
        ddl_City.Items.Insert(0, new ListItem("Select", "0"));
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        ClearData();
    }

    private void ClearData()
    {

        txt_Name.Text = "";
        ddlState.SelectedIndex = -1;
        ddl_City.SelectedIndex = -1;
        txt_Phone.Text = "";
        txt_About.Text = "";
        txt_Interest.Text = "";
        rb_Business.Checked = false;
        rb_Individual.Checked = false;
        
    }
}


In BAL.cs following code:

public class BAL
{

    public string _uname, _email;
    public int _type;
    public int _stateId, _cityId,_phone;
    public string _name, _aboutme, _interests;

    public BAL()
    {

    }
    public string username
    {
        get { return _uname; }
        set { _uname = value; }
    }
    
    public string email
    {
        get { return _email; }
        set { _email = value; }
    }

    public int type
    {
        get { return _type; }
        set { _type = value; }

    }
   
    public int stateid
    {
        get { return _stateId; }
        set { _stateId = value; }
    }

    public int cityid
    {
        get { return _cityId; }
        set { _cityId = value; }
    }
   
    public int phone1
    {
        get { return _phone; }
        set { _phone = value; }
    }

    public string name
    {
        get { return _name; }
        set { _name = value; }
    }

    public string aboutMe
    {
        get { return _aboutme; }
        set { _aboutme = value; }
    }

    public string interests
    {
        get { return _interests; }
        set { _interests = value; }
    }
   



 public int updateprofile()
    {
        SqlParameter[] p = new SqlParameter[9];
        p[0] = new SqlParameter("@Name", name);
        p[1] = new SqlParameter("@StateId", stateid);
        p[2] = new SqlParameter("@CityId", cityid);
        p[3] = new SqlParameter("@Type", type);
        p[4] = new SqlParameter("@Phone", phone1);
        p[5] = new SqlParameter("@AboutMe", aboutMe);
        p[6] = new SqlParameter("@Interest", interests);
        p[7] = new SqlParameter("@UserId", 2);
        p[8] = new SqlParameter("@Flag", 3);
        int update = DAL.executeNonquery_SP("edit_dt", p);
        return update;
    }
    
    public DataTable selectupdate()
    {
        SqlParameter[] p = new SqlParameter[2];
        p[0] = new SqlParameter("@userid",2);
         p[1] = new SqlParameter("@flag",2);
         DataTable selupdate = DAL.Ret_datatable("get_dt1", p);
         return selupdate;
    }
 public DataTable GetCitiesByStateId(int _stateid)
    {
        try
        {
            SqlParameter[] p = new SqlParameter[2];
            p[0] = new SqlParameter("@Flag", 8);
            p[1] = new SqlParameter("@stateid", _stateid);
            return DAL.Ret_datatable("Get_dt", p);
        }

        catch (ArgumentException ex)
        {
            throw new ArgumentException(ex.Message);
        }
    }
 public DataTable GetState()
    {
        try
        {
            SqlParameter[] p = new SqlParameter[1];
            p[0] = new SqlParameter("@Flag", 3);
            return DAL.Ret_datatable("Get_dt", p);
        }

        catch (ArgumentException ex)
        {
            throw new ArgumentException(ex.Message);
        }
    }


DAL.cs file

public class DAL
{
    static SqlConnection con;
    static SqlCommand cmd;
    public DAL()
    {

    }

    public static string connection = (ConfigurationManager.ConnectionStrings["classifiedsConnectionString1"].ConnectionString);

    public static int executeNonquery_SP(String CommandText, SqlParameter[] parameters)
    {
        using (SqlConnection con = new SqlConnection(connection))
        {
            try
            {
                cmd = new SqlCommand(CommandText, con);
                cmd.CommandType = CommandType.StoredProcedure;
                foreach (SqlParameter p in parameters)
                {
                    if (p.Value != null)
                    {
                        cmd.Parameters.Add(p);
                    }

                }
                con.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
    }


 public static DataTable Ret_datatable(String CommandText1, SqlParameter[] parameters)
    {

        con = new SqlConnection(connection);
        cmd = new SqlCommand(CommandText1, con);
        con.Open();
        cmd.CommandType = CommandType.StoredProcedure;
        foreach (SqlParameter c in parameters)
        {
            if (c.Value == null)
            {
            }
            cmd.Parameters.Add(c);
        }

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        da.Fill(ds);
        dt = ds.Tables[0];
        con.Close();
        return dt;
    }


Stored procedure for update:

UPDATE dbo.tbl_User
SET    Name=@NAME,Stateid=@Stateid,Cityid=@CITYID,Type=@TYPE,Phone=@PHONE,Aboutme=@ABOUTME,Interest=@INTEREST
WHERE UserId =@USERID

Stored procedure to select:

SELECT STATE,STATEID
FROM DBO.TBL_STATE



No comments:

Post a Comment