ASP.NET Dropdownlist databinding error

  • Thread starter Thread starter Guest 20221009
  • Start date Start date
G

Guest 20221009

Guest
I am have two Drop downs both bound to the database through SqlDataSource controls. The first DDL contains countries and the second DDL's values depend on DDL1's selected item.

The first time, everything loads fine, but on postback fired from DDL1 I get the following error:

'DDL2' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

I know I can use AppendDataBoundItems="true", but this is not ideal since I need my list to clear old items with the new stuff. Setting viewstate to false for the control does not help either.

Its driving me nuts, and the suggested solutions on the interwebs are not coming to my party. :(:(

Anybody here ever solved such a case?
 
Last edited by a moderator:
<asp:DropDownList ID="DropDownList2"
runat="server"
AutoPostBack="true"
DataSourceID="SqlDataSource1"
DataTextField="CategoryDesc"
DataValueField="CategoryID" OnDataBound="D_Bind" OnSelectedIndexChanged="Load_D" AppendDataBoundItems="true" >
<asp:ListItem Text="Select Instrument" Value="0" />
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ blah%>"
SelectCommand="usp_Select_Instruments"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="instrument" Name="Action" Type="String" />
<asp:SessionParameter DefaultValue="1" Name="CountryID" SessionField="countryid" />
</SelectParameters>
</asp:SqlDataSource>


<asp:DropDownList ID="DropDownList1"
runat="server"
AutoPostBack="true"
DataSourceID="SqlDataSource1"
DataTextField="CountryDesc"
DataValueField="CountryID"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ blah%>"
SelectCommand="usp_Read_UserAccess"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="countryaccess" Name="Action" Type="String" />
<asp:SessionParameter DefaultValue="0" Name="UserUID" SessionField="uid"
Type="String" />
<asp:Parameter DefaultValue="&quot;&quot;" Name="UserPWD" Type="String" />
<asp:Parameter DefaultValue="&quot;&quot;" Name="UserEmail" Type="String" />
<asp:SessionParameter DefaultValue="1" Name="CountryID" SessionField="cid" />
</SelectParameters>
</asp:SqlDataSource>

I am out of ideas.
 
changes are that your post back is causing a data bound on the first dropdown and the second one is trying to do a databound but the first control does not contain data.

step through your code and see which event is being executed first...
 
I prefer not to work with data bindings like that anymore. MVC (not necessarily ASP.NET MVC, just implementations of the methodology) with proper objects is the way of the future. I get frustrated if I have to work otherwise.

If this is a new project you're starting out on, see if you can do it in ASP.NET MVC rather. After working through the tutorial you should be good to go.
 
I prefer not to work with data bindings like that anymore. MVC (not necessarily ASP.NET MVC, just implementations of the methodology) with proper objects is the way of the future. I get frustrated if I have to work otherwise.

If this is a new project you're starting out on, see if you can do it in ASP.NET MVC rather. After working through the tutorial you should be good to go.

This is a project that was already underway when I joined this company, so Webforms it is. I am soo over them. I wish I could wave my MVC wand at it :(
 
I prefer not to work with data bindings like that anymore. MVC (not necessarily ASP.NET MVC, just implementations of the methodology) with proper objects is the way of the future. I get frustrated if I have to work otherwise.

If this is a new project you're starting out on, see if you can do it in ASP.NET MVC rather. After working through the tutorial you should be good to go.
I recently had to work on a project that didn't have any code behind at all. All SqlDataSource for all the web form controls that had to be bound. Also another sad part was that the sql statements were embedded on those SqlDataSource controls - no use of stored procs. It was painful. So we had to create DALs, BLLs, to separate all code it from the GUIs.
 
This is a project that was already underway when I joined this company, so Webforms it is. I am soo over them. I wish I could wave my MVC wand at it :(

Look at the SelectedValue vs SlelectedIndex or something
 
Finally solved the issue through ridiculous hacks and moving ascx controls around. Why the hell does the page load event fire before the dropdownlist's onselectedindexchanged event, event though the ddl triggered the postback??
 
Finally solved the issue through ridiculous hacks and moving ascx controls around. Why the hell does the page load event fire before the dropdownlist's onselectedindexchanged event, event though the ddl triggered the postback??

...because that's the Page Life-cycle in ASP.NET. Read up about it a bit and you'll see which events get triggered in what order. The event get's called based on the value of the "__EVENTTARGET" parameter that get's passed through the query string. Do yourself a favour and put a breakpoint on the first line of code in your Page_Load method and examine the Request.Params dictionary. You'll see all the parameters that gets passed through on each request. Surprised that there are so many? :p
 
Top
Sign up to the MyBroadband newsletter
X