Paweł
Grzesiak
Wdrażanie,
integracja oraz
rozwój systemów ERP
Temat: Master/detail problem z zapisywaniem nowego rekordu
W bazie danych mam 2 tabele:Zamówienia i pozycje zamówień, w pozycjach zamówień jest FK do zamówień.
W programie mam DataGridView w którym wyświetlam Zamowienia
oraz form z edycją zamówienia gdzie w kontrolkach sa dane z tabeli Zamówienia a w DataGridView pozycje zamówienia cos na kształt typowego forma do wystawiania faktury bądź zmówienia, wszytsko byłoby ok gdyby nie istostny problem: nie inseruje mi sie do bazy nowe zamówienie (w zasadzie relacja pomiędzy zamówieniem a jego pozycjami - rekordy są tylko FK w pozycjach zamówień jest NULL) przy pozostałych dzialaniach jest OK tylko z tym się męczę - jesli ktoś ma linka do tutoriala w tym temacie wzglęnie może wskazać droge do rozwiązania tego problemu będę wdzięczny.
Kod wygląda mniej więcej tak /różnie juz do tego tematu starałem się podejść /:
[Kod]
private void LoadMasterDetailData()
{
malForms.db.dbConnection dbconn = new malForms.db.dbConnection();
try
{
conn = dbconn.connection();
dataSet = new DataSet("invoice");
//parent
parentdataAdapter = new SqlDataAdapter(this.parent_qrySelect, conn);
//parentdataAdapter.FillSchema(dataSet, SchemaType.Mapped, "parentTbl");
parentdataAdapter.Fill(dataSet, "parentTbl");
//child
childdataAdapter = new SqlDataAdapter(this.child_qrySelect, conn);
//childdataAdapter.FillSchema(dataSet, SchemaType.Mapped, "childTbl");
childdataAdapter.Fill(dataSet, "childTbl");
//realcja
masterColumn = dataSet.Tables["parentTbl"].Columns[this.parent_PKname];
detailColumn = dataSet.Tables["childTbl"].Columns[this.child_FKname];
relation = new DataRelation("masterdetail", masterColumn, detailColumn);
dataSet.Relations.Add(relation);
parentbuilder = new SqlCommandBuilder(parentdataAdapter);
parentdataAdapter.InsertCommand = parentbuilder.GetInsertCommand();
//parentdataAdapter.InsertCommand.CommandText = String.Concat(parentdataAdapter.InsertCommand.CommandText, "; " + this.parent_qrySelect + " WHERE " + this.parent_PKname + " = @@IDENTITY");
//parentdataAdapter.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
parentdataAdapter.UpdateCommand = parentbuilder.GetUpdateCommand();
parentdataAdapter.DeleteCommand = parentbuilder.GetDeleteCommand();
childbuilder = new SqlCommandBuilder(childdataAdapter);
childdataAdapter.InsertCommand = childbuilder.GetInsertCommand();
childdataAdapter.UpdateCommand = childbuilder.GetUpdateCommand();
childdataAdapter.DeleteCommand = childbuilder.GetDeleteCommand();
bindingSource.DataSource = dataSet;
bindingSource.DataMember = "parentTbl";
childbindingSource.DataSource = bindingSource;
childbindingSource.DataMember = "masterdetail";
this.grid.DataSource = bindingSource;
this.grid.AutoGenerateColumns = true;
if (bindingSource.Count == 0)
{
this.deleteBtn.Enabled = false;
this.editBtn.Enabled = false;
this.exportBtn.Enabled = false;
this.printBtn.Enabled = false;
}
else
{
this.deleteBtn.Enabled = true;
this.editBtn.Enabled = true;
this.exportBtn.Enabled = true;
this.printBtn.Enabled = true;
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
finally
{
if (conn != null) conn.Close();
}
}
private void insertBtn_Click(object sender, EventArgs e)
{
bindingSource.AddNew();
if (IsMasterDetalil == true)
{
//childbindingSource.AddNew();
Invoice_Form.FormClosed += new FormClosedEventHandler(Invoice_Form_FormClosed);
Invoice_Form.ShowDialog(this, bindingSource, childbindingSource);
}
else
{
Edit_Form.FormClosed += new FormClosedEventHandler(Edit_Form_FormClosed);
Edit_Form.ShowDialog(this, bindingSource);
}
}
void Invoice_Form_FormClosed(object sender, FormClosedEventArgs e)
{
Validate();
bindingSource.EndEdit();
childbindingSource.EndEdit();
if (Invoice_Form.m_Update == true)
{
parentdataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
childdataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
parentdataAdapter.RowUpdated += new SqlRowUpdatedEventHandler(OnParentRowUpdated);
parentdataAdapter.Update(dataSet.Tables["parentTbl"].GetChanges(DataRowState.Added));
childdataAdapter.RowUpdated += new SqlRowUpdatedEventHandler(OnChildRowUpdated);
childdataAdapter.Update(dataSet.Tables["childTbl"].GetChanges(DataRowState.Added));
dataSet.Merge(dataSet.Tables["parentTbl"].GetChanges(DataRowState.Added));
dataSet.Merge(dataSet.Tables["childTbl"].GetChanges(DataRowState.Added));
dataSet.AcceptChanges();
}
LoadMasterDetailData();
}