none
Entity Framework 6 does't save new record to sql server 2008 r2 RRS feed

  • Question

  • Hy,

    I have problem with saving new record into database using Entity Framework.

    When I run program, everything seems normal, without errors . Program shows existing, manually added records into the database, and new one too. But new one isn't save into database after running program.

    I've got no idea where's problem. There is code for add new record, show existing.

    Thanks for help!!

    Saturday, January 31, 2015 7:56 AM

Answers

  • Hello,

    It seems that you are trying to implement two-ways binding between database and datagridview. By default, Entity Framework does not support it, we need to convert creating DbContext Local ObservableCollection<T>  to the IBindinglist and setting it as the DataSource of a BindingSource.

    Here is an example:

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Data;
    
    using System.Data.SqlClient;
    
    using System.Drawing;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    using System.Windows.Forms;
    
    using System.Data.Entity;
    
    namespace P20141212
    
    {
    
        public partial class Form1 : Form
    
        {
    
            BindingSource bs = new BindingSource();
    
            DFDBEntities context = new DFDBEntities();
    
            public Form1()
    
            {
    
                //SqlDependency.Start(connection.ConnectionString);
    
                InitializeComponent();
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
    
            { 
    
                context.Orders.ToList();
    
                bs.DataSource = context.Orders.Local.ToBindingList();
    
                this.dataGridView1.DataSource = bs;
    
            }
    
            private void button1_Click(object sender, EventArgs e)
    
            {
    
                context.SaveChanges();
    
            }
    
        }
    
    }
    

    The ToBindingList() method is an extended method under namesapce System.Data.Entity, please do not forget to add it.           

    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.

    • Marked as answer by Afridi_ALAMDAR Thursday, February 5, 2015 9:05 AM
    Tuesday, February 3, 2015 8:35 AM
    Moderator

All replies

  • What is your connection string? I you use Express or Compact, have you looked in 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

    Saturday, January 31, 2015 10:30 AM
  • Thank you but I am saving data to the SQL Server 2008r2, as you may know that it create connection string automatically in the "app.config" file by Adding an Entity Data Modal to the project.

    I am using the Datagrid control bounded with a Bindingsource (by draging and drop on the form).

    Saturday, January 31, 2015 12:08 PM
  • Hello Afridi_ALAMDAR,

    For helping you looking into this thread, could you please provide some information:

    >>Program shows existing, manually added records into the database, and new one too.

    After you manually added recorded to database, would these new records show in your program?

    >>But new one isn't save into database after running program.

    From your description, it seems that you are using the DataGrid control to add new record, do you have use an explicit transaction? If you have, please make sure you call the Complete() method to commit the transation. If this is not the caused reason, please provide the code which you use to save data into database.

    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.

    Tuesday, February 3, 2015 3:31 AM
    Moderator
  • hi,

    do you have SaveChanges() or probably on the datagrid endedit() not called perhaps?

    please do provide any could that will help.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 3, 2015 3:51 AM
  • thank you sir to responding me. here is the code which is update my existing record in the database (sql server 2008r2) but not saving the new record (this is i am trying to edit/inserting row using Datagridview)

    using System;
    using System.Linq;
    using System.Windows.Forms;
    using Sales;
    namespace SalesApp
    {
        public partial class Form1 : Form
        {
            private SalesEntities dbContext;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                dbContext=new SalesEntities();
                var query = dbContext.Sales;
                this.saleBindingSource.DataSource = query.ToList();
            }
    
            private void saleBindingNavigatorSaveItem_Click(object sender, EventArgs e)
            {
    
                this.saleDataGridView.EndEdit();
               dbContext.SaveChanges();
            }
        }
    }
    


    Tuesday, February 3, 2015 6:04 AM
  • Hello,

    It seems that you are trying to implement two-ways binding between database and datagridview. By default, Entity Framework does not support it, we need to convert creating DbContext Local ObservableCollection<T>  to the IBindinglist and setting it as the DataSource of a BindingSource.

    Here is an example:

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Data;
    
    using System.Data.SqlClient;
    
    using System.Drawing;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    using System.Windows.Forms;
    
    using System.Data.Entity;
    
    namespace P20141212
    
    {
    
        public partial class Form1 : Form
    
        {
    
            BindingSource bs = new BindingSource();
    
            DFDBEntities context = new DFDBEntities();
    
            public Form1()
    
            {
    
                //SqlDependency.Start(connection.ConnectionString);
    
                InitializeComponent();
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
    
            { 
    
                context.Orders.ToList();
    
                bs.DataSource = context.Orders.Local.ToBindingList();
    
                this.dataGridView1.DataSource = bs;
    
            }
    
            private void button1_Click(object sender, EventArgs e)
    
            {
    
                context.SaveChanges();
    
            }
    
        }
    
    }
    

    The ToBindingList() method is an extended method under namesapce System.Data.Entity, please do not forget to add it.           

    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.

    • Marked as answer by Afridi_ALAMDAR Thursday, February 5, 2015 9:05 AM
    Tuesday, February 3, 2015 8:35 AM
    Moderator
  • Yes exactly this is worked, thank you very much.

    I thought that the binding with datagrid will work like the dataset, where the Grid column customization is easy and available in design mode(like renaming col, formating cell etc)

    using System;
    using System.Data.Entity;
    using System.Linq;
    using System.Windows.Forms;
    using Sales;
    namespace SalesApp
    {
        public partial class Form1 : Form
        {
            private SalesEntities dbContext;
            BindingSource bs = new BindingSource();
            public Form1()
            {
                
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                dbContext=new SalesEntities();
                dbContext.Sales.ToList();
    
                bs.DataSource = dbContext.Sales.Local.ToBindingList();
                this.saleDataGridView.DataSource = bs;
                this.saleBindingNavigator.BindingSource = bs;
            }
    
            private void saleBindingNavigatorSaveItem_Click(object sender, EventArgs e)
            {
                this.saleDataGridView.EndEdit();
                bs.EndEdit();
               dbContext.SaveChanges();
            }
        }
    }

    Thursday, February 5, 2015 9:26 AM