Click here to Skip to main content
15,885,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when the database is empty gives this eror when retrieving data from db into dropdownlist.

Exception Details: System.ArgumentOutOfRangeException: 'dropdownlist1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

i have tried this one:
if (dt.Rows[rowIndex]["Zone"].ToString() == null || dt.Rows[rowIndex]["Zone"].ToString() == "")

                           dropdownlist1.SelectedValue = null;
                       else
                          dropdownlist1.SelectedValue = dt.Rows[rowIndex]["Zone"].ToString();

BUT the problem is that it takes in this case the first value of the datable, i have bind the dropdownlist...
HOW CAN I LEAVE IT EMPTY WHEN THERE IS NO DATA??
Posted

Would it be a solution for you to just add an empty item to the dropdownlist, and then select it?

something like:

C#
dropdownlist1.Items.Insert(0, "");
dropdownlist1.SelectedIndex(0);
 
Share this answer
 
v2
You need to set the option only if there is any record in dropdown. Try:
SQL
if (dt.Rows.Count > 0)
{
  // Your logic regarding data - setting control values , etc
  ...
  ...
  dropdownlist1.SelectedValue = dt.Rows[rowIndex]["Zone"].ToString();
  ...
  ...
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900