Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2189
  • Last Modified:

Get selected value from database to default on dropdown box

Hi have a dropdown box which needs to display either 0, 3 or 9 depending on what is selected in the database. I have it saving ok when someone submits the form, but when it reloads the form up, it doesnt default to the last selection, which i need it to do. The only problem is, its in a datagrid, so there are lots of these dropdown boxes.
Here is my code:-

<asp:TemplateColumn HeaderText="Score">
<ItemTemplate>
<asp:DropDownList id="Score" runat="server"  selectedindex='<%# DataBinder.Eval(Container.DataItem, "score") %>'>
<asp:ListItem Value="0">0</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>

the program runs a stored procedure which gets the value for score, but comes up with the error
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: value
pointing to :-
<asp:DropDownList id="Score" runat="server"  selectedindex='<%# DataBinder.Eval(Container.DataItem, "score") %>'>
Line 20:                                     <asp:ListItem Value="0">0</asp:ListItem>
Line 21:                                     <asp:ListItem Value="3">3</asp:ListItem>


any help appreciated!

0
richmorl
Asked:
richmorl
1 Solution
 
peterdunganCommented:
add this attribute to the dropdownlist:

Text='<%# Bind("field name goes here") %>'
0
 
Bob LearnedCommented:
It sounds like you aren't checking for Page.IsPostBack before loading data into the DropDownList.

Bob
0
 
satish_nagdevCommented:
make sure there is item in list corresponding to value you are getting from db, might be null is getting returned or so.

thanks.
satish
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now