Friday 5 April 2013

Insert Data Code with Return Boolean and Int Result in three tier arch.


Insert Data Code with Return Boolean and Int Result
Step by Step: 
1. Create Property Class for This Section and create members as inserted data required.
2. Set Property to appropriate data taken by Control.
3. After taking data in property class just call Logic layer Class with this Property Class’s obj .
4. For Step 3 just create Logic Layer Class and create method which take Property Class’s obj and call        DataLayer’s Mehod with Property Class’s obj as a parameter.
5. For Step 4 just create DataLayer Class with method which take Property Class’s obj and by this obj create SqlParameter[] and set SqlParameter with the help of Properties Value.
6. After that call the method of Main Data Class, this class is vary on Company by Company.
7. This Main Data Class takes two parameter (a).Procedure Name (b). Sqlparameter[]
8. Return Type of All Methods will be same as return type of Main Data Class.
Code…
On Submit Button:::
  protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        /*******************************Insertion***************************************/
        try
        {
            if (!Page.IsValid)
                return;
            objProp.EventDate = Convert.ToDateTime(txtEventDate.Text);
            objProp.ClientID = Convert.ToInt32(Session["clientId"]);

            ClsEvent objEvent=new ClsEvent(); //Logic Class
            bool IsDone=objEvent.AddEvent (objProp );

                if (IsDone)
                    Response.Redirect("message.aspx?EventIsOpen=true&id=72");
               
                /*******************************End***************************************/
            }   
        catch (Exception ex)
        {
            lblError.Text = "<b>Following Error Found<p>" + ex + "</p></b>";
        }

    }


//Logic Class (ClsEvent)


public bool AddEvent(ClsEventProp objProp)
    {
        ClsEventDB objDB = new ClsEventDB(); //Data Class
        bool IsDone;
        IsDone = objDB.AddEvent(objProp);
        return IsDone;
    }



//Data Class (ClsEventDB)


internal bool AddEvent(ClsEventProp objProp)
    {
        SqlParameter[] param ={
                         
                                 new SqlParameter ("@EventDate",objProp .EventDate ),
                                 new SqlParameter ("@ClientId",objProp .ClientID )
                             };

//Call Main Data Layer Class Method.

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



Main Data Class’ Method.
(Always create mehod as Static)
1…[Mehod which return int Value for get value which return by Stored 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();
        }
    }

2…[Mehod which return Bool Value for inspect operaton success or not.]

public static bool InsertUpdate(String p, SqlParameter[] param)
    {
        bool isDone;
        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();
            isDone = Convert.ToBoolean(returnvalue.Value);
            return isDone;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
        }
    }

Help

No comments:

Post a Comment