I'm attempting to update multiple tables using an SQL transaction. Right now, the transaction is only affecting one of the tables (dbo.Colors
). However, the code is pretty much the same for all three, so I'm wondering where I'm running into an issue. The key difference between the tables is that one is getting information from asp:Textbox
(es) while the other two are getting their data from drop down lists. They are all related through a Session variable named "PlanID". Here is my code:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConversionConnect2"].ConnectionString);
string query = "UPDATE dbo.Colors SET HeaderBackground = @HeaderBackground, HeaderText = @HeaderText, FooterBackground = @FooterBackground, FooterText = @FooterText, ButtonText = @ButtonText, ButtonHover = @ButtonHover WHERE PlanID = @PlanID";
string query2 = "UPDATE dbo.Fonts SET HeaderFont = @HeaderFont, FooterFont = @FooterFont, ButtonFont = @ButtonFont WHERE PlanID = @PlanID";
string query3 = "UPDATE dbo.Sizes SET HeaderSize = @HeaderSize, FooterSize = @FooterSize, ButtonSize = @ButtonSize WHERE PlanID = @PlanID";
using (conn)
{
SqlTransaction trans = null;
try
{
conn.Open();
trans = conn.BeginTransaction();
using (SqlCommand transCom = new SqlCommand(query, conn, trans))
{
transCom.Parameters.AddWithValue("@PlanID", Session["planid"].ToString());
transCom.Parameters.AddWithValue("@HeaderBackground", txtheadercolor.Text);
transCom.Parameters.AddWithValue("@HeaderText", headertext.Text);
transCom.Parameters.AddWithValue("@FooterBackground", txtfootercolor.Text);
transCom.Parameters.AddWithValue("@FooterText", footertext.Text);
transCom.Parameters.AddWithValue("@ButtonText", txtbuttoncolor.Text);
transCom.Parameters.AddWithValue("@ButtonHover", txthovercolor.Text);
transCom.ExecuteNonQuery();
}
using (SqlCommand transCom2 = new SqlCommand(query2, conn, trans))
{
transCom2.Parameters.AddWithValue("@PlanID", Session["planid"].ToString());
transCom2.Parameters.AddWithValue("@HeaderFont", headerfont.SelectedValue.ToString());
transCom2.Parameters.AddWithValue("@FooterFont", footerfont.SelectedValue.ToString());
transCom2.Parameters.AddWithValue("@ButtonFont", ddButtonFont.SelectedValue.ToString());
transCom2.ExecuteNonQuery();
}
using (SqlCommand transCom3 = new SqlCommand(query3, conn, trans))
{
transCom3.Parameters.AddWithValue("@PlanID", Session["planid"].ToString());
transCom3.Parameters.AddWithValue("@HeaderSize", ddheadersize.SelectedValue);
transCom3.Parameters.AddWithValue("@FooterSize", ddfootersize.SelectedValue);
transCom3.Parameters.AddWithValue("@ButtonSize", ddButtonSize.SelectedValue);
transCom3.ExecuteNonQuery();
}
trans.Commit();
}
catch (Exception Ex)
{
if (trans != null)
{
trans.Rollback();
}
else
{
return;
}
}
conn.Close();
}
This code runs without errors, however, when I check dbo.Fonts and dbo.Sizes, I see that both tables were not updated. Any suggestions?