I am using that sqltransaction for the insert multiple tables each data.
But I have problem that have the database have the two same data.
What should I do for solve that problem?
please help me? Thanx
SqlConnection baglanti = system.baglan();
SqlCommand Trislem1_Ekle = new SqlCommand("Insert tblTr (Ad,TipID,BolgeID,Yerler,Resim) values(@Ad,@TipID,@BolgeID,@Yerler,@Resim) SELECT SCOPE_IDENTITY()", baglanti);
SqlCommand Tr2_TrAciklama = new SqlCommand("Insert tblTrAciklamaDetay (TrID,TrProgram) values((SELECT IDENT_CURRENT('tblTr')),@TrProgram)", baglanti);
Trislem1_Ekle.Parameters.AddWithValue("@Ad", txtTrAd.Text);
Trislem1_Ekle.Parameters.AddWithValue("@TipID", dlTrTip.SelectedValue);
Trislem1_Ekle.Parameters.AddWithValue("@BolgeID", BolgeID.SelectedValue);
Trislem1_Ekle.Parameters.AddWithValue("@Yerler", Yerler.Text);
Trislem1_Ekle.Parameters.AddWithValue("@Resim", Resim.SelectedValue);
Tr2_TrAciklama.Parameters.AddWithValue("@TrProgram", TrProgram.Text);
SqlTransaction sqlTrans = baglanti.BeginTransaction();
Trislem1_Ekle.Transaction = sqlTrans;
Tr2_TrAciklama.Transaction = sqlTrans;
try
{
Trislem1_Ekle.ExecuteNonQuery();
Tr2_TrAciklama.ExecuteNonQuery();
string SonIDGelen = Trislem1_Ekle.ExecuteScalar().ToString();
sqlTrans.Commit();
}
catch (Exception hata)
{
Response.Write("İşleminiz yapılamadı, Oluşan Hatanın Detayı<br />" + hata);
sqlTrans.Rollback();
}
finally
{
baglanti.Close();
baglanti.Dispose();
Trislem1_Ekle.Dispose();
Tr2_TrAciklama.Dispose();
}
As far as I see, you executing your
Trislem1_Ekle
command twice.One with
and the other one with;
Deleting the first one seems enough. Both
ExecuteNonQuery
andExecuteScalar
executes your query, andExecuteScalar
returns first column of the first row additionally.Instead of disposing your database connections and commands manually, use
using
statement instead.And don't use
AddWithValue
method. It may generate some unexptected results. Use.Add()
method and overloads instead.AddWithValue()
already?