Hi guys,

I'm defining my dropdownlist in my codebehind and it continues to crash when when I bind the dataset to the actual dropdownlist.

here is the aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_332_Final_Proj.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<%--    <link href="~/Content/StyleSheet1.css" rel="stylesheet" type="text/css" />--%>
    <form id="form1" runat="server">
        <div >

    <%--        <asp:GridView ID="Gv_TeamsView" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="ID" GridLines="None" >
                            <asp:Label ID="Lbl_Banner" runat="server" Text="Select A Team:" />
                            <asp:DropDownList ID="Ddl_Teams" runat="server" DataTextField='<%# Eval("TeamName") %>' DataValueField='<%# Eval("ID") %>' />
            <h3><u>Player Search</u></h3>
            <table class="tbl">
                        <asp:Label ID="Lbl_SelectTeam" runat="server" Text="Select a Team" />
                        <asp:DropDownList ID="Ddl_Teams" runat="server" />
                        <asp:Button ID="Btn_SubmitTeam" runat="server" Text="Select" OnClick="TeamPage_Click" />


<%--            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" 
                SelectCommand="SELECT * FROM FB_Teams ORDER BY TeamName" />--%>

and the corresponding codebehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace _332_Final_Proj
    public partial class WebForm1 : System.Web.UI.Page
        String constr = "Data Source=localhost;Initial Catalog=789;Integrated Security=True";

        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)

        protected void FillList()
            using (SqlConnection con = new SqlConnection(constr))
                DataSet ds = new DataSet();
                SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM FB_Teams ORDER BY TeamName",con);
                Ddl_Teams.DataSource = ds;
                Ddl_Teams.DataTextField = "TeamName";
                Ddl_Teams.SelectedValue = "ID";
        protected void TeamPage_Click(object sender, EventArgs e)
            if (Request.QueryString["ID"] == null)
            String id = Request.QueryString["ID"];
            String url = String.Format("~/TeamPage.aspx?ID={0}", id);

Whenever I launch the page it crashes on Ddl_Teams.DataBind() stating :

'Ddl_Teams' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: 'Ddl_Teams' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

Thanks for the help I appreciate it.

Recommended Answers

All 5 Replies

Ddl_Teams.SelectedValue = "ID";

IIRC the SelectedValue should be one of the team names. I don't think you have a team named "ID".

I think you mean to use "DataValueField" on line 34.

Ddl_Teams.DataValueField = "ID";

Thanks for the suggestions guys, I'll give them a try.

pritaeas and JorgeM,

Thanks for the pointer, it works now. My remaining question is when should I use DataValueField as opposed to SelectedValue? What is the difference or do they more or less serve the same purpose?

Thanks for your time and help

SelectedValue is used to show/read the single value currently visible/selected in the combo box.

That value is determined by DataValueField, which points to the column from your dataset.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.