Link to home
Start Free TrialLog in
Avatar of techques
techques

asked on

SelectedValue which is invalid because it does not exist in the list of items.\r\nParameter name: value" string

Hi

I use the following code to bind data in a drop down list in a aspx with c#, but it throws error:

Message      "'currency' has a SelectedValue which is invalid because it does not exist in the list of items.\r\nParameter name: value"      string

How should I fix it?
<asp:DropDownList Runat="server" ID="currency" CssClass="form" Width=60  />
 
private void Bind_Currency()
        {
            string strSql = "SELECT id,currency from currency order by id";
            DataSet ds = new DataSet();
            try
            {
                ds = SqlHelper.ExecuteDataset(DBConnection.ConnString, CommandType.Text, strSql);
            }
            catch (Exception)
            {
                Response.Redirect("../error.aspx", true);
            }
            this.currency.DataSource = ds.Tables[0].DefaultView;
            this.currency.DataTextField = "currency";
            this.currency.DataValueField = "currency";
            try
            {
                this.currency.DataBind(); //throw exception
            }
            catch (Exception e)
            {
                string r = e.ToString();
            }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India 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
Avatar of techques
techques

ASKER

yes, it is working

this.currency.DataSource = ds.Tables[0].DefaultView;
            this.currency.DataTextField = "id";
            this.currency.DataValueField = "id";
ten check whether you are getting data in currency filed
may be it is returning null data so it having problems
it is very strange

i added break point to view it line by line

this.currency.DataTextField = "currency";
this.currency.DataValueField = "currency";

They can get value in first time of loading, but I do not know why it loads the 2nd time and it became null.

I found the error caused by the sequence of the function executed in page load.