Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have surveys categorized by its year. When I click on each it displays the survey result as dropdown with an edit and delete button by its side of each survey. when I click on the edit button it moves to the edit page of that particular survey displaying all the fields of the survey in editable mode. The issue is that when I click on the recent years like 2011 or 2012 the details are displaying as dropdown and also in the edit page on clicking the edit button. But when I click on the older years like 2009 or 2007 it displays the details as dropdown but not in the edit page. When I click on the edit button of the older years it shows an "ArgumentOutOfRangeException" and the inner exception shows as "Null" although the details of that particular year displays as dropdown. This proves that the record are present for the older years but it does not displays in the edit page.

C#
public void BindSurvey(Survey SelectedSurvey)
   {
       this.SelectedSurveyID = (int)SelectedSurvey.SurveyID;

       if (SelectedSurvey.SurveyDate != null)
       {
           string completedDay = SelectedSurvey.SurveyDate.Value.Day.ToString();
           string completedMonth = SelectedSurvey.SurveyDate.Value.Month.ToString();
           string completedYear = SelectedSurvey.SurveyDate.Value.Year.ToString();

           uxCompletedDateDay.SelectedValue = completedDay;
           uxCompletedDateMonth.SelectedValue = completedMonth;
           uxCompletedDateYear.SelectedValue = completedYear;
       }
       try
       {
           uxSurveyType.SelectedValue = SelectedSurvey.SurveyTypeID.ToString();
       }
       catch { }
       if (SelectedSurvey.SurveyTypeID != null && (int)SelectedSurvey.SurveyTypeID == 2)
       {
           ShowPupilFields(true);

           uxClassName.Text = SelectedSurvey.ClassName;
           uxClassSize.Text = SelectedSurvey.ClassSize.ToString();
           uxClassYear.Text = SelectedSurvey.Year;
       }
       else
       {
           ShowPupilFields(false);
       }
       SurveyTravelService surveyTravelsService = new SurveyTravelService();
       TravelModeTypeCollection travelModes = ApplicationCacheControl.TravelModes();

       int totalUsual = 0;
       int totalPreferred = 0;

       foreach (TravelModeType travelMode in travelModes)
       {
           string usualInputControlID = "uxUsual" + travelMode.TravelModeTypeID.ToString();
           string preferredInputControlID = "uxPreferred" + travelMode.TravelModeTypeID.ToString();

           TextBox uxUsual = (TextBox)FindControl(usualInputControlID);
           TextBox uxPreferred = (TextBox)FindControl(preferredInputControlID);

           if (uxUsual != null && uxPreferred != null)
           {
               SurveyTravel thisSurveyTravel = SelectedSurvey.SurveyTravels.Where(p => p.TravelModeTypeID == travelMode.TravelModeTypeID).FirstOrDefault();

               if (thisSurveyTravel != null)
               {
                   uxUsual.Text = thisSurveyTravel.Usual.ToString();
                   uxPreferred.Text = thisSurveyTravel.Preferred.ToString();
                   totalUsual += (int)thisSurveyTravel.Usual;
                   totalPreferred += (int)thisSurveyTravel.Preferred;
               }
           }
       }
       if (SelectedSurvey.SurveyTypeID != null && (int)SelectedSurvey.SurveyTypeID == 2
           && SelectedSurvey.ClassSize != null && SelectedSurvey.ClassSize != 0)
       {
           uxUTotal.Text = totalUsual.ToString() + " (" + Convert.ToInt32(((Convert.ToDecimal(totalUsual) / Convert.ToDecimal(SelectedSurvey.ClassSize)) * 100)).ToString() + "%)";
           uxPTotal.Text = totalPreferred.ToString() + " (" + Convert.ToInt32(((Convert.ToDecimal(totalPreferred) / Convert.ToDecimal(SelectedSurvey.ClassSize)) * 100)).ToString() + "%)";
       }
       else if (SelectedSurvey.School.NoOfStaff != null && SelectedSurvey.School.NoOfStaff != 0)
       {
           uxUTotal.Text = totalUsual.ToString() + " (" + Convert.ToInt32(((Convert.ToDecimal(totalUsual) / Convert.ToDecimal(SelectedSurvey.School.NoOfStaff)) * 100)).ToString() + "%)";
           uxPTotal.Text = totalPreferred.ToString() + " (" + Convert.ToInt32(((Convert.ToDecimal(totalPreferred) / Convert.ToDecimal(SelectedSurvey.School.NoOfStaff)) * 100)).ToString() + "%)";
       }
   }


The above is the code used. The exception occurs at the line "uxCompletedDateYear.SelectedValue = completedYear;"

When I debugged the code the value is been assigned to the completedYear but value is not been to the left side This happens only for the older years.

Can anyone please help me to fix this issue
Posted
Comments
virusstorm 12-May-15 8:27am    
Check the value of "completedYear", make sure that value exists in "uxCompletedDateYear".
user 3008 13-May-15 10:26am    
I checked in the DB the completed year value is present. It is even calling the correct ID of the data and the completedday, completedmonth. It shows null only for the year that too only for older year but not for the latest years.
virusstorm 13-May-15 10:37am    
You checked the DB, but did you check the control itself? You may be in a situation where the control doesn't have the values loaded yet and you are trying to assign it a value that doesn't exists yet.
user 3008 14-May-15 10:20am    
If the value is not loaded in the control then it will enter into the statement 'if(SelectedSurveyID.SurveyDate!=null){' The control checks the condition and finds that it is not null and thus it enters into the if statement. If I am not correct can you please let me know how to in control itself
virusstorm 14-May-15 10:26am    
Look at the contents of your control "uxCompletedDateYear". If it doesn't have the value you are trying to set, it could be causing an issue. Ultimately, what you need to do is step through the code and use the immediate window to validate that the data you are getting back is accurate and the data you are trying to bind to your controls is in there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900