The following method is supposed to get data from database, store them in an array, and then return it.
But somehow error of "Not all code paths return a value " occured. Pls help me with this, your help is greatly appreciated!
(note:DBClass etc. are class made by me, which works well without problem)
public ArrayList querycolidbyupcolid()
{
try
{
DBClass db = new DBClass();
db.ConnStr = ConfigurationSettings.AppSettings["cstr_article"];
db.Sql = "select colid from webcolumn where upcolid=" + _upcolid;
db.readerData();
ArrayList s = new ArrayList(20);
while (db.dr.Read())
{
int i;
i=0;
s.Add(db.dr["colid"].ToString());
i++;
}
return s;
}
catch
{}
}

Error:Not all code paths return a value
kxp
Mr. Narayanan,
Thank you very much! Indeed camel naming is a good programing habit, I will keep to it in the future. Thanks for your advice.
In the above snippet, I forgot to delete "i" variable, which served as index of another "Array" which was replaced here by "ArrayList".
Thanks again for what you have done for me!
TecToc
that error means that this function has a way that when it is executed it won't return a value. Example of one simple case of this:
public int A(int b)
{
if(b == 1)
{
return 2;
}
else
{
}
}
This code would produce the same error as yours because there's, at least, one way that the function doesn't return a value. In this case try calling the function A with b different than 1, it won't return any value since it doesn't enter in the if code block.
In your case, if an error occurs in the try block it will jump to the catch block and there's no return there (or after it), so since an error ocurred and you don't have values to return, a return null; would be one possible solution to your problem. Other would be return new ArrayList();
JacksonJones
Mr. n0n4m3,
Thank you very much for your patient and execellent explanation, which is so useful for me.
Your example is very clear and right to the point as to solve my problem.
Thanks a million!
AnilGopu
The code should have a return statement at either the catch block or before the end of the function.
public ArrayList QueryColIdByUpColId() {
try {
}
catch {
}
return null;
}
I see a couple of issues with the code snippet u have give...
Refer .Net Coding Guidelines -> http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpgenref/html/cpconnamingguidelines.asp