1

I was wondering if someone can help me in this case: I'm trying to save my changes to database, so I use a context, and I have _tblcustomer which is an object from my entity classes, here is my code:

private void BtnSaveCustomer_Click(object sender, EventArgs e)
{
    if (CustomerMode == (int)CustomerModeOperaton.insert)
    {
        if (!string.IsNullOrWhiteSpace(TxtCustomerName.Text) ||
            !string.IsNullOrWhiteSpace(TxtLastName.Text) ||
            !string.IsNullOrWhiteSpace(TxtCustomerCode.Text))
        {
            tblCustomer Customer = new tblCustomer();

            Customer.CustomerName = TxtCustomerName.Text.ToString();
            Customer.CustomerCode = Convert.ToInt32(TxtCustomerCode.Text);

            if (!string.IsNullOrWhiteSpace(TxtCustomerAdress.Text))
            { 
                 Customer.CustomerAdresse = TxtCustomerAdress.Text.ToString();
            }
            else
            {
                Customer.CustomerAdresse = null;
            }

            if (!string.IsNullOrWhiteSpace(TxtCustomerPhone.Text))
            { 
                 Customer.CustomerPhone = Convert.ToInt32(TxtCustomerPhone.Text); 
            }
            else
            {
                Customer.CustomerPhone = null;
            }

            if (!string.IsNullOrWhiteSpace(TxtCustomerCellphone.Text))
            {
                Customer.CustomerCellPhone = Convert.ToInt32(TxtCustomerCellphone.Text);
            }
            else
            {
                Customer.CustomerCellPhone = null;
            }

            Customer.CustomerLastName = TxtLastName.Text.ToString();
            Customer.CustomerID = Guid.NewGuid();
            Customer.rowguid = Guid.NewGuid();

            using (var Context = new FactorEntities())
            {
                Context.tblCustomers.Add(Customer);
                Context.SaveChanges();
            }

            MessageBox.Show("اطلاعات مشتری در سیستم ثبت شد");
            // status=1;
        }
        else
        {
            MessageBox.Show("نام مشتری و نام خانوادگی و کد مشتری باید پر شوند");
        }
    }
    else 
    {
        using (var context = new FactorEntities())
        {
            var CustomerDetaile = context.tblCustomers.Find(CustomerID);

            _tblCustomer = new tblCustomer();
            _tblCustomer.CustomerID = CustomerDetaile.CustomerID;
            _tblCustomer.CustomerName = TxtCustomerName.Text;
            _tblCustomer.CustomerLastName = TxtLastName.Text;
            _tblCustomer.CustomerCode = Convert.ToInt32(TxtCustomerCode.Text);
            _tblCustomer.CustomerAdresse = TxtCustomerAdress.Text;

            context.SaveChanges();
        }

        MessageBox.Show("اطلاعات در سیستم ثبت شد");
    }
}

Main part is here:

using (var context =new FactorEntities())
{
    var CustomerDetaile = context.tblCustomers.Find(CustomerID);
    _tblCustomer = new tblCustomer();
    _tblCustomer.CustomerID = CustomerDetaile.CustomerID;
    _tblCustomer.CustomerName = TxtCustomerName.Text;
    _tblCustomer.CustomerLastName = TxtLastName.Text;
    _tblCustomer.CustomerCode = Convert.ToInt32(TxtCustomerCode.Text);
    _tblCustomer.CustomerAdresse = TxtCustomerAdress.Text;

    context.SaveChanges();
}

but I don't know why it does not save yet...

Thanks in advance.

5
  • context.SaveChanges(); should do it, no? Commented May 25, 2017 at 21:42
  • @jdmdevdotnet yes sir.. but it dose not work Commented May 25, 2017 at 21:43
  • "it does not work" what doesn't work? context.SaveChanges works. What error messages? Commented May 25, 2017 at 21:46
  • @jdmdevdotnet there is no error message, context.SaveChanges should save my changes to data base, for example I change Customer last name with this code _tblCustomer.CustomerLastName = TxtLastName.Text; but it dose not save in my data base Commented May 25, 2017 at 22:00
  • Il you want tu modify the last name, you should instead write CustomerDetaile.CustomerLastName = TxtLastName.Text. Commented May 25, 2017 at 22:06

3 Answers 3

4

I don't see anywhere you add the _tblCustomer object in the context, with something like in your "main part"

context.tblCustomers.Add(_tblCustomer);

If instead you want to modify an exiting object, you should write instead

CustomerDetaile.CustomerId = "the new id"

And now it will be saved.

What you are doing now, is creating a new customer, assigning its values and do nothing with it.

Sign up to request clarification or add additional context in comments.

2 Comments

because I shouldn't use this code... I have this customer in my database so why I should add it again !? If I add I would faced with this error:Cannot insert duplicate key in object 'dbo.tblCustomers'
In this case, you must modify the entity you took from the database, not creating a new one (I edited my answer)
0

To save new object to database in this case you would need to use:

var obj = context.TableName.New();
  obj.Name = "BLA"; 
  obj.Salary = 32; 
  context.TableName.Add(obj);
  context.SaveChanges();

To edit existing object in the table:

var obj = context.TableName.Find(id); 
obj.Name = "BLA"; 
obj.Salary = 32; 
context.Entry(obj).State = EntryState.Modified; 
context.SaveChanges();

It always worked for me=) Apply this concept to your code and it may work.

3 Comments

I didn't get Entrystate ,what is Entrystate in my code?
Not in yours, just for future if you would want to change something in existing object you would have to type that.
Example, var obj = context.TableName.Find(id); obj.Name = "BLA"; obj.Salary = 32; context.Entry(obj).State = EntryState.Modified; context.SaveChanges();
0

I shouldn't make a new ... I have to write code for my select... I got it by @ChristianKouamé help here is my code:

using (var context =new FactorEntities())
                {
                   var CustomerDetaile= context.tblCustomers.Find(CustomerID);
                   CustomerDetaile.CustomerID = CustomerID;
                   if (!string.IsNullOrWhiteSpace(TxtCustomerName.Text))
                   {
                       CustomerDetaile.CustomerName = TxtCustomerName.Text;

                   }


                   CustomerDetaile.CustomerLastName = TxtLastName.Text;
                   CustomerDetaile.CustomerAdresse = TxtCustomerAdress.Text;
                   CustomerDetaile.CustomerCellPhone = Convert.ToInt32(TxtCustomerCellphone.Text);
                   CustomerDetaile.CustomerCode = Convert.ToInt32(TxtCustomerCode.Text);
                   context.SaveChanges();
                }

thanks a lot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.