Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > User Control Error : Object reference not set to an instance of an object.

Reply
Thread Tools

User Control Error : Object reference not set to an instance of an object.

 
 
gishani ratnayake via .NET 247
Guest
Posts: n/a
 
      03-11-2005
I am trying to get the selected value of a drop down list which is a user control. Can someone please tell me where I am going wrong in this code??

The error message is:

Object reference not set to an instance of an object.
get
Line 20: {
Line 21: return ucFoodCategory.Items[ucFoodCategory.SelectedIndex].Text;
Line 22: }



Here's my code
foodCategory.ascx

<SELECT id="ctlFoodCat" style="WIDTH: 160px" name="Category">
<OPTION value="" selected>Any</OPTION>
<OPTION value="Appetizers">Appetizers</OPTION>
<OPTION value="Beverages">Beverages</OPTION>
<OPTION value="Breads">Breads</OPTION>
<OPTION value="Condiments & Dips">Condiments & Dips</OPTION>
</SELECT>




using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class foodCategory : System.Web.UI.UserControl
{

protected System.Web.UI.HtmlControls.HtmlSelect ucFoodCategory;

public string SelectedText


{
get
{
return ucFoodCategory.Items [ucFoodCategory.SelectedIndex].Text;
}
}

}


.aspx code

<uc1:foodcategory id="ddlFoodCategory" runat="server"></uc1:foodcategory>

protected recipes.foodCategory ddlFoodCategory;


private void btnInsert_Click(object sender, System.EventArgs e)
{
lblfoodCategory.Text = ddlFoodCategory.SelectedText;
}

What am I missing ???

Thanks again.

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>+btCSyzsIkKvhyKRfdJxKg==</Id>
 
Reply With Quote
 
 
 
 
imsinc@gmail.com
Guest
Posts: n/a
 
      03-11-2005
Your message says Selected Value and your code is trying to pull the
Selected Text. Try this:

For Text:
ucFoodCategory.SelectedItem.Text;

For Value:
ucFoodCategory.SelectedValue;


Jason Bentley
http://geekswithblogs.net/jbentley

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Object reference not set to an instance of an object" Weird thing happens with reference a link nguyentrongkha@gmail.com ASP .Net 1 09-20-2007 09:46 PM
Dynamically loading user control into Placeholder gives Object reference not set to an instance of an object Phoenix ASP .Net Building Controls 2 06-06-2005 04:02 PM
Custom Control Problem :: Object reference not set to an instance of an object David Bowey ASP .Net Building Controls 1 03-16-2005 10:33 PM
Custom Control Problem :: Object reference not set to an instance of an object David Bowey ASP .Net Web Controls 1 03-16-2005 10:33 PM
DropDownList control - Object reference not set to an instance of an object egi ASP .Net Web Controls 1 08-03-2004 11:32 AM



Advertisments