private void FillControls(SqlInt32 ExpenseID, SqlInt32 UserID)
    {
        ExpenseBAL balExpense = new ExpenseBAL();
        ExpenseENT entExpense = new ExpenseENT();

        entExpense = balExpense.SelectByPK(ExpenseID, UserID);

        if (!entExpense.ExpenseName.IsNull)
        {
            txtExpenseName.Text = entExpense.ExpenseName.Value;
        }

        if (!entExpense.CatagoryID.IsNull)
        {
            ddlCatagoryList.SelectedValue = entExpense.CatagoryID.Value.ToString();
        }

        if (!entExpense.Date.IsNull)
        {
            txtdate.Text = entExpense.Date.Value.ToString();
        }

        if (!entExpense.Descripation.IsNull)
        {
            txtDescripation.Text = entExpense.Descripation.Value.ToString();
        }

        if (!entExpense.ExpenseAmount.IsNull)
        {
            txtExpenseAmount.Text = entExpense.ExpenseAmount.Value.ToString().Trim();
        }
    }
Example #2
0
        public Boolean Update(ExpenseENT entExpense)
        {
            ExpenseDAL dalExpense = new ExpenseDAL();

            if (dalExpense.Update(entExpense))
            {
                return(true);
            }
            else
            {
                Message = dalExpense.Message;
                return(false);
            }
        }
        public Boolean Insert(ExpenseENT entExpense)
        {
            using (SqlConnection objConn = new SqlConnection(ConnectionString))
            {
                objConn.Open();
                using (SqlCommand objCmd = objConn.CreateCommand())
                {
                    try
                    {
                        objCmd.CommandType = CommandType.StoredProcedure;
                        objCmd.CommandText = "PR_Expense_InsertByUserID";

                        objCmd.Parameters.Add("@ExpenseID", SqlDbType.Int).Direction     = ParameterDirection.Output;
                        objCmd.Parameters.Add("@ExpenseName", SqlDbType.VarChar).Value   = entExpense.ExpenseName;
                        objCmd.Parameters.Add("@ExpenseAmount", SqlDbType.Decimal).Value = entExpense.ExpenseAmount;
                        objCmd.Parameters.Add("@CatagoryID", SqlDbType.Int).Value        = entExpense.CatagoryID;
                        objCmd.Parameters.Add("@Date", SqlDbType.DateTime).Value         = entExpense.Date;
                        objCmd.Parameters.Add("@Descripation", SqlDbType.VarChar).Value  = entExpense.Descripation;
                        objCmd.Parameters.Add("@UserID", SqlDbType.Int).Value            = entExpense.UserID;

                        objCmd.ExecuteNonQuery();

                        entExpense.ExpenseID = Convert.ToInt32(objCmd.Parameters.Add("@ExpenseID", SqlDbType.Int).Value);
                        return(true);
                    }
                    catch (SqlException sqlex)
                    {
                        Message = sqlex.InnerException.ToString();
                        return(false);
                    }
                    catch (Exception ex)
                    {
                        Message = ex.InnerException.ToString();
                        return(false);
                    }
                    finally
                    {
                        if (objConn.State == ConnectionState.Open)
                        {
                            objConn.Close();
                        }
                    }
                }
            }
        }
        public Boolean Update(ExpenseENT entExpense)
        {
            using (SqlConnection objConn = new SqlConnection(ConnectionString))
            {
                objConn.Open();
                using (SqlCommand objCmd = objConn.CreateCommand())
                {
                    try
                    {
                        objCmd.CommandType = CommandType.StoredProcedure;
                        objCmd.CommandText = "PR_Expense_UpdateByPKAndUserID";

                        objCmd.Parameters.AddWithValue("@ExpenseID", entExpense.ExpenseID);
                        objCmd.Parameters.AddWithValue("@ExpenseName", entExpense.ExpenseName);
                        objCmd.Parameters.AddWithValue("@ExpenseAmount", entExpense.ExpenseAmount);
                        objCmd.Parameters.AddWithValue("@CatagoryID", entExpense.CatagoryID);
                        objCmd.Parameters.AddWithValue("@Date", entExpense.Date);
                        objCmd.Parameters.AddWithValue("@Descripation", entExpense.Descripation);
                        objCmd.Parameters.AddWithValue("@UserID", entExpense.UserID);

                        objCmd.ExecuteNonQuery();
                        return(true);
                    }
                    catch (SqlException sqlex)
                    {
                        Message = sqlex.InnerException.ToString();
                        return(false);
                    }
                    catch (Exception ex)
                    {
                        Message = ex.InnerException.ToString();
                        return(false);
                    }
                    finally
                    {
                        if (objConn.State == ConnectionState.Open)
                        {
                            objConn.Close();
                        }
                    }
                }
            }
        }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        #region Server Side Validation
        string strError = "";

        if (txtExpenseName.Text.Trim() == "")
        {
            strError += "Enter Expense Name";
        }

        if (ddlCatagoryList.SelectedIndex == 0)
        {
            strError += "Enter Catagory";
        }

        if (txtdate.Text.Trim() == "")
        {
            strError += "Enter Date";
        }

        if (txtExpenseAmount.Text.Trim() == "")
        {
            strError += "Enter Expense Amount";
        }

        if (strError.Trim() != "")
        {
            lblErrorMessage.Text = strError;
            return;
        }
        #endregion Server Side Validation

        #region Collect Data
        ExpenseENT entExpense = new ExpenseENT();

        if (txtExpenseName.Text.Trim() != "")
        {
            entExpense.ExpenseName = txtExpenseName.Text;
        }

        if (ddlCatagoryList.SelectedIndex > 0)
        {
            entExpense.CatagoryID = Convert.ToInt32(ddlCatagoryList.SelectedValue.ToString().Trim());
        }

        if (txtdate.Text.Trim() != "")
        {
            entExpense.Date = Convert.ToDateTime(txtdate.Text.ToString());
        }

        if (txtExpenseAmount.Text.Trim() != "")
        {
            entExpense.ExpenseAmount = Convert.ToDecimal(txtExpenseAmount.Text.ToString().Trim());
        }

        if (Session["UserID"] != null)
        {
            entExpense.UserID = Convert.ToInt32(Session["UserID"].ToString());
        }

        entExpense.Descripation = txtDescripation.Text;

        #endregion Collect Data

        ExpenseBAL balExpense = new ExpenseBAL();

        if (hfExpenseID.Value == "")
        {
            if (balExpense.Insert(entExpense))
            {
                lblErrorMessage.Text = "Data Insert Successfully...";
                divMessage.Visible   = true;
                ClearControls();
            }
            else
            {
                lblErrorMessage.Text = balExpense.Message;
            }
        }
        else
        {
            entExpense.ExpenseID = Convert.ToInt32(hfExpenseID.Value.ToString().Trim());

            if (balExpense.Update(entExpense))
            {
                lblErrorMessage.Text = "Data Updated Successfully...";
                divMessage.Visible   = true;
                ClearControls();
            }
            else
            {
                lblErrorMessage.Text = balExpense.Message;
            }
        }
        fillGridViewExpense(Convert.ToInt32(Session["UserID"].ToString()));
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "#add-contact", "$('body').removeClass('modal-open');$('.modal-backdrop').remove();", true);
    }
        public ExpenseENT SelectByPK(SqlInt32 ExpenseID, SqlInt32 UserID)
        {
            using (SqlConnection objConn = new SqlConnection(ConnectionString))
            {
                objConn.Open();
                using (SqlCommand objCmd = objConn.CreateCommand())
                {
                    try
                    {
                        objCmd.CommandType = CommandType.StoredProcedure;
                        objCmd.CommandText = "PR_Expense_SelectAllByPKAndUserID";

                        objCmd.Parameters.Add("@ExpenseID", SqlDbType.Int).Value = ExpenseID;
                        objCmd.Parameters.Add("@UserID", SqlDbType.Int).Value    = UserID;

                        ExpenseENT entExpense = new ExpenseENT();
                        using (SqlDataReader objSDR = objCmd.ExecuteReader())
                        {
                            while (objSDR.Read())
                            {
                                if (!objSDR["ExpenseID"].Equals(DBNull.Value))
                                {
                                    entExpense.ExpenseID = Convert.ToInt32(objSDR["ExpenseID"].ToString());
                                }

                                if (!objSDR["ExpenseName"].Equals(DBNull.Value))
                                {
                                    entExpense.ExpenseName = Convert.ToString(objSDR["ExpenseName"].ToString());
                                }

                                if (!objSDR["ExpenseAmount"].Equals(DBNull.Value))
                                {
                                    entExpense.ExpenseAmount = Convert.ToDecimal(objSDR["ExpenseAmount"].ToString());
                                }

                                if (!objSDR["CatagoryID"].Equals(DBNull.Value))
                                {
                                    entExpense.CatagoryID = Convert.ToInt32(objSDR["CatagoryID"].ToString());
                                }

                                if (!objSDR["Date"].Equals(DBNull.Value))
                                {
                                    entExpense.Date = Convert.ToDateTime(objSDR["Date"].ToString());
                                }

                                if (!objSDR["Descripation"].Equals(DBNull.Value))
                                {
                                    entExpense.Descripation = Convert.ToString(objSDR["Descripation"].ToString());
                                }
                            }
                        }
                        return(entExpense);
                    }
                    catch (SqlException sqlex)
                    {
                        Message = sqlex.InnerException.ToString();
                        return(null);
                    }
                    catch (Exception ex)
                    {
                        Message = ex.InnerException.ToString();
                        return(null);
                    }
                    finally
                    {
                        if (objConn.State == ConnectionState.Open)
                        {
                            objConn.Close();
                        }
                    }
                }
            }
        }