can't find error in code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The following code generates the error "...not all code paths return a value" but I can't find the problem. I have tried putting "return;" or "return 0;" just after the close of the "if" statement with no luc

protected void sGetError(SqlException oException, SqlConnection db

int i
sError = ""
if (db != null)

for (i = 0; i < oException.Errors.Count - 1; i++

sError = sError + oException.Errors.ToString() + "/n"
 
Lee:

It's running fine for me, the only thing I changed was the second line to
string sError = string.Empty;

(b/c sError is declared outside of this function).
Lee said:
The following code generates the error "...not all code paths return a
value" but I can't find the problem. I have tried putting "return;" or
"return 0;" just after the close of the "if" statement with no luck
protected void sGetError(SqlException oException, SqlConnection db)
{
int i;
sError = "";
if (db != null)
{
for (i = 0; i < oException.Errors.Count - 1; i++)
{
sError = sError + oException.Errors.ToString() + "/n";
}
}
}
 
You didn't declare a type for sError. This method has no return!

protected void sGetError(SqlException oException, SqlConnection db)
{
int i;
string sError = "";
if (db != null)
{
for (i = 0; i < oException.Errors.Count - 1; i++)
{
sError = sError + oException.Errors.ToString() + "/n";
}
}
}


HTH;
Eric Cadwell
http://www.origincontrols.com
 
Wierd, maybe the signature should read:

protected string sGetError(SqlException oException, SqlConnection db)
{
string sError = string.Empty;
... //build the string

return sError;
}

instead of declaring sError outside the function.

-Eric
 
Eric:

I'm not following you. I took Lee's code and pasted it into a C# project
and it compiled except for that fact that sError wasn't declared. Once it
was, it compiled fine. However, the signature is void, hence it has no
return type so I'm not sure I understand your point.

Bill
 
Yes, I found the same thing, but it never occurred to me that he had
declared sError outside the function (as you noted).
I'm just confused how he got that error message from a void function?

-Eric
 
Back
Top