Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 22nd, 2004, 03:15 PM
Authorized User
 
Join Date: Jun 2004
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Default Object Reference Problem

Hi folks: I just started to populate my dropdownlists dynamically and since then although the lists get populated, I get the following error when I click "AddToCart" process command:
Source Error:


Line 121: DropDownList ddlW = (DropDownList)e.Item.FindControl("ddlWidth");
Line 122:
Line 123: Server.Transfer("AddToCart.aspx?ProductID=" + e.CommandArgument.ToString()
Line 124: + "&shoecolor=" +
Line 125: ddlC.SelectedItem.Text +
Source File: c:\inetpub\wwwroot\perrys\shoedetails.aspx.cs Line: 123
_____________________
Here is my code file:

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here


            string query = "select * from Shoes where ShoeID='" +
                Request.QueryString [ "id" ] + "'";

            SqlConnection cnShoes = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
            SqlCommand cmdSelect = new SqlCommand(query, cnShoes);
            cnShoes.Open();
            SqlDataReader shoeReader = cmdSelect.ExecuteReader();
            if (shoeReader.Read() == true)
            {
                lblShoeName.Text = shoeReader["ShoeID"].ToString();
                if (lblShoeName.Text == "11")
                {

                    ddlColor.Items.Insert(0, "");
                    ddlColor.Items.Insert(1, "Black");
                    ddlSize.Items.Insert(0, "");
                    ddlSize.Items.Insert(1, "8");
                    ddlSize.Items.Insert(1, "8.5");

                }

            }
            cnShoes.Close();


            myData.DataSource = fetchReader ( query, "shoes");
            myData.DataBind();

        }

        private void processCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("AddToCart"))

            {

                DropDownList ddlC = (DropDownList)e.Item.FindControl("ddlColor");
                DropDownList ddlS = (DropDownList)e.Item.FindControl("ddlSize");
                DropDownList ddlW = (DropDownList)e.Item.FindControl("ddlWidth");

                Server.Transfer("AddToCart.aspx?ProductID=" + e.CommandArgument.ToString()
                    + "&shoecolor=" +
                    ddlC.SelectedItem.Text +
                    "&shoesize=" +
                    ddlS.SelectedItem.Text +
                    "&shoewidth=" + ddlW.SelectedItem.Text);
            }

        }
}
}
How do I go about rectifying this? Thanks.

 
Old July 22nd, 2004, 03:38 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It looks like this is caused by the way you add the items to your drop-down list.

Right now, ever time the page loads, you add your items. This means that when the page is posted back, the items are added again, clearing out the SelectedItem.

Wrap the code that adds the items in a !IsPostBack block:

if (!IsPostBack)
{
  // Add items here
}
else
{
  // Items have been added and should still be there.
}

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Don't Ever Get Tired by Tindersticks (Track 6 from the album: Can our love...) What's This?
 
Old July 22nd, 2004, 04:01 PM
Authorized User
 
Join Date: Jun 2004
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar. I got it working now -- I don't know about the page load event impacting this. I just took out the find control code as follows:
private void processCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("AddToCart"))

            {Server.Transfer("AddToCart.aspx?ProductID=" + e.CommandArgument.ToString()
                    + "&shoecolor=" + ddlColor.SelectedItem.Text
                     +
                    "&shoesize=" + ddlSize.SelectedItem.Text
                    +
                    "&shoewidth=" + ddlWidth.SelectedItem.Text);
            }

        }

 
Old July 22nd, 2004, 04:13 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Right, so the drop-down applies to the page, and to an individual DataGrid row?

In that case, it makes sense. FindControl is used to find a repeated control (as is the case with a control in a DataGridItem) by name. If you have just a drop-down in your page, you can reference it by its name directly...

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: My Friend Goo by Sonic Youth (Track 6 from the album: Goo) What's This?





Similar Threads
Thread Thread Starter Forum Replies Last Post
object reference not set to instant of an object shahidrasul ASP.NET 2.0 Basics 1 September 5th, 2008 02:01 PM
Object Reference not set to an instance of object srinivas_chakka ASP.NET 1.0 and 1.1 Professional 0 February 8th, 2006 11:56 AM
LNC Add-in object reference problem srichart BOOK: Expert One-on-One Access Application Development 2 July 8th, 2004 09:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.