|
-
Aug 12th, 2007, 08:54 PM
#1
Thread Starter
Just Married
[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:
public bool ExecuteMultipleQuery(string[] queryString)
{
SqlConnection myConnection;
System.Data.SqlClient.SqlTransaction transaction;
DbCommand dbCommand;
myConnection = OpenNewConnection();//OPEN THE CONNECTION
dbCommand = myConnection.CreateCommand();
try
{
transaction = myConnection.BeginTransaction();
dbCommand.CommandType = CommandType.Text;
dbCommand.Transaction = transaction;
dbCommand.Connection = myConnection;
foreach (string query in queryString)
{
dbCommand.CommandText = query;
dbCommand.ExecuteNonQuery();
}
transaction.Rollback();
return true;
}
catch (Exception ex)
{
transaction.Commit();//HERE GIVING ME ERROR USE OF UNSIGNED
//VARIABLE transaction
return false;
}
finally
{
myConnection.Close();
dbCommand.Dispose();
}
}
Last edited by shakti5385; Aug 14th, 2007 at 06:05 AM.
-
Aug 12th, 2007, 10:00 PM
#2
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:
System.Data.SqlClient.SqlTransaction transaction;
do this:
C# Code:
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|