I'am rather new to C#, but have som programing experience in VBA and VB. I'am learning Visual development in C#.
Problem:
I'am trying to call a method with a string value and to have the called method to return a boolean false value indicating that a lookup in the database returned a value that allready exists. I recieve an error that says: not all code paths return a value. Thanks in advance,
Here's the code:
chkFrm(ddlCompany.Text);
public Boolean chkFrm(string searchParam){
string ddlComp = "'" + searchParam + "%" + "'"; string sql = @"SELECT Name FROM Company WHERE Name LIKE " + ddlComp; SqlConnection con = new SqlConnection( "data source=.;initial catalog=survSys;" + "user id=laho;pwd=12345"); SqlCommand cmd = new SqlCommand(sql, con);con.Open();
string name = null; try{
name = (
string)cmd.ExecuteScalar(); if (name != null){
return false;}
}
finally{
con.Close();
}
}
Larry HojerPosted Jun 14, 2007, 3:23 AM
I'am learning the C# syntax and somtimes you need this simple but great help.
Thanks again,
AlanPosted Jun 13, 2007, 7:04 AM
As you had it originally, this wasn't the case if 'name' was null but adding the 'else' clause solved this problem.
I think it would also have worked if you'd placed a 'return true;' statement after the 'finally' close and just before the end of the method.
Larry HojerPosted Jun 13, 2007, 6:33 AM
Thanks Alan, it now works as I whant it to.
Why do you need the extra ELSE clause?
AlanPosted Jun 12, 2007, 6:42 AM
To get rid of the compiler error, you could place an else clause in there:
try
{
name = (
string)cmd.ExecuteScalar(); if (name != null){
return false;}
else
{
return true;
}
}
finally{
con.Close();
}