Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 16-Jan-13 2:55am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Would it be a solution for you to just add an empty item to the dropdownlist, and then select it?
 
something like:
 
dropdownlist1.Items.Insert(0, "");
dropdownlist1.SelectedIndex(0);
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 547
1 ChauhanAjay 180
2 CHill60 135
3 Maciej Los 125
4 Richard Deeming 125
0 Sergey Alexandrovich Kryukov 9,735
1 OriginalGriff 6,987
2 CPallini 5,835
3 George Jonsson 4,017
4 Gihan Liyanage 3,266


Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100