none
Entity Framework Doesn't add to the Database RRS feed

  • Question

  • I'm 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:

    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("نام را وارد کنید");
            }
        }
    }

    • Moved by Kristin Xie Tuesday, September 2, 2014 5:43 AM
    Monday, September 1, 2014 4:02 PM

Answers

  • Hello saeed,

    You could try to declare a variable to check whether the data is inserted into database as:

    int affectrows = db.SaveChanges();

    The return value is the effect rows number, please check if yours is 1 since you are inserting one record.

    If it is 1, then you might consider the suggestion provided by ErikEJ, sometimes, if you are working with a database file as .mdf, it will save change to the copied file.

    If this does not work for you, please share your table definition with us.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 3, 2014 2:02 AM
    Moderator

All replies

  • Do you have a primary key on the table? If so, then what is the definition of the primary key?
    Monday, September 1, 2014 7:53 PM
  • Hi saeed,

    Like the title mentioned, this is an EF case. I have moved your thread to EF forum for better support.

    Best regards,

    Krsitin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 2, 2014 5:46 AM
  • have you checked the bin/debug folder for a copy of the database file?

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    Tuesday, September 2, 2014 6:24 AM
  • Hello saeed,

    You could try to declare a variable to check whether the data is inserted into database as:

    int affectrows = db.SaveChanges();

    The return value is the effect rows number, please check if yours is 1 since you are inserting one record.

    If it is 1, then you might consider the suggestion provided by ErikEJ, sometimes, if you are working with a database file as .mdf, it will save change to the copied file.

    If this does not work for you, please share your table definition with us.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 3, 2014 2:02 AM
    Moderator