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

Discussion in 'ASP .Net Web Controls' started by gishani ratnayake via .NET 247, Mar 11, 2005.

  1. 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>
     
    gishani ratnayake via .NET 247, Mar 11, 2005
    #1
    1. Advertising

  2. gishani ratnayake via .NET 247

    Guest

    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
     
    , Mar 11, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    514
    Mark Rae [MVP]
    Sep 20, 2007
  2. David Bowey
    Replies:
    1
    Views:
    261
    David Bowey
    Mar 16, 2005
  3. Phoenix
    Replies:
    2
    Views:
    257
    Teemu Keiski
    Jun 6, 2005
  4. egi
    Replies:
    1
    Views:
    222
  5. David Bowey
    Replies:
    1
    Views:
    357
    David Bowey
    Mar 16, 2005
Loading...

Share This Page