The stored procedure cannot be executed. Can anyone please advise me and point out my stupid mistake?
The error message that I got is
Invalid operation. The connection is closed
Code:
public void Update(RepliesBAL RPBAL)
{
using (SqlConnection connection = new SqlConnection(@"Data Source=19NNZP;Initial Catalog=ivr;Persist Security Info=True;User ID=sa;Password=sa"))
{
SqlCommand command = new SqlCommand ();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.fax_UpdateFaxReply";
command.Parameters.Add("@uid", SqlDbType.VarChar, 50).Value = RPBAL.UlyssesID ;
SqlTransaction transaction;
transaction = connection.BeginTransaction("SampleTransaction");
command.Connection = connection;
command.Transaction = transaction;
try
{
connection.Open();
command.ExecuteNonQuery();
Console.WriteLine("OK");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
throw new Exception(ex.Message);
}
}
}
}
In order to call
.BeginTransaction(), your connection needs to be opened already – so change your code to:After that change, hopefully, this code should work just fine.