Transaction Problem

  • Thread starter Thread starter Ramesh
  • Start date Start date
R

Ramesh

hi
I am trying to insert random values to the tables.
I will get no.of records value from user, based on that
loop will start to insert records. For this insertion i am
using stored procedure. I am using Transaction for each
insert statement. But when i try to insert it is
displaying "use of unassigned local
variable 'InsertTrans'. Can anybody help me.
My code is as follows.

SqlTransaction InsertTrans;
DateTime Start = DateTime.Now;
DateTime End;
Random iDepartno = new Random();
Random iJobCode = new Random();
Random iExperience = new Random();

EmployeeCommand.Connection = EmployeeConnection;

EmployeeCommand.CommandType = CommandType.StoredProcedure;
EmployeeCommand.CommandText = "InsertEmployeeDtl";
EmployeeCommand.Transaction = InsertTrans;


SqlParameter FName,LName,Datej,DB,Dptno,Grade, JCode ;

FName = new SqlParameter("@FName", SqlDbType.NVarChar, 50);
FName.Direction = ParameterDirection.Input;

LName = new SqlParameter("@LName", SqlDbType.NVarChar, 50);
LName.Direction = ParameterDirection.Input;
..
..
..
EmployeeCommand.Parameters.Add(FName);
EmployeeCommand.Parameters.Add(LName);
..
..
..
for (int iInsertRecord = 0; iInsertRecord !=
System.Int32.Parse(txtInsert.Text); iInsertRecord++)
{
try
{
EmployeeCommand.Parameters["@FName"].Value
= "Firstname" + iInsertRecord;
EmployeeCommand.Parameters["@LName"].Value = "Lastname" +
iInsertRecord;
...
...

EmployeeConnection.Open();
InsertTrans= EmployeeConnection.BeginTransaction
(IsolationLevel.ReadCommitted);

EmployeeCommand.ExecuteNonQuery();
InsertTrans.Commit();
}
catch (SqlException sqlerr)
{
InsertTrans.Rollback();

EmployeeConnection.Close();

EmployeeConnection.Dispose();
Response.Write (sqlerr.ToString());

}
finally
{
EmployeeConnection.Close();
}


Thanks,
Ramesh
 
hi,
When i try to compile it is displaying "use of
unassigned local variable 'InsertTrans' (not in the
execution time). Can anybody help me.

Thanks,
Ramesh
 
Ramesh said:
I am trying to insert random values to the tables.
I will get no.of records value from user, based on that
loop will start to insert records. For this insertion i am
using stored procedure. I am using Transaction for each
insert statement. But when i try to insert it is
displaying "use of unassigned local
variable 'InsertTrans'. Can anybody help me.

Well it's right - you've got a local variable called InsertTrans, and
you're trying to read it in the line
EmployeeCommand.Transaction=InsertTrans;
without assigning to it first. You're not allowed to do that. Now, what
did you want it to actually do?
 
Ramesh said:
I am using stored procedure for insertion, I don't know
how to initialise. I am trying to create transaction for
each insertion statment.

In that case I suspect you should use SqlConnection.BeginTransaction.
To be honest, I don't have much experience in this area, but that's
what I *think* you should be doing.
 
Hi Ramesh,

I think the problem should be with the line:

EmployeeCommand.Transaction = InsertTrans;

Before this line, you only define " InsertTrans", not set it to a real
instance

You may call following line before it:

EmployeeConnection.Open();
InsertTrans= EmployeeConnection.BeginTransaction

And here this KB article you may refer to:

HOW TO: Roll Back Updates After an Error When You Use DataAdapter and
DataSet in ADO.NET and Visual C# .NET
http://support.microsoft.com/default.aspx?scid=KB;EN-US;316024

Hope this help.

Luke

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top