Re: Getting Errors with Transaction

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I see now the error is due to the fact that ExecScalar() returns null. I found several references that show that is the way to return the SCOPE_IDENTITY() of an inserted row. Guess I'll keep researching that one.

Jonathan

"Jonathan Wood" <jwood@xxxxxxxxxxxxxxxx> wrote in message news:%23mQWXid$IHA.1016@xxxxxxxxxxxxxxxxxxxxxxx
I wrote code to insert a record into a table. It worked fine. Then I added a transaction using code essentially like this:

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DTSConnectionString"].ToString()))
{
conn.Open();
trans = conn.BeginTransaction();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO Contracts (Title, Description, StartDate, EndDate) VALUES (@Title, @Description, @StartDate, @EndDate)";
cmd.Parameters.Add(new SqlParameter("@Title", txtTitle.Text));
cmd.Parameters.Add(new SqlParameter("@Description", txtDescription.Text));
cmd.Parameters.Add(new SqlParameter("@StartDate", start));
cmd.Parameters.Add(new SqlParameter("@EndDate", end));
int contractId = (int)cmd.ExecuteScalar();
}

With this code, the call to ExecuteScaler() gives me the error "ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized."

So, I tried adding the following line just before the call to ExecuteScaler():

cmd.Transaction = trans;

Now, the same call gives me the error "Object reference not set to an instance of an object."

I'm still a little new to this and just couldn't figure out exactly what I'm supposed to be doing here. I'd appreciate any help on this.

Thanks.

Jonathan


.



Relevant Pages

  • Re: Getting Errors with Transaction
    ... If you use a data adapter and you change your sql to: ... found several references that show that is the way to return the ... "ExecuteScalar requires the command to have a transaction when the ... connection assigned to the command is in a pending local transaction. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Getting Errors with Transaction
    ... the call to ExecuteScaler() gives me the error "ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. ... the same call gives me the error "Object reference not set to an instance of an object." ...
    (microsoft.public.dotnet.framework.adonet)
  • Getting Errors with Transaction
    ... the call to ExecuteScaler() gives me the error "ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. ... the same call gives me the error "Object reference not set to an instance of an object." ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Distribution Agent fails with error: Line 1: Incorrect syntax near ,null,
    ... You mentioned manipulating the command in the msrepl_commands table. ... transaction capable: 2 ... Last transaction timestamp: 0x000004810000069f000900000000 ... Transaction seqno: 0x00000481000006e70001 ...
    (microsoft.public.sqlserver.replication)
  • Re: Asynchronous Stored Procedure Never Returns - Help?
    ... private void HandleCallback ... SqlCommand command = result.AsyncState; ... the transaction completes I get back to the point just before I clicked "OK" ... par = cm.CreateParameter; ...
    (microsoft.public.dotnet.languages.csharp)