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

  • Thread starter gishani ratnayake via .NET 247
  • Start date
G

gishani ratnayake via .NET 247

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top