i try to fetch the data from the database and display text in the dropdownlist

i got the error message

'dropdownlist' has a selectedvalue which is invalid because it does not exist in the list of items parameter name value

when selectedvalue does not exist in the list of items i want the selectedindex is 0 or selectedvalue is "-- Select --".
pls. help me in this .

tnx in advance;

Hello, it would be a bit confusing if someone provides their own code for this as you may face problems again when integrating back in your application so it would be great if you could possibly post in your code here so as to get a quick reply and also understand the error reason you are facing.

Before fetching a data from the database do this:

ComboBox1.Items.Add("-- Select --");

And lastly, if there's no data on the database do this:

ComboBox1.SelectedIndex = 0;

Edited 2 Years Ago by Ferdinand_1: Enhancement

Set your combobox Text property to the default value you want to display.

If the selected index doesn't exist in the DB, set your SelectedIndex on the combo box to -1. This will revert it to your default text.

Edited 2 Years Ago by Ketsuekiame

Ketsuekiame:
It seems that you dont understand what the thread starter is asking.

What he wants is when selectedvalue does not exist in the list of items he want the selectedindex = 0 or selectedvalue is "-- Select --".

So the best way to do that is:

Before fetching a data from the database do this:

  1. ComboBox1.Items.Add("-- Select --");
  2. Fetch the data from the database.
  3. No data from database then, ComboBox1.SelectedIndex = 0;

Simple,am I right?

Set your combobox Text property to the default value you want to display.
If the selected index doesn't exist in the DB, set your SelectedIndex on the combo box to -1. This will revert it to your default text.

I don't get this behaviour when testing, the box remains blank after setting the index to -1 from another value (VS2012/.Net4.5).

I disagree with the downvote though, it is a better route than manually adding an item.

Edited 2 Years Ago by Mike Askew

sorry for posting late of my code. but thanks for all the responses .

here's my code:

Code for binding dropdownlist:

        private void ddlBUCode()
        {
            foreach (GridViewRow row in gvw.Rows)
            {
                DropDownList ddlBU = (DropDownList)row.FindControl("ddlBU");
                DropDownList ddlDiv = (DropDownList)row.FindControl("ddlDiv");

                //bind BU

                DataTable dtBU = maDDL.getBUlist();

                ddlBU.DataSource = dtBU;
                ddlBU.DataTextField = "codeDesc";
                ddlBU.DataValueField = "code";
                ddlBU.DataBind();
                ddlBU.Items.Insert(0, new ListItem("- Select -", "0"));

            }
        }

and code for my gridview:

<asp:GridView ID="gvw" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="Both" Visible="false" 
    Height="184px" AllowPaging="True" Font-Bold="False" Font-Size="9pt" Font-Names="Arial" PageSize="10" OnSelectedIndexChanged="gvw_SelectedIndexChanged"
     OnRowDataBound="gvw_RowDataBound" DataKeyNames="template_id"  EmptyDataText="No record(s) found." OnPageIndexChanging="gvw_PageIndexChanging">
    <AlternatingRowStyle BackColor="White" />
    <Columns>

        <asp:TemplateField HeaderText="Business Unit" HeaderStyle-Width="100px" HeaderStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:DropDownList ID="ddlBU" runat="server" AutoPostBack="true"  OnSelectedIndexChanged="ddlBU_SelectedIndexChanged" >
                </asp:DropDownList>

            <asp:Label ID="maid" runat="Server" Text='<%# Eval("template_id") %>' ></asp:Label>
            </ItemTemplate>

            <HeaderStyle HorizontalAlign="Left" Width="150px"></HeaderStyle>
            <ItemStyle HorizontalAlign="Left"></ItemStyle>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Division" HeaderStyle-Width="100px" HeaderStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:DropDownList ID="ddlDiv" runat="server" AutoPostBack="true" >
                </asp:DropDownList>
            </ItemTemplate>

            <HeaderStyle HorizontalAlign="Left" Width="150px"></HeaderStyle>
            <ItemStyle HorizontalAlign="Left"></ItemStyle>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Effective Date" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="40">
            <ItemTemplate> 
                <SharePoint:DateTimeControl ID="effective_dates" runat="server" DateOnly="true"
                    CssClassTextBox="ms-input" AutoPostBack="true"  />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#0F5E8E" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#0F5E8E" ForeColor="White" HorizontalAlign="Center" Font-Bold="true"  />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    <SelectedRowStyle BackColor="LawnGreen" ForeColor="White" />

</asp:GridView>

and for Page Load:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView1();
                ddlBUCode();
            }
        }

Edited 2 Years Ago by </scorpion>

Ketsuekiame:
It seems that you dont understand what the thread starter is asking.

What he wants is when selectedvalue does not exist in the list of items he want the selectedindex = 0 or selectedvalue is "-- Select --".

So the best way to do that is:

Before fetching a data from the database do this:

ComboBox1.Items.Add("-- Select --");
Fetch the data from the database.
No data from database then, ComboBox1.SelectedIndex = 0;
Simple,am I right?

I understood perfectly what he said; unfortunately your solution isn't viable as the OP is using databinding. You cannot modify the list after it is bound. So, the only way would be to insert it into the data items before the control gets bound.

The other problem with your method is that you're treating a valid selection as in in-valid one. An index of 0 is a perfectly valid selection. If you're trying to deselect an item, you must set the SelectedIndex to -1.

From the MSDN:

To deselect the currently selected item, set the SelectedIndex to -1.

This unsets the selected item and invalidates the control. The fact that the control is no longer valid is important is ASP.NET validation.

Unfortunately, my way doesn't work either. As the list is databound, you cannot set the Text property of the ComboBox. It is unset (reset) and made read-only.

The only way to get what the OP wants, is to dirty the actual data, or simply pre-process it and remove anything that can't be selected anyway.

In either case, to unselect an item, you must set the index to -1.

Comments
This

thanks to all of you. i've already got the answer.
i used rowdatabound in gridview to show the selectedvalue or selectedindex =0/select in dropdownlist . :D

This question has already been answered. Start a new discussion instead.