Link to home
Start Free TrialLog in
Avatar of fwsteal
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("uspGetClientById", new SqlConnection(myConn));
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("CID", authID);
        cmd.Connection.Open();
        FormView1.DataSource = cmd.ExecuteReader();
        FormView1.DataBind();
        cmd.Connection.Close();
        cmd.Connection.Dispose();
    }
Avatar of Edwin_C
Edwin_C
Flag of Hong Kong image

This is because your dropdown is empty.  You need to populate items in your dropdown either by hard coding in the aspx (if the items are static)

            <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.
Avatar of fwsteal
fwsteal

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?
Avatar of fwsteal

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="SqlDataSourceStates"
                                DataTextField="StateName" DataValueField="StateCode">                                  </asp:DropDownList><asp:SqlDataSource ID="SqlDataSourceStates" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringD %>"
                                SelectCommand="SELECT [StateCode], [StateName] FROM [States]"></asp:SqlDataSource>
                                 &nbsp;</td>
                            </tr>
Avatar of fwsteal

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
Avatar of Edwin_C
Edwin_C
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial