tags:

views:

355

answers:

11

I'm getting a "Unreachable code detected" message in Visual Studio at the point con.close() in my code below. Can you spot what I've done wrong?

private int chek1(String insert)
{
    OleDbConnection con = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=d:\\sdb.mdb");
    OleDbCommand com = new OleDbCommand("select count(*) from sn where sn='" + insert + "\'", con);           
    con.Open();

    int po = (int)com.ExecuteScalar();           
    if (po > 0)
        return 1;
    else
        return 0;
    con.Close();    
}
+28  A: 

The function exits when you return 1 or 0 (when you return anything, but 1 or 0 in your case); so there's no way that con.Close() can be called.

In the code you've posted, you're guaranteed to return, since you have a return statement in both branches of your if-statement. If only one branch had a return statement, con.Close() could still be reached.

But you shouldn't be using Close that way anyway -- you should be using using statements.

using (OleDbConnection con = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=d:\\sdb.mdb"))
using (OleDbCommand com = new OleDbCommand("select count(*) from sn where sn='" + insert + "\'", con))
{
    con.Open();

    int po = (int)com.ExecuteScalar();           
    if (po > 0)
        return 1;
    else
        return 0;

    // con.Close and con.Dispose will be called automatically at the end of the using block
}      
Mark Rushakoff
thanks, so should I delete it, it is just waring not an error
habbo95
@habbo95, no, don't just delete it, use the `using` statements as shown by Mark. This will automatically call `Dispose` on the connection and command, and disposing a connection closes it
Thomas Levesque
@habbo95 well, it IS and error if it never gets closed. "using" is idisposible, thus it closes/disposes of the connection
Mark Schultheiss
+4  A: 

You have an if / else above that will always trigger a return:

    if (po > 0)
        return 1;
    else
        return 0;

So there is no possible way any code will execute after this snippet of code.

Justin Ethier
+2  A: 
 con.Close();

is unreachable. Either branch of the if statement returns so that line cannot be reached.

Joshua
+1  A: 
    if (po > 0)
        return 1;
    else
        return 0;

There's your problem. Either po > 0 is true, in which case 1 is returned, either it isn't, in which case 0 is returned. No matter what, con.Close(); will never get executed.

IVlad
+1  A: 

you have a return before the end of your routine. it will break out of that function before the Close() statement is called. just move con.Close() before your if block.

Ian Jacobs
+1  A: 

In addition to considering all the above answers, better consider using try..catch..finally and in finally, close the connection object. This will be better coding approach. You can improve your code like this.

private int chek1(String insert) {    
        OleDbConnection con = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=d:\\sdb.mdb");
        OleDbCommand com = new OleDbCommand("select count(*) from sn where sn='" + insert + "\'", con);       
    try{    
        con.Open();

        int po = (int)com.ExecuteScalar();           
        if (po > 0)
            return 1;
        else
            return 0;
    catch(Exception e){
    }finally{
        con.Close();
    }
}
RaviG
This will work, but the use of "using" blocks, as in other answers is generally considered cleaner.
Beska
A: 

This will work

int po = (int)com.ExecuteScalar();           
int result = 0;
if (po > 0) {
    result = 1;
}
con.Close();
return result;

in your case the con.Close(); is not reached because the function returns earlier

Harald Scheirich
A: 

Change your code to

private int chek1(String insert)
{
    OleDbConnection con = null;
    try {
    con = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=d:\\sdb.mdb");
    OleDbCommand com = new OleDbCommand("select count(*) from sn where sn='" + insert + "\'", con);           
    con.Open();

    int po = (int)com.ExecuteScalar();           
    if (po > 0)
        return 1;
    else
        return 0;
    }
    finally {
        con.Close();
    }
}
Sijin
+1  A: 

The reason has been answered, you should look at using the using statement. It will close your connection for you automatically.

using (OleDbConnection con = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=d:\\sdb.mdb"))
{
    using (OleDbCommand com = new OleDbCommand("select count(*) from sn where sn='" + insert + "\'", con))
    {
        con.Open();
        int po = (int)com.ExecuteScalar();           
        if (po > 0)
            return 1;
        else
            return 0;
    }
}
Dustin Laine
`using` statements can be nested to decrease indent
abatishchev
+6  A: 

Your code could be like this:

private int check(string sn)
{
    using (OleDbConnection connection = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=d:\\sdb.mdb"))
    using (OleDbCommand command = connection.CreateCommand())          
    {
        command.CommandText = "SELECT COUNT(*) FROM sn WHERE sn=?";
        command.Parameters.Add("@sn", sn));
        con.Open();               
        return ((int)com.ExecuteScalar() > 0) ? 1 : 0;
    }
}
abatishchev
That code won't compile. You have a stray `if` in there...
Drew Noakes
@Drew Noakes: You just accidentally saw a moment between 2 edits
abatishchev
A: 

It is unreachable because the method already return a value before its execute the con.Close() method.

Please Modify your code into:

using (OleDbConnection con = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=d:\\sdb.mdb"))
{
    using (OleDbCommand com = new OleDbCommand("select count(*) from sn where sn='" + insert + "\'", con))
    {
        con.Open();

        int po = (int)com.ExecuteScalar();  

        return po > 0 ? 1 : 0;
    }
}
Christofel