Transaction Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ramesh

    Transaction Problem

    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 = EmployeeConnect ion;

    EmployeeCommand .CommandType = CommandType.Sto redProcedure;
    EmployeeCommand .CommandText = "InsertEmployee Dtl";
    EmployeeCommand .Transaction = InsertTrans;


    SqlParameter FName,LName,Dat ej,DB,Dptno,Gra de, JCode ;

    FName = new SqlParameter("@ FName", SqlDbType.NVarC har, 50);
    FName.Direction = ParameterDirect ion.Input;

    LName = new SqlParameter("@ LName", SqlDbType.NVarC har, 50);
    LName.Direction = ParameterDirect ion.Input;
    ..
    ..
    ..
    EmployeeCommand .Parameters.Add (FName);
    EmployeeCommand .Parameters.Add (LName);
    ..
    ..
    ..
    for (int iInsertRecord = 0; iInsertRecord !=
    System.Int32.Pa rse(txtInsert.T ext); iInsertRecord++ )
    {
    try
    {
    EmployeeCommand .Parameters["@FName"].Value
    = "Firstname" + iInsertRecord;
    EmployeeCommand .Parameters["@LName"].Value = "Lastname" +
    iInsertRecord;
    ...
    ...

    EmployeeConnect ion.Open();
    InsertTrans= EmployeeConnect ion.BeginTransa ction
    (IsolationLevel .ReadCommitted) ;

    EmployeeCommand .ExecuteNonQuer y();
    InsertTrans.Com mit();
    }
    catch (SqlException sqlerr)
    {
    InsertTrans.Rol lback();

    EmployeeConnect ion.Close();

    EmployeeConnect ion.Dispose();
    Response.Write (sqlerr.ToStrin g());

    }
    finally
    {
    EmployeeConnect ion.Close();
    }


    Thanks,
    Ramesh
  • Ramesh

    #2
    Transaction Problem

    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[color=blue]
    >-----Original Message-----
    >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[/color]
    am[color=blue]
    >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.
    >
    >SqlTransacti on InsertTrans;
    >DateTime Start = DateTime.Now;
    >DateTime End;
    >Random iDepartno = new Random();
    >Random iJobCode = new Random();
    >Random iExperience = new Random();
    >
    >EmployeeComman d.Connection = EmployeeConnect ion;
    >
    >EmployeeComman d.CommandType = CommandType.Sto redProcedure;
    >EmployeeComman d.CommandText = "InsertEmployee Dtl";
    >EmployeeComman d.Transaction = InsertTrans;
    >
    >
    >SqlParameter FName,LName,Dat ej,DB,Dptno,Gra de, JCode ;
    >
    >FName = new SqlParameter("@ FName", SqlDbType.NVarC har,[/color]
    50);[color=blue]
    >FName.Directio n = ParameterDirect ion.Input;
    >
    >LName = new SqlParameter("@ LName", SqlDbType.NVarC har,[/color]
    50);[color=blue]
    >LName.Directio n = ParameterDirect ion.Input;
    >..
    >..
    >..
    >EmployeeComman d.Parameters.Ad d(FName);
    >EmployeeComman d.Parameters.Ad d(LName);
    >..
    >..
    >..
    >for (int iInsertRecord = 0; iInsertRecord !=
    >System.Int32.P arse(txtInsert. Text); iInsertRecord++ )
    >{
    >try
    >{
    >EmployeeComman d.Parameters["@FName"].Value
    > = "Firstname" + iInsertRecord;
    >EmployeeComman d.Parameters["@LName"].Value = "Lastname" +
    >iInsertRecor d;
    >...
    >...
    >
    > EmployeeConnect ion.Open();
    >InsertTrans= EmployeeConnect ion.BeginTransa ction
    >(IsolationLeve l.ReadCommitted );
    >
    >EmployeeComman d.ExecuteNonQue ry();
    >InsertTrans.Co mmit();
    > }
    >catch (SqlException sqlerr)
    > {
    > InsertTrans.Rol lback();
    >
    > EmployeeConnect ion.Close();
    >
    > EmployeeConnect ion.Dispose();
    >Response.Wri te (sqlerr.ToStrin g());
    >
    > }
    >finally
    > {
    > EmployeeConnect ion.Close();
    > }
    >
    >
    >Thanks,
    >Ramesh
    >.
    >[/color]

    Comment

    • Jon Skeet

      #3
      Re: Transaction Problem

      Ramesh <jr_babu@hotmai l.com> wrote:[color=blue]
      > 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.[/color]

      Well it's right - you've got a local variable called InsertTrans, and
      you're trying to read it in the line
      EmployeeCommand .Transaction=In sertTrans;
      without assigning to it first. You're not allowed to do that. Now, what
      did you want it to actually do?

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Jon Skeet

        #4
        Re: Transaction Problem

        Ramesh <jr_babu@hotmai l.com> wrote:[color=blue]
        > I am using stored procedure for insertion, I don't know
        > how to initialise. I am trying to create transaction for
        > each insertion statment.[/color]

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

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • MSFT

          #5
          RE: Transaction Problem

          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:

          EmployeeConnect ion.Open();
          InsertTrans= EmployeeConnect ion.BeginTransa ction

          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


          Hope this help.

          Luke

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

          Comment

          Working...