fwsteal
asked on
bind to formview in drop down menu
I'm trying to bind to a drop down menu in a form view and can't seem to get it to work. Using 2.0 c#.
<asp:TextBox ID="txtState" runat="server" Text='<%# Eval("State") %>'></asp:TextBox>
show's my state; however I want it selected in a drop down menu.
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Eval("State") %>'>
</asp:DropDownList>
The above gives me the error below:
'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
here is my code in the codebehind:
private void BindEDetails(string authID)
{
//bind data to details
FormView1.Visible = true;
//sql
SqlCommand cmd = new SqlCommand("uspGetClientBy Id", new SqlConnection(myConn));
cmd.CommandType = System.Data.CommandType.St oredProced ure;
cmd.Parameters.AddWithValu e("CID", authID);
cmd.Connection.Open();
FormView1.DataSource = cmd.ExecuteReader();
FormView1.DataBind();
cmd.Connection.Close();
cmd.Connection.Dispose();
}
<asp:TextBox ID="txtState" runat="server" Text='<%# Eval("State") %>'></asp:TextBox>
show's my state; however I want it selected in a drop down menu.
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Eval("State") %>'>
</asp:DropDownList>
The above gives me the error below:
'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
here is my code in the codebehind:
private void BindEDetails(string authID)
{
//bind data to details
FormView1.Visible = true;
//sql
SqlCommand cmd = new SqlCommand("uspGetClientBy
cmd.CommandType = System.Data.CommandType.St
cmd.Parameters.AddWithValu
cmd.Connection.Open();
FormView1.DataSource = cmd.ExecuteReader();
FormView1.DataBind();
cmd.Connection.Close();
cmd.Connection.Dispose();
}
ASKER
they are not static as I have a table that contains the states:
States Table:
[StateId] [int] IDENTITY(1,1) NOT NULL,
[StateCode] [char](2) COLLATE SQL_Latin1_General_CP1_CI_ AS NOT NULL,
[StateName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_ AS NOT NULL,
[TimeStamp] [datetime] NOT NULL,
how would I do that in the code behind?
States Table:
[StateId] [int] IDENTITY(1,1) NOT NULL,
[StateCode] [char](2) COLLATE SQL_Latin1_General_CP1_CI_
[StateName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_
[TimeStamp] [datetime] NOT NULL,
how would I do that in the code behind?
ASKER
i first loaded them in the insert aspx file like:
<tr>
<td>State</td>
<td style="width: 212px">
<asp:DropDownList ID="ddlState" runat="server" DataSourceID="SqlDataSourc eStates"
DataTextField="StateName" DataValueField="StateCode" > </asp:DropDownList><asp:Sq lDataSourc e ID="SqlDataSourceStates" runat="server" ConnectionString="<%$ ConnectionStrings:Connecti onStringD %>"
SelectCommand="SELECT [StateCode], [StateName] FROM [States]"></asp:SqlDataSou rce>
</td>
</tr>
<tr>
<td>State</td>
<td style="width: 212px">
<asp:DropDownList ID="ddlState" runat="server" DataSourceID="SqlDataSourc
DataTextField="StateName" DataValueField="StateCode"
SelectCommand="SELECT [StateCode], [StateName] FROM [States]"></asp:SqlDataSou
</td>
</tr>
ASKER
i can do the same binding in the update formview but how to I default to the selected value per what's in the db table?
The look OK to me. Still getting error?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1" Text="Option 1"></asp:ListItem>
<asp:ListItem Value="2" Text="Option 2"></asp:ListItem>
<asp:ListItem Value="3" Text="Option 3"></asp:ListItem>
<asp:ListItem Value="4" Text="Option 4"></asp:ListItem>
</asp:DropDownList>
or by assigning data source to the DataSource or DataSourceID property.