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.

3
  • 1
    Could you post the error message you're seeing?
    – Dan
    Commented May 1, 2012 at 21:26
  • Clearly if the compiler gives you an error, the code is wrong. Oddly enough, the error message produced tells you exactly what the wrong bit is!
    – Tergiver
    Commented May 1, 2012 at 21:28
  • I would expect a 'not all code paths return a value' error to start with...
    – Dan
    Commented May 1, 2012 at 21:29

5 Answers 5

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 ;
 }
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
    {
    }
}
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

0

just add your return statement below finally block.

Dont return in try blck.

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
        }


 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.