Question

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.

Était-ce utile?

La solution

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

Autres conseils

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

    catch (Exception ex)
    {
        return cValueFound;
    }
    finally
    {
    }
}

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

move the return either after finally or from catch

just add your return statement below finally block.

Dont return in try blck.

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
        }


 }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top