A
Assimalyst
Hi,
I'm attempting to input data into two related tables using stored
procedures. I found some good example code, which i have followed,
checked and double checked, but for some reason the update is not
happening.
Below is the code, it's pretty lengthy so to save you time i'll say now
that it runs through fine in debug until near the very end, the
patientDataAdapter.Update(dsAddPatient, "Patient"); line just above the
'catch' section. It runs the previous line then jumps this and
subsequent lines going straight to 'catch'.
The code:
private void addPatientDataToDB()
{
try
{
// Create the DataSet object
DataSet dsAddPatient = new DataSet();
// Connect to database
SqlConnection conn = new
SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
conn.Open();
// Create the DataTable "Patient" in the Dataset and the
DataAdapter
SqlDataAdapter patientDataAdapter = new
SqlDataAdapter(new SqlCommand("SELECT * FROM tblPatient", conn));
patientDataAdapter.InsertCommand = new
SqlCommand("proc_InsertPatient", conn);
SqlCommand cmdInsert = patientDataAdapter.InsertCommand;
cmdInsert.CommandType = CommandType.StoredProcedure;
cmdInsert.Parameters.Add(new SqlParameter("@patientNo",
SqlDbType.Int));
cmdInsert.Parameters["@patientNo"].Direction =
ParameterDirection.Output;
cmdInsert.Parameters["@patientNo"].SourceColumn = "patientNo";
cmdInsert.Parameters.Add(new SqlParameter("@pntUnitID",
SqlDbType.NVarChar,15,"pntUnitID"));
cmdInsert.Parameters.Add(new SqlParameter("@pntTitle",
SqlDbType.NVarChar,4,"pntTitle"));
cmdInsert.Parameters.Add(new SqlParameter("@pntFName",
SqlDbType.NVarChar,20,"pntFName"));
cmdInsert.Parameters.Add(new SqlParameter("@pntLName",
SqlDbType.NVarChar,30,"pntLName"));
cmdInsert.Parameters.Add(new SqlParameter("@pntDOB",
SqlDbType.DateTime,8,"pntDOB"));
cmdInsert.Parameters.Add(new SqlParameter("@pntSex",
SqlDbType.NVarChar,1,"pntSex"));
cmdInsert.Parameters.Add(new SqlParameter("@pntAddress1",
SqlDbType.NVarChar,150,"pntAddress1"));
cmdInsert.Parameters.Add(new SqlParameter("@pntAddress2",
SqlDbType.NVarChar,150,"pntAddress2"));
cmdInsert.Parameters.Add(new SqlParameter("@pntAddress3",
SqlDbType.NVarChar,150,"pntAddress3"));
cmdInsert.Parameters.Add(new SqlParameter("@pntcountryNo",
SqlDbType.Int,4,"pntCountryNo"));
cmdInsert.Parameters.Add(new SqlParameter("@pntPostcode",
SqlDbType.NVarChar,10,"pntPostcode"));
cmdInsert.Parameters.Add(new SqlParameter("@pntHPnone",
SqlDbType.NVarChar,14,"pntHPnone"));
cmdInsert.Parameters.Add(new SqlParameter("@pntWPhone",
SqlDbType.NVarChar,14,"pntWPhone"));
cmdInsert.Parameters.Add(new SqlParameter("@pntMobPhone",
SqlDbType.NVarChar,14,"pntMobPhone"));
cmdInsert.Parameters.Add(new SqlParameter("@pntEmail",
SqlDbType.NVarChar,50,"pntEmail"));
cmdInsert.Parameters.Add(new SqlParameter("@pntStage",
SqlDbType.NVarChar,5,"pntStage"));
cmdInsert.Parameters.Add(new SqlParameter("@pntT",
SqlDbType.TinyInt,1,"pntT"));
cmdInsert.Parameters.Add(new SqlParameter("@pntN",
SqlDbType.TinyInt,1,"pntN"));
cmdInsert.Parameters.Add(new SqlParameter("@pntM",
SqlDbType.TinyInt,1,"pntM"));
cmdInsert.Parameters.Add(new SqlParameter("@pntPreviousTreatments",
SqlDbType.Char,1000,"pntPreviousTreatments"));
cmdInsert.Parameters.Add(new SqlParameter("@pntFurtherNotes",
SqlDbType.Char,1000,"pntFurtherNotes"));
patientDataAdapter.FillSchema(dsAddPatient, SchemaType.Source);
DataTable pTable = dsAddPatient.Tables["Table"];
pTable.TableName = "Patient";
// Create the DataTable "Histology" in the
// Dataset and the DataAdapter
SqlDataAdapter histologyDataAdapter = new
SqlDataAdapter(new SqlCommand("SELECT * FROM tblSample", conn));
histologyDataAdapter.InsertCommand = new
SqlCommand("proc_InsertHistology", conn);
cmdInsert = histologyDataAdapter.InsertCommand;
cmdInsert.CommandType = CommandType.StoredProcedure;
cmdInsert.Parameters.Add(new SqlParameter("@patientNo",
SqlDbType.Int,4));
cmdInsert.Parameters["@patientNo"].SourceColumn = "patientNo";
cmdInsert.Parameters.Add(new SqlParameter("@lesHistology",
SqlDbType.NVarChar,50));
cmdInsert.Parameters["@lesHistology"].SourceColumn =
"lesHistology";
cmdInsert.Parameters.Add(new SqlParameter("@lesNull",
SqlDbType.NVarChar,10));
cmdInsert.Parameters["@lesNull"].SourceColumn = "lesNull";
histologyDataAdapter.FillSchema(dsAddPatient, SchemaType.Source);
pTable = dsAddPatient.Tables["Table"];
pTable.TableName = "Histology";
// Create the relationship between the two tables
dsAddPatient.Relations.Add(new DataRelation("ParentChild",
dsAddPatient.Tables["Patient"].Columns["patientNo"],
dsAddPatient.Tables["Histology"].Columns["patientNo"]));
// Insert the Data
DataRow patientRow = dsAddPatient.Tables["Patient"].NewRow();
patientRow["pntUnitID"] = patientCodeTxtBx.Text;
patientRow["pntTitle"] = titleCboBx.SelectedValue;
patientRow["pntFName"] = fNameTxtBx.Text;
patientRow["pntLName"] = lNameTxtBx.Text;
patientRow["pntSex"] = sexCboBx.SelectedValue;
patientRow["pntDOB"] = DOBTxtBx.Text;
patientRow["pntAddress1"] = address1TxtBx.Text;
patientRow["pntAddress2"] = address2TxtBx.Text;
patientRow["pntAddress3"] = address3TxtBx.Text;
patientRow["pntcountryNo"] = countryCboBx.SelectedValue;
patientRow["pntPostcode"] = postcodeTxtBx.Text;
patientRow["pntHPhone"] = phoneTxtBx.Text;
patientRow["pntWPhone"] = workPhoneTxtBx.Text;
patientRow["pntMobPhone"] = mobileTxtBx.Text;
patientRow["pntEmail"] = emailTxtBx.Text;
patientRow["pntStage"] = stageCboBx.SelectedValue;
patientRow["pntT"] = tTxtBx.Text;
patientRow["pntN"] = nTxtBx.Text;
patientRow["pntM"] = mTxtBx.Text;
patientRow["pntPreviousTreatments"] = previousTreatmentsTxtBx.Text;
patientRow["pntFurtherNotes"] = notesTxtBx.Text;
dsAddPatient.Tables["Patient"].Rows.Add(patientRow);
DataRow histologyRow = dsAddPatient.Tables["Histology"].NewRow();
histologyRow["lesHistology"] = histologyCboBx.SelectedValue;
histologyRow.SetParentRow(patientRow);
dsAddPatient.Tables["Histology"].Rows.Add(histologyRow);
patientDataAdapter.Update(dsAddPatient, "Patient");
histologyDataAdapter.Update(dsAddPatient, "Histology");
messageLbl.Text = "Data successfully added to database";
}
catch (Exception ex)
{
messageLbl.Text = ex + "Unable to connect to the database";
}
finally
{
// Close the connection
DataAccess.DBConnection.CloseDBConnection();
}
}
Anyone with any ideas why it might be jumping the
patientDataAdapter.Update(dsAddPatient, "Patient");
and
histologyDataAdapter.Update(dsAddPatient, "Histology");
statements?
Thanks.
I'm attempting to input data into two related tables using stored
procedures. I found some good example code, which i have followed,
checked and double checked, but for some reason the update is not
happening.
Below is the code, it's pretty lengthy so to save you time i'll say now
that it runs through fine in debug until near the very end, the
patientDataAdapter.Update(dsAddPatient, "Patient"); line just above the
'catch' section. It runs the previous line then jumps this and
subsequent lines going straight to 'catch'.
The code:
private void addPatientDataToDB()
{
try
{
// Create the DataSet object
DataSet dsAddPatient = new DataSet();
// Connect to database
SqlConnection conn = new
SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
conn.Open();
// Create the DataTable "Patient" in the Dataset and the
DataAdapter
SqlDataAdapter patientDataAdapter = new
SqlDataAdapter(new SqlCommand("SELECT * FROM tblPatient", conn));
patientDataAdapter.InsertCommand = new
SqlCommand("proc_InsertPatient", conn);
SqlCommand cmdInsert = patientDataAdapter.InsertCommand;
cmdInsert.CommandType = CommandType.StoredProcedure;
cmdInsert.Parameters.Add(new SqlParameter("@patientNo",
SqlDbType.Int));
cmdInsert.Parameters["@patientNo"].Direction =
ParameterDirection.Output;
cmdInsert.Parameters["@patientNo"].SourceColumn = "patientNo";
cmdInsert.Parameters.Add(new SqlParameter("@pntUnitID",
SqlDbType.NVarChar,15,"pntUnitID"));
cmdInsert.Parameters.Add(new SqlParameter("@pntTitle",
SqlDbType.NVarChar,4,"pntTitle"));
cmdInsert.Parameters.Add(new SqlParameter("@pntFName",
SqlDbType.NVarChar,20,"pntFName"));
cmdInsert.Parameters.Add(new SqlParameter("@pntLName",
SqlDbType.NVarChar,30,"pntLName"));
cmdInsert.Parameters.Add(new SqlParameter("@pntDOB",
SqlDbType.DateTime,8,"pntDOB"));
cmdInsert.Parameters.Add(new SqlParameter("@pntSex",
SqlDbType.NVarChar,1,"pntSex"));
cmdInsert.Parameters.Add(new SqlParameter("@pntAddress1",
SqlDbType.NVarChar,150,"pntAddress1"));
cmdInsert.Parameters.Add(new SqlParameter("@pntAddress2",
SqlDbType.NVarChar,150,"pntAddress2"));
cmdInsert.Parameters.Add(new SqlParameter("@pntAddress3",
SqlDbType.NVarChar,150,"pntAddress3"));
cmdInsert.Parameters.Add(new SqlParameter("@pntcountryNo",
SqlDbType.Int,4,"pntCountryNo"));
cmdInsert.Parameters.Add(new SqlParameter("@pntPostcode",
SqlDbType.NVarChar,10,"pntPostcode"));
cmdInsert.Parameters.Add(new SqlParameter("@pntHPnone",
SqlDbType.NVarChar,14,"pntHPnone"));
cmdInsert.Parameters.Add(new SqlParameter("@pntWPhone",
SqlDbType.NVarChar,14,"pntWPhone"));
cmdInsert.Parameters.Add(new SqlParameter("@pntMobPhone",
SqlDbType.NVarChar,14,"pntMobPhone"));
cmdInsert.Parameters.Add(new SqlParameter("@pntEmail",
SqlDbType.NVarChar,50,"pntEmail"));
cmdInsert.Parameters.Add(new SqlParameter("@pntStage",
SqlDbType.NVarChar,5,"pntStage"));
cmdInsert.Parameters.Add(new SqlParameter("@pntT",
SqlDbType.TinyInt,1,"pntT"));
cmdInsert.Parameters.Add(new SqlParameter("@pntN",
SqlDbType.TinyInt,1,"pntN"));
cmdInsert.Parameters.Add(new SqlParameter("@pntM",
SqlDbType.TinyInt,1,"pntM"));
cmdInsert.Parameters.Add(new SqlParameter("@pntPreviousTreatments",
SqlDbType.Char,1000,"pntPreviousTreatments"));
cmdInsert.Parameters.Add(new SqlParameter("@pntFurtherNotes",
SqlDbType.Char,1000,"pntFurtherNotes"));
patientDataAdapter.FillSchema(dsAddPatient, SchemaType.Source);
DataTable pTable = dsAddPatient.Tables["Table"];
pTable.TableName = "Patient";
// Create the DataTable "Histology" in the
// Dataset and the DataAdapter
SqlDataAdapter histologyDataAdapter = new
SqlDataAdapter(new SqlCommand("SELECT * FROM tblSample", conn));
histologyDataAdapter.InsertCommand = new
SqlCommand("proc_InsertHistology", conn);
cmdInsert = histologyDataAdapter.InsertCommand;
cmdInsert.CommandType = CommandType.StoredProcedure;
cmdInsert.Parameters.Add(new SqlParameter("@patientNo",
SqlDbType.Int,4));
cmdInsert.Parameters["@patientNo"].SourceColumn = "patientNo";
cmdInsert.Parameters.Add(new SqlParameter("@lesHistology",
SqlDbType.NVarChar,50));
cmdInsert.Parameters["@lesHistology"].SourceColumn =
"lesHistology";
cmdInsert.Parameters.Add(new SqlParameter("@lesNull",
SqlDbType.NVarChar,10));
cmdInsert.Parameters["@lesNull"].SourceColumn = "lesNull";
histologyDataAdapter.FillSchema(dsAddPatient, SchemaType.Source);
pTable = dsAddPatient.Tables["Table"];
pTable.TableName = "Histology";
// Create the relationship between the two tables
dsAddPatient.Relations.Add(new DataRelation("ParentChild",
dsAddPatient.Tables["Patient"].Columns["patientNo"],
dsAddPatient.Tables["Histology"].Columns["patientNo"]));
// Insert the Data
DataRow patientRow = dsAddPatient.Tables["Patient"].NewRow();
patientRow["pntUnitID"] = patientCodeTxtBx.Text;
patientRow["pntTitle"] = titleCboBx.SelectedValue;
patientRow["pntFName"] = fNameTxtBx.Text;
patientRow["pntLName"] = lNameTxtBx.Text;
patientRow["pntSex"] = sexCboBx.SelectedValue;
patientRow["pntDOB"] = DOBTxtBx.Text;
patientRow["pntAddress1"] = address1TxtBx.Text;
patientRow["pntAddress2"] = address2TxtBx.Text;
patientRow["pntAddress3"] = address3TxtBx.Text;
patientRow["pntcountryNo"] = countryCboBx.SelectedValue;
patientRow["pntPostcode"] = postcodeTxtBx.Text;
patientRow["pntHPhone"] = phoneTxtBx.Text;
patientRow["pntWPhone"] = workPhoneTxtBx.Text;
patientRow["pntMobPhone"] = mobileTxtBx.Text;
patientRow["pntEmail"] = emailTxtBx.Text;
patientRow["pntStage"] = stageCboBx.SelectedValue;
patientRow["pntT"] = tTxtBx.Text;
patientRow["pntN"] = nTxtBx.Text;
patientRow["pntM"] = mTxtBx.Text;
patientRow["pntPreviousTreatments"] = previousTreatmentsTxtBx.Text;
patientRow["pntFurtherNotes"] = notesTxtBx.Text;
dsAddPatient.Tables["Patient"].Rows.Add(patientRow);
DataRow histologyRow = dsAddPatient.Tables["Histology"].NewRow();
histologyRow["lesHistology"] = histologyCboBx.SelectedValue;
histologyRow.SetParentRow(patientRow);
dsAddPatient.Tables["Histology"].Rows.Add(histologyRow);
patientDataAdapter.Update(dsAddPatient, "Patient");
histologyDataAdapter.Update(dsAddPatient, "Histology");
messageLbl.Text = "Data successfully added to database";
}
catch (Exception ex)
{
messageLbl.Text = ex + "Unable to connect to the database";
}
finally
{
// Close the connection
DataAccess.DBConnection.CloseDBConnection();
}
}
Anyone with any ideas why it might be jumping the
patientDataAdapter.Update(dsAddPatient, "Patient");
and
histologyDataAdapter.Update(dsAddPatient, "Histology");
statements?
Thanks.