Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional 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 December 26th, 2006, 08:30 PM
Registered User
 
Join Date: Oct 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Object reference not set to an instance of an obje

I am creating a set of drop down lists on a page with the following code:

Table table = new Table();
        TableRow trow = new TableRow();
        TableCell tcell = new TableCell();
        DropDownList ddl;
        Label lbl;
        table.ID = "tblItems";
        table.BorderColor = System.Drawing.Color.Blue;
        table.BorderStyle = BorderStyle.Solid;
        table.BorderWidth = 1;

        int i = 0;
        foreach (DataRow row in dtColumns.Rows)
        {
            trow = new TableRow();
            tcell = new TableCell();
            tcell.ID = "Cell" + i.ToString();
            tcell.Width = 120;
            ddl = new DropDownList();
            lbl = new Label();
            lbl.Text = row["Column_Name"].ToString();
            tcell.Controls.Add(lbl);
            trow.Cells.Add(tcell);
            tcell = new TableCell();
            ddl.DataSource = dt;
            ddl.DataTextField = "Choice";
            ddl.DataValueField = "Reference";
            ddl.DataBind();
            ddl.SelectedValue = row["Column_Name"].ToString();
            ddl.ID = "ddlFields" + i.ToString();
            tcell.Controls.Add(ddl);
            trow.Cells.Add(tcell);
            table.Rows.Add(trow);
            i++;
        }
        hidColumns.Value = (--i).ToString();
        plhColumnMatch.Controls.Add(table);
        plhWorksheets.Visible = false;
        plhColumns.Visible = true;

On the page is a button and it OnClick event contains the following code:

for (int i = 0; i <= intColumns; i++)
        {
            DropDownList ddl1 = new DropDownList();
            string strValue;
            Button btn = (Button)Page.FindControl("Button1");
            ddl1 = (DropDownList)plhColumnMatch.FindControl("ddlField s0");
            strValue = ddl1.SelectedValue;

The last piece of code generates the error. I have tired referencing a control (a button) that was added at design time but I get the same. I have tried variations, such as:
strValue = (plhColumnMatch.FindControl("ddlFields0")as DropDownList).SelectedValue;
but I still get the same.

I am using VS2005 with sp1 and it using localhost/http not file

Thanks
Robert





Similar Threads
Thread Thread Starter Forum Replies Last Post
Object reference not set to an instance of an obje GS C# 2005 3 May 21st, 2007 10:11 AM
Object reference not set to an instance of an obje williadn General .NET 0 January 18th, 2005 05:39 PM
Object reference not set to an instance of an obje bekim Classic ASP Basics 1 August 9th, 2004 01:25 AM
Object reference not set to an instance of an obje DolphinBay VB.NET 2002/2003 Basics 6 June 28th, 2004 03:14 AM





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