Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is there a simple way to test for a value in the options
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
OS:
Windows XP
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01331284
Message ID:
01331475
Views:
9
This message has been marked as a message which has helped to the initial question of the thread.
>>>Hi everybody,
>>>
>>>I'm getting mycontrols "has a SelectedValue which is invalid because it does not exist in the list of items" error. I know how to fix this for the particular value I'm trying to pass, but is there a generic way to check if the value you want to assign a selectedvalue is in the list of Items?
>>>
>>>Thanks a lot in advance.
>>
>>What control is it (base class)? Could you post some code?
>
>DropDownList (Web Forms).
>
>In my UserControl I have (I added a check for empty value)
>
>[Bindable(true, BindingDirection.TwoWay), Category("User-Defined"), DefaultValue(""), Description("Payment Method") ]
>    public string PaymentMethod
>    {
>        get
>        {
>            return this.ddlPaymentMethod.SelectedValue;
>        }
>        set
>        {
>            if (value !="") // Here we have error if value is not in the list
>                this.ddlPaymentMethod.SelectedValue = value;
>        }
>    }
When I try something very similar to what you have I do not get an exception if the item is not in the list.
Here is the code I was playing around with:
try
			{
				string id = "3";
				if (((DataSet1.DataTable1DataTable)this.comboBox2.DataSource).FindById(id)!=null)
				{
					this.comboBox2.SelectedValue = id;
				}
				else
				{
					MessageBox.Show("Not in combo");
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
I load the combo using this code:
this.comboBox2.DataSource = this.dataSet1.DataTable1;
			this.comboBox2.DisplayMember = "Name";
			this.comboBox2.ValueMember = "Id";
Semper ubi sub ubi.
Previous
Reply
Map
View

Click here to load this message in the networking platform