There are many possible solution to your answer. First, I would suggest that you create a TRIGGER
on every insert on table CarTab
.
Second, why not change the CommandText
of your command object and call ExecuteNonQuery()
again?
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString))
{
string QueryA = "Insert into CarTab(Manufacture,Model,Plate,Color,Year,Service,Roadtax) Values (@manufactures,@models,@plates,@colors,@years,@services,@roadtaxs";
string QueryB = "Insert into TempTab(Manufacture,Model,Plate,Color,Year,Service,Roadtax) Values (@manufactures,@models,@plates,@colors,@years,@services,@roadtaxs";
using (SqlCommand cmd = new SqlCommand("", conn))
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = QueryA;
cmd.Parameters.AddWithValue("@manufactures", Label1.Text);
cmd.Parameters.AddWithValue("@models", Label2.Text);
cmd.Parameters.AddWithValue("@plates", Label3.Text);
cmd.Parameters.AddWithValue("@colors", Label4.Text);
cmd.Parameters.AddWithValue("@years", Label5.Text);
cmd.Parameters.AddWithValue("@services", Label6.Text);
cmd.Parameters.AddWithValue("@roadtaxs", Label7.Text);
try
{
conn.Open()
cmd.ExecuteNonQuery();
cmd.CommandText = QueryB;
cmd.ExecuteNonQuery();
}
catch (SqlException e)
{
// put error here
}
finally
{
conn.Close();
}
}
}
and Lastly create a Stored Procedure
CREATE PROCEDURE InsertRecord
@manufactures varchar(50),
@models varchar(50),
@plates varchar(50),
@colors varchar(50),
@years varchar(50),
@services varchar(50),
@roadtaxs varchar(50)
AS
BEGIN
Insert into CarTab(Manufacture,Model,Plate,Color,Year,Service,Roadtax)
Values (@manufactures,@models,@plates,@colors,@years,@services,@roadtaxs)
Insert into CarTab(Manufacture,Model,Plate,Color,Year,Service,Roadtax)
Values (@manufactures,@models,@plates,@colors,@years,@services,@roadtaxs)
END
GO
and in your C# code,
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("InsertRecord", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = QueryA;
cmd.Parameters.AddWithValue("@manufactures", Label1.Text);
cmd.Parameters.AddWithValue("@models", Label2.Text);
cmd.Parameters.AddWithValue("@plates", Label3.Text);
cmd.Parameters.AddWithValue("@colors", Label4.Text);
cmd.Parameters.AddWithValue("@years", Label5.Text);
cmd.Parameters.AddWithValue("@services", Label6.Text);
cmd.Parameters.AddWithValue("@roadtaxs", Label7.Text);
try
{
conn.Open()
cmd.ExecuteNonQuery();
}
catch (SqlException e)
{
// put error here
}
finally
{
conn.Close();
}
}
}