Results 1 to 2 of 2

Thread: [2.0] How to commit

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Question [2.0] How to commit

    Hi all
    I make a function for execute multile query I am using transaction commit and rollback in it!
    but the problem is how to commit the transaction it showing me error that use of unsigned variable transaction!


    C# Code:
    1. public bool ExecuteMultipleQuery(string[] queryString)
    2.         {
    3.             SqlConnection myConnection;
    4.             System.Data.SqlClient.SqlTransaction transaction;
    5.             DbCommand dbCommand;
    6.             myConnection = OpenNewConnection();//OPEN THE CONNECTION
    7.             dbCommand = myConnection.CreateCommand();
    8.             try
    9.             {
    10.                 transaction = myConnection.BeginTransaction();
    11.                 dbCommand.CommandType = CommandType.Text;
    12.                 dbCommand.Transaction = transaction;
    13.                 dbCommand.Connection = myConnection;
    14.                 foreach (string query in queryString)
    15.                 {
    16.                     dbCommand.CommandText = query;
    17.                     dbCommand.ExecuteNonQuery();
    18.                 }
    19.                 transaction.Rollback();
    20.                 return true;
    21.             }
    22.             catch (Exception ex)
    23.             {
    24.                 transaction.Commit();//HERE GIVING ME ERROR USE OF UNSIGNED
    25.                 //VARIABLE  transaction
    26.                 return false;
    27.             }
    28.             finally
    29.             {
    30.                 myConnection.Close();
    31.                 dbCommand.Dispose();
    32.             }
    33.         }
    Last edited by shakti5385; Aug 14th, 2007 at 06:05 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] How to commit

    It's not giving you an error message, it's giving you a warning message. It's warning you that it is theoretically possible to reach that line without the variable ever having been assigned a value. It's telling you that to make sure that you are aware and make sure that that doesn't happen, thus averting a NullReferenceException. If you've already made sure of that then the way to clear the warning is to make sure you assign to the variable. Instead of declaring it like this:
    C# Code:
    1. System.Data.SqlClient.SqlTransaction transaction;
    do this:
    C# Code:
    1. System.Data.SqlClient.SqlTransaction transaction = null;
    That said, I recommend using the TransactionScope class in .NET 2.0.

    Also, you are currently rolling back the transaction if there is no exception and committing if there is. Surely it should be the other way around.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width