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">
<title></title>
<%-- <link href="~/Content/StyleSheet1.css" rel="stylesheet" type="text/css" />--%>
</head>
<body>
<form id="form1" runat="server">
<div >
</div>
<div>
<%-- <asp:GridView ID="Gv_TeamsView" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="ID" GridLines="None" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Lbl_Banner" runat="server" Text="Select A Team:" />
<asp:DropDownList ID="Ddl_Teams" runat="server" DataTextField='<%# Eval("TeamName") %>' DataValueField='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>--%>
<h3><u>Player Search</u></h3>
<table class="tbl">
<tr>
<td>
<asp:Label ID="Lbl_SelectTeam" runat="server" Text="Select a Team" />
</td>
<td>
<asp:DropDownList ID="Ddl_Teams" runat="server" />
</td>
</tr>
<tr></tr><tr></tr>
<tr>
<td></td>
<td>
<asp:Button ID="Btn_SubmitTeam" runat="server" Text="Select" OnClick="TeamPage_Click" />
</td>
</tr>
</table>
<%-- <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="SELECT * FROM FB_Teams ORDER BY TeamName" />--%>
</div>
</form>
</body>
</html>
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)
{
Ddl_Teams.ClearSelection();
FillList();
}
}
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);
sda.Fill(ds);
Ddl_Teams.DataSource = ds;
Ddl_Teams.DataTextField = "TeamName";
Ddl_Teams.SelectedValue = "ID";
Ddl_Teams.DataBind();
}
}
protected void TeamPage_Click(object sender, EventArgs e)
{
if (Request.QueryString["ID"] == null)
{
Response.Redirect("Default.aspx");
}
String id = Request.QueryString["ID"];
String url = String.Format("~/TeamPage.aspx?ID={0}", id);
Response.Redirect(url);
}
}
}
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.