invalid SelectedValue - does not exist in the list of items

hi Experts,

I get the following error on navigating to my Edit screen:
'DDLStartHrs' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value 

Open in new window


DDLStartHrs is populated in the page-load event as follows:
 if (!IsPostBack)
        {
             DDLStartHrs.Items.Add("--hh--");
                       List<string> hours = new List<string>();
            for (int hour = 0; hour < 24; hour++)
              
                hours.Add(string.Format("{0:00}", hour));
                     foreach (string child in hours)
            {
                DDLStartHrs.Items.Add(child);
                           }

}

Open in new window


I thought the problem lay with the data where the hours value was being returned as a single digit if it was <10 but having amended the sql to handle this and return all values as double digit I am now not sure what the problem is - for ref. the sql is below - you'll note the 'StartHrs' value is coming from a datetime value:

Select otID, CONVERT(DATE, startDateTime) As startDate, CONVERT(DATE, endDateTime) As endDate, CASE WHEN DATEPART(HOUR,startDateTime) <10 THEN  convert(VARCHAR(2),'0'+CONVERT(CHAR(1),DATEPART(HOUR,startDateTime))) ELSE CONVERT(VARCHAR(2),DATEPART(HOUR,startDateTime)) END AS startHrs, CASE WHEN DATEPART(HOUR,endDateTime) <10 THEN  convert(VARCHAR(2),'0'+CONVERT(CHAR(1),DATEPART(HOUR,endDateTime))) ELSE CONVERT(VARCHAR(2),DATEPART(HOUR,endDateTime)) END AS stopHrs, CASE WHEN DATEPART(MINUTE,startDateTime) <10 THEN  convert(VARCHAR(2),'0'+CONVERT(CHAR(1),DATEPART(MINUTE,startDateTime))) ELSE CONVERT(VARCHAR(2),DATEPART(MINUTE,startDateTime)) END As startMins, CASE WHEN DATEPART(MINUTE,endDateTime) <10 THEN  convert(VARCHAR(2),'0'+CONVERT(CHAR(1),DATEPART(MINUTE,endDateTime))) ELSE CONVERT(VARCHAR(2),DATEPART(MINUTE,endDateTime)) END  AS stopMins, otDuration, otDurationRounded, otDurationDecimal, Rate, ClientNumber, MatterNumber, selectedReason, otReason, ChkClientMatter, preAuthorised from Overtime where otID = @otID

Open in new window


for completeness this is the code binding the selectedvalue in the dropdownlist;

 <asp:DropDownList CssClass="dropdownnarrow" ID="DDLStartHrs" AutoPostBack="true" onchange="TimeDuration()" SelectedValue='<%#Bind("startHrs") %>' AppendDataBoundItems="true" Width="40" runat="server">
                                                </asp:DropDownList>

Open in new window


if anyone can advise that'd be great - thanks in advance
forstersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
did you select an item for the dropdownlist DDLStartHrs in your script?

or did you use DDLStartHrs.FindByValue method in somewhere in your script?
0
forstersAuthor Commented:
hi,

if I've understood the question, neither. The selected value is being stored in sql so on loading the page I am running the sql select to get that value and trying to bind it to the drop down as follows:

SelectedValue='<%#Bind("startHrs") %>' this is set within the <asp:DropDownList> tags which you can see in my original posting.
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
can you first remove the "SelectedValue='<%#Bind("startHrs") %>'" from your code, and see what's the values generated by the script itself?

and see if it will match to the field "startHrs" values in your query?

Select otID, CONVERT(DATE, startDateTime) As startDate, CONVERT(DATE, endDateTime) As endDate, CASE WHEN DATEPART(HOUR,startDateTime) <10 THEN  convert(VARCHAR(2),'0'+CONVERT(CHAR(1),DATEPART(HOUR,startDateTime))) ELSE CONVERT(VARCHAR(2),DATEPART(HOUR,startDateTime)) END AS startHrs, CASE WHEN DATEPART(HOUR,endDateTime) <10 THEN  convert(VARCHAR(2),'0'+CONVERT(CHAR(1),DATEPART(HOUR,endDateTime))) ELSE CONVERT(VARCHAR(2),DATEPART(HOUR,endDateTime)) END AS stopHrs, CASE WHEN DATEPART(MINUTE,startDateTime) <10 THEN  convert(VARCHAR(2),'0'+CONVERT(CHAR(1),DATEPART(MINUTE,startDateTime))) ELSE CONVERT(VARCHAR(2),DATEPART(MINUTE,startDateTime)) END As startMins, CASE WHEN DATEPART(MINUTE,endDateTime) <10 THEN  convert(VARCHAR(2),'0'+CONVERT(CHAR(1),DATEPART(MINUTE,endDateTime))) ELSE CONVERT(VARCHAR(2),DATEPART(MINUTE,endDateTime)) END  AS stopMins, otDuration, otDurationRounded, otDurationDecimal, Rate, ClientNumber, MatterNumber, selectedReason, otReason, ChkClientMatter, preAuthorised from Overtime where otID = @otID

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
forstersAuthor Commented:
so without setting the selected value the dropdown loads with no value selected and lists hours as follows: 00, 01,02...through to 23, the top and only visible item in the dropdown on page load is
'--hh--'
the sql returns a value of 09 in this instance
0
forstersAuthor Commented:
this turns out to be a known issue - we used a workaround involving a bunch of labels - not ideal but functions! many thanks for trying to assist
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.