Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

DataReader problem

shinsnakeshinsnake Member Posts: 93
I'm having a problem with a DataReader that I can't quite figure out. I'm using Visual Studio 2003, working on a Windows Application and this code is in my .cs file.

[code]private void btnCheckData_Click(object sender, System.EventArgs e)
{
OleDbConnection updConn = new OleDbConnection(tagConnString);
updConn.Open();


OleDbDataReader sel1Reader;
try
{
sel1Reader = selPSWCommand.ExecuteReader();
sel1Reader.Read();
[STUFF]...
sel1Reader.Close();
}
catch(Exception ex)
{
string errMessage = "";
if(ex.Message.ToString() == "Row handle is invalid.")
{
errMessage = "A Plug Switch Tag Entry for the Tag Number Does Not Exist in Database!
Please check tag name.";
MessageBox.Show(errMessage, "Error");
}
else
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
[STUFF]...
}
try
{
sel1Reader = selPSWFCommand.ExecuteReader();
sel1Reader.Read();
[STUFF]...
sel1Reader.Close();
}
catch(Exception ex)
{
string errMessage = "";
if(ex.Message.ToString() == "Row handle is invalid.")
{
errMessage = "A Plug Switch Tag Entry for the Tag Number Does Not Exist in Database!
Please check tag name.";
MessageBox.Show(errMessage, "Error");
}
else
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
[STUFF]...
}
}[/code]

Okay, so here's the problem. I need to capture the exception for the .ExecuteReader() command because the user might type in a database that doesn't exist, at which point, the program will crash. So I declare the data reader, then assign it in the try part of the program. But if it causes an exception, it doesn't execute the rest of the try section and thus, never closes the reader, and then can't complete any of the remaining tasks in the program. So, I tried putting the sel1Reader.Close() in a finally section, but then I get a compile error about the use of an unassigned variable. Please help.

Comments

  • iwilld0itiwilld0it Member Posts: 1,134
    Try to follow this pattern ...

    [code]
    OleDbConnection updConn = new OleDbConnection(tagConnString);
    OleDbCommand selPSWCommand = new OleDbCommand(myQuery, updConn);
    OleDbDataReader sel1Reader;

    try
    {
    updConn.Open();
    sel1Reader = selPSWCommand.ExecuteReader(CommandBehavior.CloseConnection);
    sel1Reader.Read();
    // Stuff

    }
    catch(Exception ex)
    {
    // Handle error
    }
    finally
    {
    if(null != sel1Reader && !sel1Reader.IsClosed)
    sel1Reader.Close();
    }
    [/code]

    1. The call to open the connection must be in the Try part because that is where an exception would be thrown if the connect string was wrong.

    2. In the Finally, you check to see if the datareader variable was set and still open. If so, this is when you close it.

    NOTE: The "CommandBehavior.CloseConnection" argument means that closing the datareader will also close the connection that opened it.

    : I'm having a problem with a DataReader that I can't quite figure out. I'm using Visual Studio 2003, working on a Windows Application and this code is in my .cs file.
    :
    : [code]private void btnCheckData_Click(object sender, System.EventArgs e)
    : {
    : OleDbConnection updConn = new OleDbConnection(tagConnString);
    : updConn.Open();
    :
    :
    : OleDbDataReader sel1Reader;
    : try
    : {
    : sel1Reader = selPSWCommand.ExecuteReader();
    : sel1Reader.Read();
    : [STUFF]...
    : sel1Reader.Close();
    : }
    : catch(Exception ex)
    : {
    : string errMessage = "";
    : if(ex.Message.ToString() == "Row handle is invalid.")
    : {
    : errMessage = "A Plug Switch Tag Entry for the Tag Number Does Not Exist in Database!
    Please check tag name.";
    : MessageBox.Show(errMessage, "Error");
    : }
    : else
    : {
    : MessageBox.Show(ex.Message.ToString(), "Error");
    : }
    : [STUFF]...
    : }
    : try
    : {
    : sel1Reader = selPSWFCommand.ExecuteReader();
    : sel1Reader.Read();
    : [STUFF]...
    : sel1Reader.Close();
    : }
    : catch(Exception ex)
    : {
    : string errMessage = "";
    : if(ex.Message.ToString() == "Row handle is invalid.")
    : {
    : errMessage = "A Plug Switch Tag Entry for the Tag Number Does Not Exist in Database!
    Please check tag name.";
    : MessageBox.Show(errMessage, "Error");
    : }
    : else
    : {
    : MessageBox.Show(ex.Message.ToString(), "Error");
    : }
    : [STUFF]...
    : }
    : }[/code]
    :
    : Okay, so here's the problem. I need to capture the exception for the .ExecuteReader() command because the user might type in a database that doesn't exist, at which point, the program will crash. So I declare the data reader, then assign it in the try part of the program. But if it causes an exception, it doesn't execute the rest of the try section and thus, never closes the reader, and then can't complete any of the remaining tasks in the program. So, I tried putting the sel1Reader.Close() in a finally section, but then I get a compile error about the use of an unassigned variable. Please help.
    :

  • shinsnakeshinsnake Member Posts: 93
    [b][red]This message was edited by shinsnake at 2006-7-7 6:57:25[/red][/b][hr]
    I tried your code and it gives me the same build error where it won't let me run because of the follwing error:

    Use of unassigned local variable 'sel1Reader'

    The errors occurs in the finally statement.

    Is there a default you can assign to sel1Reader? When I declare my other variables, I usually assign them a null or a new data type. But, it also gives me an error if I assign it like this:

    [code]OleDbDataReader sel1Reader = new OleDbDataReader();[/code]

    The error is because there is no overload for OleDbDataReader that uses 0 arguments.

    : Try to follow this pattern ...
    :
    : [code]
    : OleDbConnection updConn = new OleDbConnection(tagConnString);
    : OleDbCommand selPSWCommand = new OleDbCommand(myQuery, updConn);
    : OleDbDataReader sel1Reader;
    :
    : try
    : {
    : updConn.Open();
    : sel1Reader = selPSWCommand.ExecuteReader(CommandBehavior.CloseConnection);
    : sel1Reader.Read();
    : // Stuff
    :
    : }
    : catch(Exception ex)
    : {
    : // Handle error
    : }
    : finally
    : {
    : if(null != sel1Reader && !sel1Reader.IsClosed)
    : sel1Reader.Close();
    : }
    : [/code]
    :
    : 1. The call to open the connection must be in the Try part because that is where an exception would be thrown if the connect string was wrong.
    :
    : 2. In the Finally, you check to see if the datareader variable was set and still open. If so, this is when you close it.
    :
    : NOTE: The "CommandBehavior.CloseConnection" argument means that closing the datareader will also close the connection that opened it.
    :


  • shinsnakeshinsnake Member Posts: 93
    [b][red]This message was edited by shinsnake at 2006-7-7 7:21:26[/red][/b][hr]
    Okay, I just declared the OleDbDataReader sel1Reader globally, and now it works fine. I'm sure that's not the "proper" way to do it, but it works. Thanks.


  • iwilld0itiwilld0it Member Posts: 1,134
    Working w/ Globals is not usually good programming practice ...

    try setting the datareader to a default ...

    [code]
    OleDbDataReader sel1Reader = null;
    [/code]
Sign In or Register to comment.