locked
Insert and delete wont work RRS feed

  • Question

  • Hello guys.

    I have a very simple windows form project with entity framework.

    Simply I draged my tables from "Data Source" tab in my "form" and it made for me a "datagridview" and a "bindingnavigator" and a "bindingsource".

    Data binded successfully and when I run project I can see "datagridview" filled with data correctly and I can update any cells value from "datagridview" or "bindingnavigator"

    Problem is I can not insert or delete any records in my datagridview or in "bindingnavigator".

    Here is some codes that I wrote hope it be usefull to solve problem:

            TeachersAndStudents_DBEntities context = new TeachersAndStudents_DBEntities();
    
            private void SaveItem_Click(object sender, EventArgs e)
            {
                context.SaveChanges();
                MessageBox.Show("saved successfully");
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                teachers_tbBindingSource.DataSource = context.teachers_tb.ToList();
            }
    Any help is appreciated.

    Thursday, August 2, 2012 4:08 PM

Answers

  • ToList() broke format of DataGridView.

    I removed it and problem solved.


    Computer Enterprise Masoud Keshavarz for more information contact masoudk1990@yahoo.com

    Saturday, August 4, 2012 5:47 AM

All replies

  • Well, since no one answerd I thought its better I give you guys some additional information about my project.

    //------------------

    I followed this

    free tutorial.

    //-----------------

    My orginal teachers_table in my sql database have following fields:

    person_id smallint (autoincrement) (primary key)

    fname varchar(10)

    lname varchar(10)

    age tinyint
    //-----------------

    In my edmx file I mapped all fields correctly and person_id "StoreGeneretedPattern" property is Identity and its "Entity Key" property is true.

    //-----------------------------

    I have other table named students_table that use teachers_table.person_id as foreign key to display which teachers teach to which students.

    Computer Enterprise Masoud Keshavarz for more information contact masoudk1990@yahoo.com

    Thursday, August 2, 2012 5:46 PM
  • After so many hours search and investigations I found that whenever I try to insert or delete a record in my "datagridview", changes will apply to my "BindingSource" successfully.

    But whenever I call context.savechanges() this changes wont apply from "BindingSource" to database.

    Any idea?


    Computer Enterprise Masoud Keshavarz for more information contact masoudk1990@yahoo.com

    Thursday, August 2, 2012 7:20 PM
  • Hi,

    I think the problem here is that you need to write code to add the new object to the context. You can add child items, the ones in the grids in the demo, automatically because EF is already tracking their parent and knows when the new objects are added. But to add a new parent object you need to explicitly add it to the context.

    I searched and found another post that shows you how to add the object to the context here: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/56e8c695-e253-45c6-af70-f1261f158b97


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Thursday, August 2, 2012 9:32 PM
  • No Glenn, "BindingSource" take care of changes in my "DataGridView" and I shouldnt have to write additional code to add new object whenever I insert new record in my "DataGridView".

    I tested my "BindingSource" and found that it successfully understand if new record add in "DataGridVeiw", but changes wont apply in database when I call context.savechanges();

    context.savechanges() work fine when I update a cell, but it dont work when I try to insert a new record in my "DataGridView".

    Thank you for reply, but thats not it.


    Computer Enterprise Masoud Keshavarz for more information contact masoudk1990@yahoo.com

    Friday, August 3, 2012 5:11 AM
  • I asked my question in here too, you can see their answers in there if you like.

    Computer Enterprise Masoud Keshavarz for more information contact masoudk1990@yahoo.com

    Friday, August 3, 2012 11:56 AM
  • ToList() broke format of DataGridView.

    I removed it and problem solved.


    Computer Enterprise Masoud Keshavarz for more information contact masoudk1990@yahoo.com

    Saturday, August 4, 2012 5:47 AM
  • Thanks Masoud.  I had the same problem with MyBindingSource.DataSource = _context.MyTable.Local.ToBindingList.  When I removed the ".ToBindingList" everything worked fine.  I'd like to follow up on my other forum questions concerning this but have noe idea what the actual problem was.  You say that "ToList() broke format of DataGridView".  Can you explain in more detail what this means?  You saved me on this one!

    .DataSource= _context.StlTripDetails.Local

    StlTripDetailBindingSource

    .DataSource= _context.StlTripDetails.Local.ToBindingList


    Thursday, January 3, 2013 10:45 PM