NOT ALL CODE PATHS RETURN A VALUE! [Resolved]

Posted by Coolbharat under ASP.NET on 3/4/2014 | Points: 10 | Views : 837 | Status : [Member] | Replies : 4
Hi,
in the below code i am checking whether the database exists or not.if it exists it returns boolean value true or else false.But it gives me strange errors i.e. "Not All Code Paths Return A Value".I need some suggestions.

protected Boolean check_database(String dbname)
{ //Begining of 'check_database()' method.

try
{
con.Open();
SqlCommand cmd = new SqlCommand("checkDB_SP", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@db_name", dbname);
da = new SqlDataAdapter(cmd);
da.Fill(ds);

Boolean result;

if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
txt_result.Text = Convert.ToString(ds.Tables[0].Rows[0][0]);
int count = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
if (count == 1)
{
Response.Write("<Script language='javascript' type='text/javascript'>alert('DataBase Exists!')</Script>");
//return true;
result = true;
break;
}
else
{
Response.Write("<Script language='javascript' type='text/javascript'>alert('DataBase Doesn't Exists!')</Script>");
txt_dbname.Text = "";
txt_dbname.Focus();
//return false;
result = false;
break;
}
return result;

}
else
{
lbl_msg.Text = "No Result In The DataSet!";
return false;
}

}
catch (Exception excp)
{
lbl_msg.Text = excp.Message.ToString();
}
finally
{
con.Close();
}

} //End of 'check_database()' method.




Responses

Posted by: Prabu_Spark on: 3/4/2014 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi Coolbharat ,
You missed the return type, in case if you caught exception, in catch block. Try the below code to solve your problem.


catch (Exception excp)
{

lbl_msg.Text = excp.Message.ToString();
return false;
}

With regards,
J.Prabu.
[Email:prbspark@gmail.com]

Coolbharat, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rasik on: 3/4/2014 [Member] Starter | Points: 50

Up
0
Down

Resolved
You need to specify return statement in each main block like outer If and else statement, Try and catch block.
Otherwise you will receive the above error.

Coolbharat, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 3/4/2014 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi,
Whenever you declare any functions or methods with return type,then you must return that data type

from function or method.

For example:
public int sum_nos(int i,int j)

{
return i+j;
}

Here type is integer.

In your case,you have defined function with return type as Boolean,then you must return true/false

based on your requirement.

Coolbharat, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Coolbharat on: 3/4/2014 [Member] Starter | Points: 25

Up
0
Down
Thanks,everyone for spending their valuable time on solving this problem.I solved this when i wrote the return statement inside the catch block.

Coolbharat, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response