We help IT Professionals succeed at work.

bind to formview in drop down menu

fwsteal asked
Last Modified: 2008-04-24
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") %>'>

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;
        SqlCommand cmd = new SqlCommand("uspGetClientById", new SqlConnection(myConn));
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("CID", authID);
        FormView1.DataSource = cmd.ExecuteReader();
Watch Question

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>

or by assigning data source to the DataSource or DataSourceID property.


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?


i first loaded them in the insert aspx file like:

                              <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>


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?
This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.