-1

I have a data access layer to update and trying to call method on different page wont update database

i have tried moving some code around to be outside of the loop and also set a break point but the data doesn't reach the break point

Data access layer code (DAL):

public void editBuyerByID(int bID, string title, string fname, string lname, string email, string pWord, string tel,string pCode, double price, string type)
{
  using (var context = dc)
  {
    Buyer editBuyer = (from b in context.Buyers where b.BuyerID == bID select b).FirstOrDefault();

    if (editBuyer != null)
      {
        editBuyer.BuyerTitle = title;
        editBuyer.BuyerFName = fname;
        editBuyer.BuyerLName = lname;
        editBuyer.BuyerEmail = email;
        editBuyer.BuyerPassword = pWord;
        editBuyer.BuyerTelNo = tel;
        editBuyer.BuyerPostcode = pCode;
        editBuyer.BuyerPriceRange = price;
        editBuyer.BuyerType = type;

        //context.Buyers.Add(editBuyer);
        context.SaveChanges();

      }
    }
}

Account Page where method being called:

   protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                aDal.editBuyerByID(Convert.ToInt32(Session["Buyer"].ToString()),
                                            txtTitle.Text, txtFName.Text, txtLName.Text,
                                            txtEmail.Text, txtPassword.Text, txtTelNo.Text, txtPostcode.Text, Convert.ToSingle(txtPrice.Text), txtType.Text);
                string editBuyerScript = "alert(\"Your details have been updated\");";
                ScriptManager.RegisterStartupScript(this, GetType(),
                                      "ServerControlScript", editBuyerScript, true);
            }
            catch
            {
                string invalidScript = "alert(\"All fields must be filled correctly\");";
                ScriptManager.RegisterStartupScript(this, GetType(),
                                      "ServerControlScript", invalidScript, true);
            }



        }
Rashid Ali
  • 587
  • 3
  • 13
amb101293
  • 5
  • 5

1 Answers1

0

in your DAL, editBuyerByID method could receive the changed Buyer object instead of a set of fields, in the method just mark the entity as changed and save changes:

public void editBuyerByID(Buyer buyer)
{
  using (var context = dc)
  {
    context.Entry(buyer).State = EntityState.Modified;
    context.SaveChanges();
  }
}

and in the btnUpdate_Click event make the following changes, get the buyer object (assuming you have method to get buyer by ID in the DAL), update fields and send to the editBuyerByID DAL method:

protected void btnUpdate_Click(object sender, EventArgs e)
{
  try
  {
    int buyerId = Convert.ToInt32(Session["Buyer"].ToString());
    Buyer buyer = aDal.getBuyerByID(buyerId);

      if(buyer!=null) 
      {
        buyer.BuyerTitle = txtTitle.Text;
        buyer.BuyerFName = txtFName.Text;
        buyer.BuyerLName = txtLName.Text;
        buyer.BuyerEmail = txtEmail.Text;
        buyer.BuyerPassword = txtPassword.Text;
        buyer.BuyerTelNo = txtTelNo.Text;
        buyer.BuyerPostcode = txtPostcode.Text;
        buyer.BuyerPriceRange = Convert.ToSingle(txtPrice.Text);
        buyer.BuyerType = txtType.Text;

        aDal.editBuyerByID(buyer);
      }

      string editBuyerScript = "alert(\"Your details have been updated\");";
      ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", editBuyerScript, true);
  }
  catch
  {
    string invalidScript = "alert(\"All fields must be filled correctly\");";
    ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", invalidScript, true);
  }
}
Rashid Ali
  • 587
  • 3
  • 13
  • i dont have the method to get the buyer id only a list of buyers – amb101293 Oct 02 '19 at 15:41
  • i have this code in DAL im not sure if its the same thing you mean : public Buyer findBuyerByID(int ID) { using (var context = new DBModel()) { Buyer findBuyer = (from b in context.Buyers where b.BuyerID == ID select b).FirstOrDefault(); return findBuyer; } } – amb101293 Oct 02 '19 at 19:26
  • Any help would be appreciated – amb101293 Oct 07 '19 at 17:51