G
geeksgk
All,
I broke my head over this for 18 hours now and tried out all the
suggestions posted here but of no use.
Here is what I'm trying to accomplish
I have two tables, 1) Criteria 2) Regulator
Criteria is a master table and Regulator is a child table. ie, the
primary key of Criteria table (CriteriaID, identity column with
autoincrement set to 1) is a foreign key in the Regulator table. pretty
classic stuff.
I wrote a console app in C# to update these two tables using
SqlDataAdapter.Update method. The only slight difference from other
postings is that I get the XML data file from an external application.
I have to import the data from that XML file into these two tables.
I tried to follow David Sceppa's suggestions but No matter what I do,
the identity value I receive from the Criteria table DOES NOT GET
CASCADED to the Regulator table. As a result records are not inserted
into the child (Regulator) table.
Here is my code
Any thoughts would be much appreciated
{
.......
SelectQuery = "select * from Criteria";
SqlDataAdapter CriteriaDataAdapter = new SqlDataAdapter();
SqlConnection CriteriaSQLConnection = new
SqlConnection(ConnectionString);
SqlCommand CriteriaSQLSelectCommand = new SqlCommand(SelectQuery,
CriteriaSQLConnection);
CriteriaDataAdapter.SelectCommand = CriteriaSQLSelectCommand;
cmdGetIdentity = new SqlCommand("SELECT @@IDENTITY as CriteriaID",
CriteriaDataAdapter.SelectCommand.Connection);
CriteriaDataAdapter.RowUpdated +=new
SqlRowUpdatedEventHandler(CriteriaDataAdapter_RowUpdated);
SqlCommandBuilder Criteriacmdb = new
SqlCommandBuilder(CriteriaDataAdapter);
SelectQuery = "select * from Regulator";
SqlDataAdapter RegulatorDataAdapter = new SqlDataAdapter();
//attempted to use the same connection object..didn't help
//SqlConnection RegulatorSQLConnection = new
SqlConnection(ConnectionString);
//SqlCommand RegulatorSQLSelectCommand = new SqlCommand(SelectQuery,
RegulatorSQLConnection);
SqlCommand RegulatorSQLSelectCommand = new SqlCommand(SelectQuery,
CriteriaSQLConnection);
RegulatorDataAdapter.SelectCommand = RegulatorSQLSelectCommand;
SqlCommandBuilder Regulatorcmdb = new
SqlCommandBuilder(RegulatorDataAdapter);
DataSet NewDataSet = new DataSet();
NewDataSet.ReadXmlSchema("PostApprovalsReport.xsd");
DataColumn NewColumn =
NewDataSet.Tables["Criteria"].Columns.Add("CriteriaID", typeof(Int32));
NewColumn.Unique = true;
NewColumn.AutoIncrement = true;
NewColumn.AutoIncrementSeed = -1;
NewColumn.AutoIncrementStep = -1;
DataColumn NewFColumn =
NewDataSet.Tables["Regulator"].Columns.Add("CriteriaID",
typeof(Int32));
NewDataSet.Relations.Add("CriReg",
NewDataSet.Tables["Criteria"].Columns["CriteriaID"],
NewDataSet.Tables["Regulator"].Columns["CriteriaID"]);
Reading the data from a xml file that came from another application
NewDataSet.ReadXml("5393_PostApprovalsReport_2004-12-01.xml");
CriteriaDataAdapter.Update(NewDataSet, "Criteria");
RegulatorDataAdapter.Update(NewDataSet, "Regulator");
}
private void CriteriaDataAdapter_RowUpdated(object sender,
SqlRowUpdatedEventArgs e)
{
if ((e.Status == UpdateStatus.Continue) && ((e.StatementType ==
StatementType.Insert)))
{
Console.WriteLine (cmdGetIdentity.ExecuteScalar().ToString());
e.Row["CriteriaID"] =
Int32.Parse(cmdGetIdentity.ExecuteScalar().ToString());
e.Row.AcceptChanges();
Console.WriteLine (e.Row["CriteriaID"]);
}
}
I broke my head over this for 18 hours now and tried out all the
suggestions posted here but of no use.
Here is what I'm trying to accomplish
I have two tables, 1) Criteria 2) Regulator
Criteria is a master table and Regulator is a child table. ie, the
primary key of Criteria table (CriteriaID, identity column with
autoincrement set to 1) is a foreign key in the Regulator table. pretty
classic stuff.
I wrote a console app in C# to update these two tables using
SqlDataAdapter.Update method. The only slight difference from other
postings is that I get the XML data file from an external application.
I have to import the data from that XML file into these two tables.
I tried to follow David Sceppa's suggestions but No matter what I do,
the identity value I receive from the Criteria table DOES NOT GET
CASCADED to the Regulator table. As a result records are not inserted
into the child (Regulator) table.
Here is my code
Any thoughts would be much appreciated
{
.......
SelectQuery = "select * from Criteria";
SqlDataAdapter CriteriaDataAdapter = new SqlDataAdapter();
SqlConnection CriteriaSQLConnection = new
SqlConnection(ConnectionString);
SqlCommand CriteriaSQLSelectCommand = new SqlCommand(SelectQuery,
CriteriaSQLConnection);
CriteriaDataAdapter.SelectCommand = CriteriaSQLSelectCommand;
cmdGetIdentity = new SqlCommand("SELECT @@IDENTITY as CriteriaID",
CriteriaDataAdapter.SelectCommand.Connection);
CriteriaDataAdapter.RowUpdated +=new
SqlRowUpdatedEventHandler(CriteriaDataAdapter_RowUpdated);
SqlCommandBuilder Criteriacmdb = new
SqlCommandBuilder(CriteriaDataAdapter);
SelectQuery = "select * from Regulator";
SqlDataAdapter RegulatorDataAdapter = new SqlDataAdapter();
//attempted to use the same connection object..didn't help
//SqlConnection RegulatorSQLConnection = new
SqlConnection(ConnectionString);
//SqlCommand RegulatorSQLSelectCommand = new SqlCommand(SelectQuery,
RegulatorSQLConnection);
SqlCommand RegulatorSQLSelectCommand = new SqlCommand(SelectQuery,
CriteriaSQLConnection);
RegulatorDataAdapter.SelectCommand = RegulatorSQLSelectCommand;
SqlCommandBuilder Regulatorcmdb = new
SqlCommandBuilder(RegulatorDataAdapter);
DataSet NewDataSet = new DataSet();
NewDataSet.ReadXmlSchema("PostApprovalsReport.xsd");
DataColumn NewColumn =
NewDataSet.Tables["Criteria"].Columns.Add("CriteriaID", typeof(Int32));
NewColumn.Unique = true;
NewColumn.AutoIncrement = true;
NewColumn.AutoIncrementSeed = -1;
NewColumn.AutoIncrementStep = -1;
DataColumn NewFColumn =
NewDataSet.Tables["Regulator"].Columns.Add("CriteriaID",
typeof(Int32));
NewDataSet.Relations.Add("CriReg",
NewDataSet.Tables["Criteria"].Columns["CriteriaID"],
NewDataSet.Tables["Regulator"].Columns["CriteriaID"]);
Reading the data from a xml file that came from another application
NewDataSet.ReadXml("5393_PostApprovalsReport_2004-12-01.xml");
CriteriaDataAdapter.Update(NewDataSet, "Criteria");
RegulatorDataAdapter.Update(NewDataSet, "Regulator");
}
private void CriteriaDataAdapter_RowUpdated(object sender,
SqlRowUpdatedEventArgs e)
{
if ((e.Status == UpdateStatus.Continue) && ((e.StatementType ==
StatementType.Insert)))
{
Console.WriteLine (cmdGetIdentity.ExecuteScalar().ToString());
e.Row["CriteriaID"] =
Int32.Parse(cmdGetIdentity.ExecuteScalar().ToString());
e.Row.AcceptChanges();
Console.WriteLine (e.Row["CriteriaID"]);
}
}