Having trouble updating DB Table from View entity RRS feed

  • Question

  • I've created a winform app that is using an EDM to populate the datagrids but on when trying to insert new records into this DGV I am getting this error: 'The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: The key-value pairs that define an EntityKey cannot be null or empty. Parameter name: record'
    This does insert a record into the DB but CompanyID, ExecutiveID, and ExecutiveType are all set to 0 which is not correct.
    So I would like to populate CompanyID, ExecutiveID, and ExecutiveType with the selected dropdown value before saving my entity changes. Can anyone help me retrieve these values and assign them to the entity property?
    I'm populating a datagrid with this code:  

    private void frmCompanyExecutives_Load(object sender, EventArgs e)
      dal = new DataAccessLayer();
    	// Get the List of CompanyExecutives from the DAL and bind to Datagrid 
    	this.dgvCompanyExecutives.DataSource = dal.GetCompanyExecutivesList();
    	dgvCompanyExecutives.Columns["CompanyName"].Visible = false;
    	dgvCompanyExecutives.Columns["ExecutiveName"].Visible = false;
    	// Get the List of Companies/Executives from the DAL and bind to Datagrid Dropdown columns
    	List<Companies> companies = dal.GetCompanyList1();
    	List<Executives> executive = dal.GetExecutivesList1();
            //Read each row in the Grid and determine what the ExecutiveType DropDown should be set to based on the ExecutiveTypeID 
    	foreach (DataGridViewRow  row in dgvCompanyExecutives.Rows)
    	    DataGridViewComboBoxCell rowCompanyCombocell = (DataGridViewComboBoxCell)row.Cells["CompanyName1"];                    
    	    rowCompanyCombocell.DataSource = companies;
    	    rowCompanyCombocell.DisplayMember = "CompanyName";
    	    rowCompanyCombocell.ValueMember = "CompanyID";
    	    rowCompanyCombocell.Value = row.Cells["CompanyID"].Value;
    	    DataGridViewComboBoxCell rowExecutiveCombocell = (DataGridViewComboBoxCell)row.Cells["ExecutiveName1"];
    	    rowExecutiveCombocell.DataSource = executive;
    	    rowExecutiveCombocell.DisplayMember = "ExecutiveName";
    	    rowExecutiveCombocell.ValueMember = "ExecutiveID";
    	    rowExecutiveCombocell.Value = row.Cells["ExecutiveID"].Value;
    	    //set ID to value(1 or 2) from ExecutiveTypeID cell for that row
    	    DataGridViewComboBoxCell rowExecTypeCombocell = (DataGridViewComboBoxCell)row.Cells["ExecutiveType"];
    	    int ExecutiveTypeID = Convert.ToInt32(row.Cells["ExecutiveTypeID"].Value);
    	    if (ExecutiveTypeID == 1)
    	    	rowExecTypeCombocell.Value = "Director";   
    		rowExecTypeCombocell.Value = "Secretary";	                
    	    dgvCompanyExecutives.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
    	    dgvCompanyExecutives.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
      catch (Exception ex)
        MessageBox.Show(string.Format("Exception occurred: {0}", ex.Message));

    On my btnSave click event I need to read any new rows/values from my datagrid to be inserted into the DB and update my entity fields with these values to make sure the Insert will work. 

    ON my view entity I have assigned an SP for Insert, 1 for Update, and 1 for Delete my Update and Delete are working fine. Can anyone please help me?

    Monday, March 12, 2012 4:30 PM

All replies

  • Hi pmcm83,

    Welcome to MSDN Forum.

    I'm not clear about the question, do you mean, you want to retrieve the data from datagridview and assign them to entities' properties? If I misunderstand, please feel free to correct me.

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, March 14, 2012 5:10 AM
  • Hi pmcm83,

    Have you solved the issue? I look forward to hearing from you.

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, March 19, 2012 2:26 AM