problem when inserting detail into database, the datarowstatus on header become added too.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Good afternoon,
I have problem when inserting header detail to the database (oledb), let say, order header and order detail.
I use oledbcommand to create the insert and update manually, and create 2 more dataset
1. dsAdded = ds.GetChanges(DataRowState.Added), basing on this dsAdded, I execute the command Insert.
2. dsModified = ds.GetChanges(DataRowState.Modified), basing on this dsModified, I execute the command Update
Then I filled dataset and display on the datagridview, everything is fine.
When I added some more record in Order Detail, the dsAdded, table Order Detail give me the record tha i just added, Its absolutly right, but on dsAdded, table Order Header, It give me that record too, its not right, because the OledbCommand insert
the Order Header too, which is I didnt add a new record to Order Header ( I only added a new record to order detail), It give a an error, ORA-00001: unique constraint(TESt.PK_ORD) Vialoted
Please help, what wrong with the code.
Thank you
---some of the code-------------------
Form1_Load
<span style="font-size:x-small <font size="2
conn = GlobalCl.
</font>
<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small Connection<span style="font-size:x-small .GetConnection();

conn.Open();
<font size="2

</font>

<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small OleDbDataAdapter<span style="font-size:x-small daParent =
<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small new<span style="font-size:x-small
<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small OleDbDataAdapter<span style="font-size:x-small (<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "Select
* from ord_h"<span style="font-size:x-small , conn);<font size="2


</font>

<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small OleDbDataAdapter<span style="font-size:x-small daChild =
<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small new<span style="font-size:x-small
<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small OleDbDataAdapter<span style="font-size:x-small (<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "Select
* from ord_d"<span style="font-size:x-small , conn);

<font size="2
daParent.TableMappings.Add(
</font>

<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "Table"<span style="font-size:x-small ,
<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "ord_h"<span style="font-size:x-small );<font size="2

daChild.TableMappings.Add(
</font>

<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "Table"<span style="font-size:x-small ,
<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "ord_d"<span style="font-size:x-small );

daParent.Fill(ds);
daChild.Fill(ds);
<font size="2

</font>

<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small DataRelation<span style="font-size:x-small relation =
<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small new<span style="font-size:x-small
<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small DataRelation<span style="font-size:x-small (<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "FK_Ord_d"<span style="font-size:x-small ,
ds.Tables[<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "ord_h"<span style="font-size:x-small ].Columns[<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "ordnum"<span style="font-size:x-small ],<font size="2

ds.Tables[
</font>

<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "ord_d"<span style="font-size:x-small ].Columns[<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "ordnum"<span style="font-size:x-small ]);

ds.Relations.Add(relation);
<font size="2
bsParent =
</font>

<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small new<span style="font-size:x-small
<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small BindingSource<span style="font-size:x-small (ds,
<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "ord_h"<span style="font-size:x-small );<font size="2

bsChild =
</font>

<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small new<span style="font-size:x-small
<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small BindingSource<span style="font-size:x-small (bsParent,
<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "FK_Ord_d"<span style="font-size:x-small );

dgvOrd_h.DataSource = bsParent;
dgvOrd_d.DataSource = bsChild;
-------------------------------
<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small <font size="2" color="#0000ff <font size="2" color="#0000ff
private
</font></font><font size="2" color="#0000ff

</font>
<span style="font-size:x-small <span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small void<span style="font-size:x-small SaveAll(<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small DataSet<span style="font-size:x-small
paraDs)
<span style="font-size:x-small <font size="2

</font>
<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small this<span style="font-size:x-small .Validate();

bsParent.EndEdit();
bsChild.EndEdit();
<font size="2

</font>

<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small try
<span style="font-size:x-small
{
conn.Open();
<font size="2
trans = conn.BeginTransaction(
</font>
<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small IsolationLevel<span style="font-size:x-small .RepeatableRead);<font size="2


</font>

<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small if<span style="font-size:x-small (paraDs.HasChanges())

{

<font size="2

</font>

<span style="color:#008000; font-size:x-small <span style="color:#008000; font-size:x-small //added new records
<span style="font-size:x-small <font size="2

</font>
<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small DataSet<span style="font-size:x-small dsAdded =
<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small new<span style="font-size:x-small
<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small DataSet<span style="font-size:x-small ();<font size="2

dsAdded = paraDs.GetChanges(
</font>

<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small DataRowState<span style="font-size:x-small .Added);<font size="2


</font>

<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small if<span style="font-size:x-small (dsAdded !=
<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small null<span style="font-size:x-small )

{
//call method ChangeAdded, to execute insert command to database
// the problem is when added new record to detail, datarowstate on header become Added too, this is wrong.
ChangeAdded(dsAdded);
}

<font size="2

</font>

<span style="color:#008000; font-size:x-small <span style="color:#008000; font-size:x-small //Modify records
<span style="font-size:x-small <font size="2

</font>
<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small DataSet<span style="font-size:x-small dsModified =
<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small new<span style="font-size:x-small
<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small DataSet<span style="font-size:x-small ();<font size="2

dsModified = paraDs.GetChanges(
</font>

<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small DataRowState<span style="font-size:x-small .Modified);<font size="2


</font>

<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small if<span style="font-size:x-small (dsModified !=
<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small null<span style="font-size:x-small )

{
ChangeModified(dsModified);
}
trans.Commit();
ds.AcceptChanges();
}
<font size="2

</font>

<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small else
<span style="font-size:x-small
{
trans.Rollback();
<font size="2

</font>
<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small MessageBox<span style="font-size:x-small .Show(<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "Nothing
to be saved"<span style="font-size:x-small );
}
}
<font size="2

</font>

<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small catch<span style="font-size:x-small (<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small Exception<span style="font-size:x-small
ex)
{
bsChild.CancelEdit();
bsParent.CancelEdit();
trans.Rollback();
<font size="2

</font>

<span style="color:#2b91af; font-size:x-small <span style="color:#2b91af; font-size:x-small MessageBox<span style="font-size:x-small .Show(ex.Message);

}
<font size="2

</font>

<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small finally
<span style="font-size:x-small
{
conn.Close();
}




View the full article
 
Back
Top