0

Hi,
I want to display a text, getting from database, into a DropdownList using text property.

My Code::

SqlDataReader drd; 
if (drd.Read())
{
	drpcompname.Text  = drd.GetValue(0).ToString();

}

But It Display an error::

'drpcompname' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

Please help me.

Thanks!
Pankaj Singh

3
Contributors
10
Replies
11
Views
7 Years
Discussion Span
Last Post by Ramesh S
0

Welcome Pankaj18,

Your source code must be surrounded with bb code tags. Read this How to use bb code tags?

Error message shows that a value you are assigned to the DropDownList control is not present inside the DropDownList's items.
Yes, the DropDownControl is empty. Isn't it?

Here, I am using bb code tags - See, how a source looks !!!

SqlDataReader drd; 
   .....
   if (drd.Read())
     {
        drpcompname..Items.Add(drd.GetValue(0).ToString());
     }
0

Welcome Pankaj18,

Your source code must be surrounded with bb code tags. Read this How to use bb code tags?

Error message shows that a value you are assigned to the DropDownList control is not present inside the DropDownList's items.
Yes, the DropDownControl is empty. Isn't it?

Here, I am using bb code tags - See, how a source looks !!!

SqlDataReader drd; 
   .....
   if (drd.Read())
     {
        drpcompname..Items.Add(drd.GetValue(0).ToString());
     }

I want to display at runtime not add

0

I want to display at runtime not add

drpcompname..Items.Add(drd.GetValue(0).ToString());
the above code add the items ,not display......

0

Does your dropdownlist contains items? If yes then use

drpcompname.SelectedValue=drd.GetValue(0).ToString();
0

Does your dropdownlist contains items? If yes then use

drpcompname.SelectedValue=drd.GetValue(0).ToString();

but it displays the same error::
drpclienttype' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

help me..

Pankaj Singh

0

Thanks.
But it working when dropdownlist have some values, i have another case when i have no items in dropdownlist,it have only one item 'Select One', it display same error::
'drpcompname' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

Thanks!

Pankaj Singh

0

If dropdownlist has no items (values) then you cannot assign a value for selection. This is the fact. Do not assign value if dropdown is empty.

0

You can handle this error

drpcompname.SelectedValue = drd.GetValue(0).ToString();

Replace the above statement with the following code

ListItem item = drpcompname.Items.FindByValue(drd.GetValue(0).ToString());
if (item != null)
        item.Selected = true;
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.