0

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."}

2 Answers 2

0

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.

1
  • Thanks for the help. But this did not resolve the issue. Commented May 29, 2012 at 2:38
0

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

Your Answer

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

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