I wish to have a form with a gridview on one side and a panel with current record data on the other side (See attached file) so that I can edit data in the gridview or on the panel. Editing works fine, but problems appear on inserting.
Code behind update button is following:
private void UpdateGridInfo(GridViewRowInfo currentRow)
{
if (currentRow == null)
return;
DB.Contract rowView = (DB.Contract)currentRow.DataBoundItem;
if (rowView.ID == 0)
{
// insert
DB.Contract contract = new DB.Contract();
contract.RadioStationID = ((DB.vRadioStation)bsRadioStations2.Current).acSubject;
...
App.ContractsDAL.DC.Contracts.InsertOnSubmit(contract);
}
else
{
// edit
rowView.RadioStationID = ((DB.vRadioStation)bsRadioStations2.Current).acSubject;
...
}
}
private void bUpdate_Click(object sender, EventArgs e)
{
UpdateGridInfo(gvContracts.CurrentRow);
App.ContractsDAL.SubmitChanges();
App.ContractsDAL.RefreshDC();
bsContracts.DataSource = App.ContractsDAL.Get();
}
When I click add a new row on the gridview and then edit data on the panel, I get exceptions for NOT NULL columns, foreign keys... when I try to submit changes, for example:
Cannot insert the value NULL into column 'RadioStationID', table 'RadioStations.dbo.Contracts'; column does not allow nulls. INSERT fails.
The statement has been terminated.
If I debug program, I see that contract.RadioStationID is set to correct value. At first it seems, that gridview automatically posts NULL values, but if I set columns to Nullable and delete relationships from database, everything works as it is supposed to.
How could I make this work without setting columns to Nullable and removing relationships?
Code behind update button is following:
private void UpdateGridInfo(GridViewRowInfo currentRow)
{
if (currentRow == null)
return;
DB.Contract rowView = (DB.Contract)currentRow.DataBoundItem;
if (rowView.ID == 0)
{
// insert
DB.Contract contract = new DB.Contract();
contract.RadioStationID = ((DB.vRadioStation)bsRadioStations2.Current).acSubject;
...
App.ContractsDAL.DC.Contracts.InsertOnSubmit(contract);
}
else
{
// edit
rowView.RadioStationID = ((DB.vRadioStation)bsRadioStations2.Current).acSubject;
...
}
}
private void bUpdate_Click(object sender, EventArgs e)
{
UpdateGridInfo(gvContracts.CurrentRow);
App.ContractsDAL.SubmitChanges();
App.ContractsDAL.RefreshDC();
bsContracts.DataSource = App.ContractsDAL.Get();
}
When I click add a new row on the gridview and then edit data on the panel, I get exceptions for NOT NULL columns, foreign keys... when I try to submit changes, for example:
Cannot insert the value NULL into column 'RadioStationID', table 'RadioStations.dbo.Contracts'; column does not allow nulls. INSERT fails.
The statement has been terminated.
If I debug program, I see that contract.RadioStationID is set to correct value. At first it seems, that gridview automatically posts NULL values, but if I set columns to Nullable and delete relationships from database, everything works as it is supposed to.
How could I make this work without setting columns to Nullable and removing relationships?