0
public static string GetContentFromSPList(string cValueToFind)
{   
    string cValueFound = "";
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite("http://mysite"))
            {
                site.AllowUnsafeUpdates = true;
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;

                    SPList oListAbout = web.Lists["About"];
                    SPQuery oQuery = new SPQuery();

                    oQuery.Query = "<OrderBy><FieldRef Name='myField' /></OrderBy><Where><Eq><FieldRef Name='myField' /><Value Type='Choice'>" + cValueToFind + "</Value></Eq></Where>";

                    SPListItemCollection collListItems = oListAbout.GetItems(oQuery);

                    foreach (SPListItem oListItem in collListItems)
                    {
                        cValueFound = (oListItem["FieldContents"] != null ? oListItem["FieldContents"].ToString() : "");
                    }
                }
            }
            return cValueFound;
        });
        //return cValueFound;
    }
    catch (Exception ex)
    {
    }
    finally
    {
        //return cValueFound;
    }
}

Above is the piece of code.

Problem is not allowing to return the string. It keeps on giving compilation errors. I am sure I am doing something wrong!!.

Thanks.

Dan Ellis
  • 5,537
  • 8
  • 47
  • 73
Anirudh
  • 581
  • 5
  • 14
  • 32

5 Answers5

2

I suppose it's something like:

"not all codes return value".

If so, just add

public static string GetContentFromSPList(string cValueToFind)
{   
       string cValueFound = "";
        try
        {
           //code
        }
        catch (Exception ex)
        {
        }
        finally
        {
           //some cleanup
        }

        return cValueFound ;
 }
Tigran
  • 61,654
  • 8
  • 86
  • 123
1

Put this at the bottom of your method because you don't return if an exception is caught.

    catch (Exception ex)
    {
        return cValueFound;
    }
    finally
    {
    }
}
agent-j
  • 27,335
  • 5
  • 52
  • 79
1

You cannot return from finally,
(control cannot leave the body from finally clause or something)

move the return either after finally or from catch

NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
0

just add your return statement below finally block.

Dont return in try blck.

Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154
0

I have seen developers missing this lot of times. Reason this happens is because once you define the return type of a function, then function should have a return statement at all exit points. In this case a function should have a return statement one at the end of the try block and one inside a catch block or just one right at the bottom as Tigran has defined. If you do not intend to return any thing from the catch block then just return null;

public static string GetContentFromSPList(string cValueToFind)
{   
       string value= "";
        try
        {
           //code
return value;
        }
        catch (Exception ex)
        {
return null;
        }
        finally
        {
           //some cleanup
        }


 }
Kunal
  • 1,913
  • 6
  • 29
  • 45