DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   ASP.NET (http://www.daniweb.com/forums/forum18.html)
-   -   Dropdownlist (http://www.daniweb.com/forums/thread201502.html)

Pankaj18 Jul 3rd, 2009 12:32 pm
Dropdownlist
 
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

adatapost Jul 3rd, 2009 12:40 pm
Re: Dropdownlist
 
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());
    }

Pankaj18 Jul 3rd, 2009 12:48 pm
Re: Dropdownlist
 
Quote:

Originally Posted by adatapost (Post 907613)
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

Pankaj18 Jul 3rd, 2009 12:49 pm
Re: Dropdownlist
 
Quote:

Originally Posted by Pankaj18 (Post 907619)
I want to display at runtime not add

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

adatapost Jul 3rd, 2009 12:51 pm
Re: Dropdownlist
 
Does your dropdownlist contains items? If yes then use
drpcompname.SelectedValue=drd.GetValue(0).ToString();

Pankaj18 Jul 4th, 2009 5:17 am
Re: Dropdownlist
 
Quote:

Originally Posted by adatapost (Post 907622)
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

adatapost Jul 4th, 2009 5:25 am
Re: Dropdownlist
 
First, fill the dropdownlist with values.

Pankaj18 Jul 4th, 2009 5:32 am
Re: Dropdownlist
 
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

adatapost Jul 4th, 2009 5:46 am
Re: Dropdownlist
 
Thanks,

Please, Mark this thread as Solved if you get solution.

adatapost Jul 4th, 2009 5:56 am
Re: Dropdownlist
 
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.


All times are GMT -4. The time now is 4:02 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC