Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 April 13th, 2006, 08:35 AM
Authorized User
Join Date: Apr 2006
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to vivekshah Send a message via Yahoo to vivekshah
Default Problem in adding controls dynamically

I am trying to create multiple datagrid ,

Below is the server error that i got while executing.

Server Error in '/threaad' Application.

 Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 38: for(int i=1;i<=5;i++)
Line 39: {
Line 40: dg[i].DataSource = ds;
Line 41: dg[i].DataBind();
Line 42: place1.Controls.Add(dg[i]);

Source File: c:\inetpub\wwwroot\threaad\dynmicdatagrid.aspx.cs Line: 40

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   threaad.DynmicDatagrid.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\threaad\dynmicdatagrid.aspx.cs: 40
   System.Web.UI.Control.OnLoad(EventArgs e)

&&& the code -snippet that i executed is below.

SqlConnection con;SqlDataAdapter myda;
        protected System.Web.UI.WebControls.PlaceHolder place1;
        DataSet ds;
        private void Page_Load(object sender, System.EventArgs e)
            // Put user code to initialize the page here

            DataGrid [] dg = new DataGrid[5];
            //DataGrid dg = new DataGrid();
            ds = new DataSet();
             con = new SqlConnection("server=deepsea;database=master;user id=sa;password=****");
            string qry = "SELECT TOP 100 * FROM PRODUCTS ";
            myda = new SqlDataAdapter(qry,con);

// dg.DataSource = ds;
// dg.DataBind();
// place1.Controls.Add(dg);

            for(int i=1;i<=5;i++)
                dg[i].DataSource = ds;

Vivek Shah
Vivek Shah

Similar Threads
Thread Thread Starter Forum Replies Last Post
Events - Adding Dynamically dash dev C# 2005 2 December 5th, 2007 02:05 PM
Adding Controls Dynamically andyj00 C# 0 March 19th, 2007 08:54 AM
dynamically adding objects shawnvb VB How-To 3 February 14th, 2007 03:12 PM
Problem w/ dynamically added controls in GridView jpamental Pro Visual Basic 2005 2 May 12th, 2006 09:10 AM
Dynamically Adding Controls - Error pagates Excel VBA 1 August 28th, 2003 01:59 AM

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