0

I am getting the below error when I try to bind data.

SelectedValue which is invalid because it does not exist in the list of items

<asp:DropDownList ID="DropDownList2" runat="server" 
     DataSourceID="JobCategoryEntityDataSource" DataTextField="ItemValue"
     DataValueField="ItemValue" SelectedValue='<%# Bind("JobCategory") %>' 
     Width="230px">
</asp:DropDownList>

I know the reason why I am getting this error. It is because it cant find the value in the selection. When JohCategory field from the transaction table can not find it in the list that is coming from JobCategoryEntityDataSource, it simply throws an error.

All I want to do is not bind the data if it can't find it in the dropdownlist and don't want to throw errors. How can I do this ?

Another option is if it can't find the matching value, just add "Please select" or something similar.

Can someone help please ?

rs.
  • 26,707
  • 12
  • 68
  • 90
BenW
  • 1,393
  • 1
  • 16
  • 26

2 Answers2

1

You can select the value in DataBound event of the dropdown. You can check for the existence of value and then select it or you can implement try catch

<asp:DropDownList ID="DropDownList2" runat="server"
 DataSourceID="JobCategoryEntityDataSource" DataTextField="ItemValue"
 DataValueField="ItemValue" 
 Width="230px" OnDataBound="DropDownList2_DataBound">
</asp:DropDownList>

<asp:HiddenField ID="JobCategory" runat="server" value='<%# Bind("JobCategory") %>'></asp:HiddenField>

Code-Behind

protected void DropDownList2_DataBound(object sender, EventArgs e)  
{  
    try{
       HiddenField hf = (HiddenField)Form.FindControl("JobCategory");

       DropDownList2.SelectedValue = hf.Value;

    }Catch(Exception ex){

    }
}  
Hary
  • 5,690
  • 7
  • 42
  • 79
  • can you please tell me where exactly I have to put the try catch ? my dropdownlist is inside a detailsview control and not sure where to put the code. Thanks – BenW Jan 27 '13 at 10:18
  • Hi, Thanks. Now I am able to handle the error without any issue. But there is another problem. Because there is no binding in the dropdownlist, it does not save the value in the database. Any idea ? – BenW Jan 28 '13 at 04:52
  • @user1833408 "Because there is no binding in the dropdownlist," What does it meant? – Hary Jan 28 '13 at 05:11
  • In the oroginal code, it has SelectedValue='<%# Bind("JobCategory") %> for the dropdownlist. To implement your suggested code, I had to remove it from the code. But now when I select a value from the dropdownlist and save the record, it does not save the value in the database. I assume this is because no binding for the droudownlist control. I am bit new to this and sorry that I may be wrong. – BenW Jan 29 '13 at 02:35
  • Also I had to do some modifications to your code because DropDownList2.SelectedValue = hf.Value; line give an error saying it cant find the DropDownList2. so I changed to DropDownList2 dl = (DropDownList)Form.FindControl("DropDownList2"); dl.SelectedValue = hf.Value; and hope this would not make any harm. – BenW Jan 29 '13 at 02:39
0

You need to bind it in the code behind and either

  • Wrap the statement in a try/catch block
  • Check the item is there with FindByName first, then, optionally, add it if not.
Echilon
  • 10,064
  • 33
  • 131
  • 217
  • can you please tell me where exactly I have to put the try catch ? my dropdownlist is inside a detailsview control and not sure where to put the code. Thanks – BenW Jan 27 '13 at 10:19