Click here to Skip to main content
15,886,699 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Im using entity framework to register a customer using Windows Form but after calling the add method and calling the savechange method the row doesn't insert and i don't know why here is my code: there is a primary key column named Cid which is identity

it is a register form when ever from the main form the user click on the register button this form loads and he enters his data and press the submit button.but when i click the submit button it doesn't add to database after the call to .SaveChanges() I close my forms and see the data from the server explorer (R-click on the tables and show table data) and it is empty.and even I tried putting a breakpoint in the register form and see if the 'db' has any data which is none

here is my code:

C#
public partial class RegisterForm : Form
{
    PachinEntities1 db=new PachinEntities1();
    private Customer temp;
    public RegisterForm()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();  
    }

    private void RegisterForm_Load(object sender, EventArgs e)
    {
        temp=new Customer();
        DateTime gocal = DateTime.Now;
        PersianCalendar pcal=new PersianCalendar();
        int year=pcal.GetYear(gocal);
        int month = pcal.GetMonth(gocal);
        int day = pcal.GetDayOfMonth(gocal);
        string pday = string.Format("{0}/{1}/{2}", year, month, day);
        temp.Date = pday;
        txt_Date.Text = pday;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (txt_Name.Text.Trim().Length != 0)
        {

            if (txt_Tell1.Text.Trim().Length != 0)
            {
                if (txt_Address.Text.Trim().Length != 0)
                {
                    temp.Name = txt_Name.Text;
                    temp.Tell1 = txt_Tell1.Text;
                    temp.Tell2 = txt_Tell2.Text;
                    temp.Address = txt_Address.Text;
                    db.Customers.Add(temp);
                    db.SaveChanges();
                }
                else
                {
                    MessageBox.Show("آدرس را وارد کنید");
                }
            }
            else
            {
                MessageBox.Show("شماره تلفن را وارد کنید");
            }
        }
        else
        {
            MessageBox.Show("نام را وارد کنید");
        }
    }
}
Posted
Comments
mahmoodof 1-Sep-14 13:45pm    
hello
use && instead of nest if's.
Saeedek 1-Sep-14 13:51pm    
thank you.but that's not my problem exactly :)
mahmoodof 1-Sep-14 14:13pm    
if your solution Not to be your vs.
test it by seed info in db and be sure your Entity has value.
if you are using dbFisrt.
Saeedek 1-Sep-14 14:15pm    
What do you mean my Entity has value? I am using Dbfirst
mahmoodof 1-Sep-14 14:24pm    
Is there any Data in your db?
if yes : after getting your entity you will see them into that.
[ls test it.
if you have been those datas then your relation is ok.
if No : check your CS and going on

1 solution

Thank you everyone but my problem is solved.i used Using statement instead of globally make an instance of my context

C#
using (var db = new PachinEntities())
                        {
                            temp.Name = txt_Name.Text;
                            temp.Tell1 = txt_Tell1.Text;
                            temp.Tell2 = txt_Tell2.Text;
                            temp.Address = txt_Address.Text;
                            db.Customers.Add(temp);
                            db.SaveChanges();
                        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900