Posts
463
Comments
319
Trackbacks
1
.NET: Use of unassigned local variable in try-catch-finally

This is just an example, but you should get the idea:

You have a SqlDataReader.   You use ExecuteReader inside a try block like this:

SqlDataReader oDataReader =  oCommand.ExecuteReader()  ;

You want to ensure it closes, so you say SqlDataReader.Close() in the finally block.  But the compiler gives the “Use of unassigned local variable” error. 

To avoid this, you need to make sure that you declare the DataReader outside of the try block itself, either within whatever method you have the try-catch-block in, or as a memeber of the class itself.

posted on Friday, August 12, 2005 9:19 AM
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 4 and 6 and type the answer here: