Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have created a Survey List in MOSS 2007. The field used in the survey is of 'Choice' type. While getting the field value in a custom web part program on another page, the FieldRenderingControl of SPField is throwing an exception.

The code for webpart is :

    SPSite mySite = SPContext.Current.Web.Site;

            SPWeb myWeb = mySite.OpenWeb();

            SPList mylist = myWeb.Lists[surveyname];

            int surveyUserCount = mylist.ItemCount;
        try
         {

            if (surveyUserCount > 0)
            {
                foreach (SPListItem item in mylist.Items)
                {

                    foreach (SPField field in item.Fields)
                    {
                        if (field.TypeAsString == "Choice")
                        {

                            string check = Convert.ToString(field.FieldRenderingControl.ItemFieldValue);
                            if (check != "")
                        }
                   }
                }
             }
           }

The exception thrown is : 'field.FieldRenderingControl.ItemFieldValue' threw an exception of type 'System.InvalidOperationException'
base {System.SystemException} = {"Operation is not valid due to the current state of the object."}

share|improve this question
add comment

2 Answers

You need to increase connection keys in your web config file as there is a good chance the survey may have a lot of items.

Add the following entries in the app settings section inside the web.config file for your web application where the site collection is located.

<appSettings> <add key="aspnet:MaxHttpCollectionKeys" value="8000" /> </appSettings>

Let us know if the above the solves your issue or not.

share|improve this answer
    
Thanks for the help. But this did not resolve the issue. –  Thomas Mathew May 29 '12 at 2:38
add comment
up vote 0 down vote accepted

The problem was that FieldRenderingControl is created only on Edit/DispForm page. Otherwise it will always give an exception.

However, by making use of SPFieldChoice and StringCollection, i was able to get both the count and index of the selected option. Here is the snippet:

                 try
             {

                 if (surveyUserCount > 0)
                 {
                     foreach (SPListItem item in myList.Items)
                     {
                         responseUserTotal = 0;



                         foreach (SPField field in item.Fields)
                         {
                             int tempIndex = 0;

                             if (field.TypeAsString == "Choice")
                             {
                                 SPFieldChoice dropDown = field as SPFieldChoice;
                                 StringCollection eachQuesChoices = dropDown.Choices;
                                 tempIndex = eachQuesChoices.IndexOf(Convert.ToString(item[field.Title]));
                                 if (tempIndex >= 0)
                                 {
                                     responseUserTotal += getRating(eachQuesChoices.Count, tempIndex);
                                 }


                             }
                         }

                         responseSurveyTotal += responseUserTotal;

                     }

                     responseSurveyTotal = responseSurveyTotal / surveyUserCount;
                 }
             }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.