Visual Studio .NET - fill the dropdownlist dynamically

Asked By Gurmeet Singh on 18-Aug-06 01:19 AM
hello frnds,

i got Exception: "Object reference not set to an instance of an object" while selecting particular item in dropdownlist by using findbytext method in selected index change event of listbox.

Plz help me out.

Thanks
Gurmeet

Object reference not set to an instance of an obj - Asked By J S on 18-Aug-06 01:57 AM

First of all, this dropdownlist control needs databinding. Check with your control properties, if you are setting them right. Check if you are setting the table property to a valid table reference.

Next, On a computer that has Microsoft .NET Framework 1.0 installed (and that does not have Microsoft Visual Studio .NET installed), when you try to access data using ADO Interop on an ASP.NET page that resides at an identical site and that is hosted on the same computer, you may receive the following error message:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Look here for details:
http://support.microsoft.com/default.aspx?scid=kb;en-us;810098

As another reason, the control might just not be available to your compiler.If you are using the OnItemDataBound event, you have to check first whether the ListItemType is Item or AlternatingItem. You get the error because the OnItemDataBound event is first executed for the HeaderTemplate, which doesn't contain your control. 
Use something like this:
Sub DataList1_ItemDataBound(sender As Object, _
e As DataListItemEventArgs)
If(e.Item.ItemType=ListItemType.Item Or _
e.Item.ItemType=ListItemType.AlternatingItem) Then
' your code here
End If

Specifically for web apps, session, by default, is set to be placed as cookie. All I had to do is to set cookieless="true" in web.config

Code Please - Asked By Chandu . on 18-Aug-06 08:05 AM

Hi Gurumeet,
Is it possible for you to send the code blocks where you are binding the DropDownList, and handling the Select Index Changed event.. so that we can understand it more better.
Thanks

fill dropdownlist dynamically - Asked By Gurmeet Singh on 18-Aug-06 08:58 AM

hi,
i m binding dropdownlist in pageload event.
and on selected index change event of listbox, i am try to select the particular item by using findbytext method as follows:

string temp=dr["state"].ToString();
dstate.Items.FindByText(temp).Selected=true;

when control goes at "dstate.Items.FindByText(temp).Selected=true;" it through an exception that "Object reference not set to an instance of an object".

One more thing :
1st time it runs ok, but 2nd time it throughs an exception

Thanks
Gurmeet
Returning a Null Value - Asked By Chandu . on 18-Aug-06 09:07 AM
Hi Gurumeet,
From the scenario it is clear that the dstate.Items.FindByText(temp) is returning a null value.
Can you verify (through debugging) what is the value of temp and if this entry exists in the DropDown?
Also are your binding the DropDownList evrytime the PageLoads or is it only if it is not post back?
fill dropdown list dynamically - Asked By Gurmeet Singh on 18-Aug-06 09:18 AM
hi 
value of temp is ok and this value exists in drop down list also.
i bind dropdownlist only if it is not post back.

thanks

gurmeet
No Other Possible way - Asked By Chandu . on 18-Aug-06 09:21 AM
Hi Gurumeet,
In that case I cannot see any other possible way the exception can be thrown.
Is the code confidential?
If not can you post the code.
Thanks.
Asked By Gurmeet Singh on 18-Aug-06 09:27 AM
hi,

can u give me ur mail id, bcoz i can send code on that

thanks

gurmeet
My Mail Id - Asked By Chandu . on 18-Aug-06 09:37 AM
Hi,
You can reach me at Chandra.Vedantham@gmail.com .
Remove the drpstate ClearSelection(); - Asked By Chandu . on 18-Aug-06 09:55 AM
Hi Gurumeet,
Remove the drpstate.ClearSelection() method and rest all should be fine.
Also check if the EnableViewState property of the DropDOwnList is not set to false accidentally.
Let me know for further issues.
fill the dropdownlist dynamically - Asked By Byock Encarnacion on 30-Aug-06 02:15 AM
Check if this is missing in the declaration part

    Protected WithEvents dropdownlist1 As System.Web.UI.WebControls.DropDownList

If it is...add it...

Then

 dropdownlist1 .DataSource = thisIsaDataReader
 dropdownlist1 .DataTextField = "ProductName"
 dropdownlist1 .DataValueField = "ProductID"
 dropdownlist1 .DataBind()
fill the dropdownlist dynamically - Asked By Byock Encarnacion on 30-Aug-06 02:25 AM
Retrieving the selected item in a dropdownlist which resides in the same page, is pretty straight forward dropdownlist1.selecteditem.value.

However, when it is in a usercontrol, its pretty difficult to retrieve it since you will get the "Object Reference not set to an instance" error if you try the normal method. Even if you create an instance of the usercontrol and try to access its dropdownlist, you will get the above error.

The following code demonstrates how to overcome this problem.

UserControl objCtrl = ((UserControl)(FindControl("Id of the Usercontrol as declared in the container page")));

DropDownList ddl = ((DropDownList)(objCtrl.FindControl("Id of the Dropdownlist control in the usercontrol")));

Response.Write(ddl.SelectedItem.Value);

Thats it and you can access the value.
Case seems to be the problem - Riz G replied to Gurmeet Singh on 12-Jan-08 09:44 AM

I found that the comparison is case sensitive. Easy enough to chage case of temp like temp.ToUpper( ), but how can case of text to find in list be set to upper?

// Code does not execute if case of temp doesn't match case of item in list
if
(temp.Length > 0 && drpstate.Items.FindByText(temp.ToString()).ToUpper()!= null)
{
      drpstate.ClearSelection(); // if only one item is allowed to be selected
      drpstate.Items.FindByText(temp.ToString().ToUpper()).Selected =
true;
}

Here's the workaround

foreach (ListItem drListItem in drpstate.Items)
{
      // If there's a match, select the item in the list
      if (drListItem.Text.ToUpper() == temp.ToString().ToUpper())
      {
            drpstate.ClearSelection(); // if only one item is allowed to be selected
            drListItem.Selected =
true;
            break; // No need to check the rest of the items, so break out of the loop
      }
}