Save in related table ...
-
Hi there ! I have two table (mainTable & SubTable) with these fileds : mainTable --> IdMetal , NameMetal , Formula , ... SubTable --> IDDetails,IdMetal , Company , ... also mainTable.IdMetal=SubTable.IdMetal in myForm , I have some TextBox that filled by MainTable Fields and Datagrid that filled by subTable Fields when I want to create New row(Save) in main table , at the same time i want to fill and save in datagrid , here is my code :
try
{
// for MainTable ...
OleDbConnection cnnSave=new OleDbConnection(myConnection);
OleDbCommand cmd__AddRows = new OleDbCommand();
cmd__AddRows.CommandText =
@"INSERT INTO MainRegTable (IDMetal,NameMetal,FormulMetal, GroupMetal, CodeMetal,Comments)
VALUES (@p1,@p2,@p3,@p4,@p5,@p6)";
cmd__AddRows.Parameters.Clear();
cmd__AddRows.Parameters.AddWithValue("@p1", txtID.Text);
....
//for subTable ...
OleDbDataAdapter dagrd = new OleDbDataAdapter(@"Select * From SubTblMetal
WHERE IDMetal=
"+int.Parse(txtID.Text),
cnnSave);
DataSet dsGrd = new DataSet();
dagrd.Fill(dsGrd, "SubTblMetal");
dsGrd.GetChanges();
dagrd.Update(dsGrd);
dsGrd.AcceptChanges();but , no changes in subtable ... thanks for Reply
power is knowledge ...
-
Hi there ! I have two table (mainTable & SubTable) with these fileds : mainTable --> IdMetal , NameMetal , Formula , ... SubTable --> IDDetails,IdMetal , Company , ... also mainTable.IdMetal=SubTable.IdMetal in myForm , I have some TextBox that filled by MainTable Fields and Datagrid that filled by subTable Fields when I want to create New row(Save) in main table , at the same time i want to fill and save in datagrid , here is my code :
try
{
// for MainTable ...
OleDbConnection cnnSave=new OleDbConnection(myConnection);
OleDbCommand cmd__AddRows = new OleDbCommand();
cmd__AddRows.CommandText =
@"INSERT INTO MainRegTable (IDMetal,NameMetal,FormulMetal, GroupMetal, CodeMetal,Comments)
VALUES (@p1,@p2,@p3,@p4,@p5,@p6)";
cmd__AddRows.Parameters.Clear();
cmd__AddRows.Parameters.AddWithValue("@p1", txtID.Text);
....
//for subTable ...
OleDbDataAdapter dagrd = new OleDbDataAdapter(@"Select * From SubTblMetal
WHERE IDMetal=
"+int.Parse(txtID.Text),
cnnSave);
DataSet dsGrd = new DataSet();
dagrd.Fill(dsGrd, "SubTblMetal");
dsGrd.GetChanges();
dagrd.Update(dsGrd);
dsGrd.AcceptChanges();but , no changes in subtable ... thanks for Reply
power is knowledge ...
Well...you haven't inserted anything into the sub table. So I'm not surprised that it contains no records. If you want to do this, I would recommend that your either use a transaction round both assignments, or better create a stored procedure to create a transaction, and do both inserts - this has the advantage of keeping it all within the DB. But you are going to need two inserts whichever way you go.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
-
Well...you haven't inserted anything into the sub table. So I'm not surprised that it contains no records. If you want to do this, I would recommend that your either use a transaction round both assignments, or better create a stored procedure to create a transaction, and do both inserts - this has the advantage of keeping it all within the DB. But you are going to need two inserts whichever way you go.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
Yes , but I want that the User have an option to Fill datagrid(subTable) or not , so it can be Blank ... thanks