IT Masab

IT Masab

Sunday, 31 March 2013

Update Data Code


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();
        }
    }

Select or Get Data Code



Simple with sorting, Select or Get Data Code
 Step by Step:
1.  Call logic layer with appropriate parameter
(a). ClientId or other if required by logic.
(b). ViewState["SortBy"] ,which set before For Column name for Sorting.
(c). ViewState["SortAs"],which set before for Sorting as  asc(ascending) or desc(descending) order for column sorting.
(d). ViewState["SearchExprBy"], which set before for value ,which is takeb by search text box means search date by inserting name.
2.  First thing is that I am not using property here due to some search sort.
3. Take these value in Logic Layer method and send them to Data Class’s Mehod.
4. In DataClass take these value in assign to SqlParameter[] as before.
5. Send this SqlParameter[] and Store Procedure name to Mail DataClass for taking Dataset.
6. All method from first step has returen type will be Dataset.

//BindGrid for taking Dataset and Formating GridView such as total records etc.

public void BindGrid(string orderby, string strVal)  //BingGrid
    {
       
        try
        {
ClsEvent objEvent = new ClsEvent(); //Logic layer

            DataSet ds = objEvent.GetEvent
(Convert.ToInt32(Session["clientId"]),
ViewState["SortBy"].ToString(),
ViewState["SortAs"].ToString(),
ViewState["SearchExprBy"].ToString());
          
            Int32 intCnt;
            intCnt = ds.Tables[0].Rows.Count;
            if (ds.Tables[0].Rows.Count > 0)
            {
               
                lblTotalRecords.Text = "Total Records : " + intCnt;

                DataView dv = (DataView)ds.Tables[0].DefaultView;
                GVUSer.DataSource = ds;
                GVUSer.DataBind();
                lblGoto.Visible = true;
                txtPage.Visible = true;
                imgbtnGo.Visible = true;
            }
            else
            {
                lblTotalRecords.Text = "";
                GVUSer.DataBind();
                lblGoto.Visible = false;
                txtPage.Visible = false;
                imgbtnGo.Visible = false;

            }
        }
        catch (Exception ex)
        {

            lblError.Text = "<b>Following Error Found<p>" + ex + "</p></b>";
        }
    }



//Logic Layer

public DataSet GetEvent(int ClientId, string SortBy, string SortAs, string SearchExprBy)
    {
        DataSet ds = new DataSet();
       ClsEventDB objDB = new ClsEventDB();

        ds = objDB.GetEvent(ClientId, SortBy, SortAs, SearchExprBy);
        return ds;
    }

//Data Layer
internal DataSet GetEvent(int ClientId, string SortBy, string SortAs, string SearchExprBy)
    {
        SqlParameter[] param ={
                                new SqlParameter ("@SortBy", SortBy),
                                new SqlParameter ("@SortAs", SortAs),
                                new SqlParameter ("@Search", SearchExprBy),
                                new SqlParameter ("@ClientID", ClientId),

                           };

        DataSet ds = new DataSet();
        ds = ClsDataLayer.GetDataSet("[sp_Get_Event]", param);
        return ds;
    }

//Main Data Class
public static DataSet GetDataSet(string ProcName, SqlParameter[] param)
    {
        SqlConnection cn = GetConnection();
        DataSet Ds = new DataSet();
        SqlDataAdapter ObjAdapter = new SqlDataAdapter(ProcName, cn);       
        ObjAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
        ObjAdapter.SelectCommand.CommandTimeout = 0;
             
              try
        {
                     cn.Open();
                     foreach (SqlParameter par in param)
                     {
                           ObjAdapter.SelectCommand.Parameters.Add(par);
                     }
                     ObjAdapter.Fill(Ds);
            cn.Close();
        }
        catch (Exception Ex)
        {
            throw Ex;
        }
        finally
        {            
                     cn.Close();
              cn.Dispose();
        }

        return Ds;
    }
When you don’t send parameter and just take value :
Step by Step:
<![if !supportLists]>1.       <![endif]>Don’t Send anything in Mehod
<![if !supportLists]>2.       <![endif]>In DataClass send Only Procedure Name.
<![if !supportLists]>3.       <![endif]>Best Example take value of Country and Regions.

Code For Main DataLayer.
public static DataSet GetDS(string ProcName)
    {       
        DataSet ds = new DataSet();
        SqlConnection cn = GetConnection();
        using (cn)
        {
                  try
                  {
                        SqlDataAdapter da = new SqlDataAdapter(ProcName, cn);
                        da.SelectCommand.CommandType = CommandType.StoredProcedure;
                        da.SelectCommand.CommandTimeout = 0;
                        da.Fill(ds);
                cn.Close();
                cn.Dispose();
                  }
                  catch (Exception Ex)
                  {
                        throw Ex;
                  }
                  finally
                  {
                        cn.Close();
                        cn.Dispose();
                  }          

            return ds;
        }

    }