0
public bool ConnectToDB()
{
    SqlConnection sqlConnect = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ToString());
    try
    {
        sqlConnect.Open();
        if (sqlConnect.State == ConnectionState.Open)
        {
            return true;
        }
    }
    catch (SqlException ex)
    {
        // some code here...
    }
    finally
    {
        sqlConnect.Close();
    }
}

when I run this code,it is giving an error

Error 'DataAccess.ConnectToDB()': not all code paths return a value

0

8 Answers 8

1

you have to put all return condition in function.

Sign up to request clarification or add additional context in comments.

Comments

0
public bool ConnectToDB()
{
    SqlConnection sqlConnect = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ToString());
    try
    {
        sqlConnect.Open();
        if (sqlConnect.State == ConnectionState.Open)
        {
            return true;
        }else 
return false;
    }
    catch (SqlException ex)
    {
        return false;
    }
    finally
    {
        sqlConnect.Close();
    }
}

Comments

0

Your method signature says that you return a bool (public bool ConnectToDB()). However, you only return a bool when your connection is open.

I think you intend to return false otherwise. If so, return false from your catch clause.

catch (SqlException ex)
{
    // some code here...
    return false;
}

Comments

0

You will need to amend a return value outside the if condition as to what will happen if there was a exception before the 'return true' reaches or if the IF condition fails? You would need to have the return statement in the else part and in the catch block.

1 Comment

thanks for pointing it out. That was a unintentional mistake and it has been corrected.
0

Consider in your code if an exception happens which gets handled in the catch but return true is never reached and the execution is now in catch and then moves to the finally block. So you would need to add return false in the catch part for this to compile.

Comments

0

When you have a return type all of your code paths i.e. try/catch blocks are supposed to return something. In this case, a false would be a appropriate value. However, if the catch blocks rethrows the exception it wont be required.

Comments

0

You need to add return true at the end of function, and add return false in exception block.

Comments

0

It seems superfluous that you're opening the connection to check if the database is open. In fact, one could argue that if the database connection is already open it will throw an exception. You do have exception handling involved but you can simply check the state without opening a connection:

public bool ConnectToDB() {
   return sqlConnect.State == ConnectionState.Open;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.