Hi,
I m getting this error when I m running update & insert query in transaction. while inserting,in middle of transaction throw this error and few records insert in table. while update,getting this error every time and records not updating. please help me if any one have solution for this problem.
Here is the code:
if(con!=null)
{
try{
cmd = new SqlCommand();
cmd.Connection = con;
trans = con.BeginTransaction();
msg="Begin Transaction";
cmd.Transaction = trans;
for(int i = 0; i<arry.Count ; i++)
{
try
{
cmd.CommandText = arry[i].ToString() + "";
//if(trans!=null && con!=null)
re += cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
re = -1;
trans.Rollback();
throw new Exception("Catched ExecuteNonQuery exception : " + ex.Message+"Message:"+msg);
}
}
try
{
msg = "Commit";
trans.Commit();
}
catch (Exception ex)
{
re = -1;
trans.Rollback();
throw new Exception("Catched Transaction commit exception : " + ex.Message + "Message:" + msg);
}
}
catch (SqlException sqlexception)
{
trans.Rollback();
re = -1;
throw new Exception("SQLBatch.executeBatch :" + sqlexception.Message);
}
catch (Exception ex)
{
trans.Rollback();
re = -1;
throw new Exception("SQLBatch.executeBatch : " + ex.Message);
}
finally
{
try
{
con.Close();
con = null;
trans.Dispose();
trans = null;
}
catch (Exception ex)
{
re = -1;
throw new Exception("Catched an exception when trying to rollback : " + ex.Message);
}
}
}
}
catch (Exception ex)
{
re = -1;
throw new Exception("Catched exception : " + ex.Message+ "Message: " +msg);
}
}