Hi,
I'm trying to find the best way to treat erro in my web applicatin (using VB.NET) but i'm something is not right.
Look at this code (part of the code was removed but on the original, we do need a transaction because we update different tables):
Protected Sub ClearFlag()Dim mySQLConnectionAs New SqlConnection(ConfigurationManager.ConnectionStrings("MyString").ToString)Dim mySQLTransactionAs SqlTransactionDim mySQLCommandAs SqlCommandDim sQuery1As String ="UPDATE Users SET UserStatus = 1"Try mySQLConnection.Open() mySQLTransaction = mySQLConnection.BeginTransaction mySQLCommand =New SqlCommand(sQuery1, mySQLConnection, mySQLTransaction) mySQLCommand.ExecuteNonQuery() mySQLTransaction.Commit()Catch exAs Exception mySQLTransaction.Rollback()Finally mySQLConnection.Close() mySQLCommand.Dispose() mySQLTransaction.Dispose() mySQLConnection.Dispose()End Try End Sub
The Error List says:
- Variable 'mySQLCommand' is used before it has been assigned a value. A null reference exception could result at runtime.
- Variable 'mySQLTransaction' is used before it has been assigned a value. A null reference exception could result at runtime.
What i did to try to avoid it is declare the variables using "NEW", like:
Dim mySQLCommand as NEW SQLCommand
But, i can't do this with the transaction.
NESTED TRY AND CATCH
Also, i was thinking about this to handle specific errors like, try to open a connection and just continue if the connection was oppened, or else, treat that error. Try to update the database and just continue, it if did well or else, treat the error...
Is that possible to use nested try and catch for this or, does anyone has a better idea?
I appreciate any comment.
Thanks
Hi E.Brito,
As I can see, these messages are warning messages. The comipler analyzes the code and found the variable might not be pointing to a valid object, since the Try block and Final Block are in same level.
Of course, you can ignore this message, but I would suggest you put the following code before the Try block.
mySQLConnection.Open()
mySQLTransaction = mySQLConnection.BeginTransaction
mySQLCommand = New SqlCommand(sQuery1, mySQLConnection, mySQLTransaction)
This will eliminate the warning message.
HTH. If anything is unclear, please feel free to mark this post as Not Answered and post your reply. Thanks!
|||hiKevin Yu
Thanks for your replay.
If i do it the warning message will desapear but... what if i get an error on the connection? Since it will be out of the "Try Catch" block, i wont be able to handle the error.
Do you have a sugestion for this?
Thanks in advance,
|||Hi E.Brito,
For this warning message, you can try to put the dispost in an If block, like
If Not mySQLCommand Is Nothing Then mySQLCommand.Dispose()
This will make sure that the object reference is not Nothing, but it will not deliminate the warning message. In this case, you can ignore it.
You can also put the SqlConnection.Open in another try/catch block, so that you can seperate the process of exceptions.
|||This will work fine.Thanks a lot for your help
No comments:
Post a Comment