Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 22nd, 2004, 03:15 PM
Authorized User
 
Join Date: Jun 2004
Location: , , USA.
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.

Reply With Quote
  #2 (permalink)  
Old July 22nd, 2004, 03:38 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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?
Reply With Quote
  #3 (permalink)  
Old July 22nd, 2004, 04:01 PM
Authorized User
 
Join Date: Jun 2004
Location: , , USA.
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);
            }

        }

Reply With Quote
  #4 (permalink)  
Old July 22nd, 2004, 04:13 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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?
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:32 PM.


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