Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Try block and variable scope

Status
Not open for further replies.

brd24gor

MIS
May 18, 2005
123
US
I am having a bit of trouble with a try..catch..finally block I am writing. When I get to my finally block, the compiler is complaining that it can't find my reader. It doesn't make much sense to me that a variable is the try block would be out of scope in the finally block.

Code:
		private Question RetrieveQuestion(string testID)
		{
			try
			{
				//Retrieve test questions from the database
				string sqlQues = "SELECT Tests.Test_ID, Test_Questions.Question_Num, [Test_Questions.Text], " +
					"Test_Questions.Graphic_Path" +
					"FROM Tests, Test_Questions" +
					"WHERE Tests.Test_ID = Test_Questions.Test_ID AND Test_Questions.Test_ID = '" + testID + "' " +
					"ORDER BY Test_Questions.Question_Num";
				OleDbCommand getQues = new OleDbCommand(sqlQues, aLib.SQLConnection);
				OleDbDataReader readQues = getQues.ExecuteReader();

				while(readQues.Read())
				{
					Question tempQues = new Question();
					tempQues.Text = readQues.GetString(2);
					tempQues.SetGraphic(readQues.GetString(3),"administrator","sthstrn");
					tempTest.AddQuestion(tempQues);
				}
			}
			catch (Exception e)
			{
				MessageBox.Show(e.ToString());
			}
			finally
			{
				readQues.Close();
			}
		}
 
Forget it. I should have consulted Google sooner. It does in fact go out of scope after the Try block.

--Brad
 
I'm not sure what is the best solution to this, but here is one.

There is also the "using" block that automatically releases the resources when out of the block

private Question RetrieveQuestion(string testID)
{
OleDbDataReader readQues = null;
try
{
//Retrieve test questions from the database
string sqlQues = "SELECT Tests.Test_ID, Test_Questions.Question_Num, [Test_Questions.Text], " +
"Test_Questions.Graphic_Path" +
"FROM Tests, Test_Questions" +
"WHERE Tests.Test_ID = Test_Questions.Test_ID AND Test_Questions.Test_ID = '" + testID + "' " +
"ORDER BY Test_Questions.Question_Num";
OleDbCommand getQues = new OleDbCommand(sqlQues, aLib.SQLConnection);

readQues = getQues.ExecuteReader();

while(readQues.Read())
{
Question tempQues = new Question();
tempQues.Text = readQues.GetString(2);
tempQues.SetGraphic(readQues.GetString(3),"administrator","sthstrn");
tempTest.AddQuestion(tempQues);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{//Just in case something bombs before
if (!(readQues ==null))
readQues.Close();
}
}
 
Thanks for the input. I have read a couple of different ways to do this including embedded try blocks and the using block (as long as it inherits IDisposable). I think a using block would be more useful if I had a longer function as length of scope would be more of an issue. While it may not be the "best" way of doing it, your method looks like it will work.

Thanks again!

--Brad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top