Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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, 02:53 PM
Authorized User
 
Join Date: Jun 2004
Location: , , USA.
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Default Object reference error

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:
using System;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Perrys
{
    /// <summary>
    /// Summary description for shoeDetails.
    /// </summary>
    public class shoeDetails : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataList myData;
        protected System.Web.UI.WebControls.DropDownList ddlC;
        protected System.Web.UI.WebControls.DropDownList ddlS;
        protected System.Web.UI.WebControls.Label lblShoeName;
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.DropDownList ddlColor;
        protected System.Web.UI.WebControls.RequiredFieldValidator ValidateColor;
        protected System.Web.UI.WebControls.Label Label2;
        protected System.Web.UI.WebControls.DropDownList ddlSize;
        protected System.Web.UI.WebControls.RequiredFieldValidator validateSize;
        protected System.Web.UI.WebControls.Label Label3;
        protected System.Web.UI.WebControls.DropDownList ddlWidth;
        protected System.Web.UI.WebControls.RequiredFieldValidator validateWidth;
        protected System.Web.UI.WebControls.ImageButton imgBtn;
        protected System.Web.UI.WebControls.DropDownList ddlW;


        public SqlDataReader fetchReader(string query, string db)
        {
            SqlConnection cnShoes = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

            SqlCommand cmdSelect = new SqlCommand(query, cnShoes);

            cnShoes.Open();

            return cmdSelect.ExecuteReader (CommandBehavior.CloseConnection);
        }

        public DataSet fetchData(string query, string db)
        {
            SqlConnection cnShoes = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

            SqlDataAdapter myAdapter = new SqlDataAdapter(query, cnShoes);

            DataSet myData = new DataSet();

            myAdapter.Fill(myData);

            return myData;
        }

        object fetchScalar(string query, string db)
        {
            SqlConnection cnShoes = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

            SqlCommand cmdSelect = new SqlCommand(query, cnShoes);

            cnShoes.Open();

            object scalar = cmdSelect.ExecuteReader();

            cnShoes.Close();

            return scalar;
        }


        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, 02:56 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I think you're better of posting this in a .NET related forum, as I think the problem is more related to .NET than it is to SQL Server.

You could also try to limit the amount of code you posted (if possible). Wading through tons of code you haven't written yourself is not much fun.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Where Is My Mind? by Pixies (Track 7 from the album: Surfer Rosa & Come On Pilgrim) What's This?
 
Old July 22nd, 2004, 03:13 PM
Authorized User
 
Join Date: Jun 2004
Location: , , USA.
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sure. Thanks.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Error - Object reference not set to an instance ? venkikrao VB.NET 2 July 5th, 2007 01:00 AM
Class Object reference error. mjhoagland ASP.NET 1.0 and 1.1 Basics 2 February 2nd, 2006 09:28 AM
Error in OleDBConnection : Object reference not se pvsunil Classic ASP Databases 0 April 9th, 2005 02:45 AM
Getting Error - "Object reference not set to an in peri C# 2 December 24th, 2004 11:20 AM
Object reference Error mrideout BOOK: Beginning ASP.NET 1.0 4 August 9th, 2004 07:29 AM





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