Friday 5 April 2013

Update Data Code using three tier Architecture


Update Data Code with the help of Id taken from Query String or from other Parameter
Step by Step :
1.       Take Id from Query String which come from other page ( for example from GridView Edit Button in GridView Command Event by [ e.CommandArgument ]
 in the region of [ e.Command Name ]
2.       This Process is same as Insert Process accept it take another paramer for Id which will be put in Sql Procedure with where clause.

Code:

//Update Button Code
  protected void btnUpdate_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
/*******************************Updation********************************/

            objProp.EventTitle = txtEventTitle.Text.Trim();
            objProp.EventDate = Convert.ToDateTime(txtEventDate.Text);
            objProp.ClientID = Convert.ToInt32(Session["clientId"]);

            objProp .EventId =Convert.ToInt32(Request.QueryString["id"]);

            ClsEvent objEvent = new ClsEvent();
            bool IsDone=objEvent .UpadteEvent(objProp );
            if (!IsDone)
            {
                Response.Redirect("message.aspx?EventIsOpen=true&id=73");
            }
            else
                lblError.Text = "Error Occure";
                /*******************************End***************************************/
            }
       
        catch (Exception ex)
        {
            lblError.Text = "<b>Following Error Found<p>" + ex + "</p></b>";
        }

    }

//Update Button Code
public bool UpadteEvent(ClsEventProp objProp)
    {
        bool IsDone;

        ClsEventDB objDB = new ClsEventDB();

        IsDone = objDB.UpadteEvent(objProp);
        return IsDone;
    }

//Update Button Code
internal bool UpadteEvent(ClsEventProp objProp)
    {
        SqlParameter[] param ={
             new SqlParameter ("@EventId",objProp.EventId ),
           new SqlParameter ("@EventTitle",objProp .EventTitle ),
       new SqlParameter ("@EventDate",objProp .EventDate ),
       new SqlParameter ("@ClientId",objProp .ClientID )
                             };

        return ClsDataLayer.GetScaler("sp_Update_Event", param);
    }

//Main Datalayer Code
[ When return true false for confirmation of right execution ]
internal static bool GetScaler(string ProcName, SqlParameter[] param)
    {
        bool IsDone;
        SqlConnection cn = GetConnection();
        SqlCommand cmd = new SqlCommand(ProcName, cn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandTimeout = 0;
        cn.Open();
        SqlParameter returnValue = new SqlParameter("returnValue", SqlDbType.Int);
        returnValue.Direction = ParameterDirection.ReturnValue;
        cmd.Parameters.Add(returnValue);
        foreach (SqlParameter par in param)
        {
            cmd.Parameters.Add(par);
        }
        cmd.ExecuteNonQuery();
        IsDone = Convert.ToBoolean(returnValue.Value);
        cn.Close();
        cn.Dispose();
        return IsDone;

    }


[ When return int result for taking value which is return by stroed procedure]
public static int InsertUpdate(String p, SqlParameter[] param)
    {
        int id;
        SqlConnection conn = GetConnection();
        SqlCommand cmd = new SqlCommand(p, conn);
        try
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandTimeout = 0;
            conn.Open();
SqlParameter returnvalue = new SqlParameter("returnvalue", SqlDbType.Int);
            returnvalue.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(returnvalue);
            foreach (SqlParameter para in param)
            {
                cmd.Parameters.Add(para);
            }
            cmd.ExecuteNonQuery();
            id = Convert.ToInt32(returnvalue.Value);
            return id;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
        }
    }

No comments:

Post a Comment