1

I have a web control (ascx). This control has some database connections which perform selects based on a member variable (e.g., shows details based on a certain database ID).

What works: I can add the web control to a page in the designer, set the member variable representing the ID in the Page_Load variable.

What does not: I am trying to add the web control (several, actually) to a panel during the Page_Load. When I do this, the SqlDataSource's on the web control are null.

This is how I am trying to add the web controls.

foreach (DataRow dr in dv.Table.Rows)
{
    Label header = new Label();
    header.Text = "Blah blah blah";
    Panel1.Controls.Add(header);
    DetailsControl widgetDetails = new DetailsControl();
    widgetDetails.WidgetID = (Int64)dr[1];
    Panel1.Controls.Add(widgetDetails);
}

This is the error I get, which is coming from the DetailsControl.

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 19:         protected void Page_Load(object sender, EventArgs e)
Line 20:         {
Line 21:             DataView dv = (DataView)SqlDataSourceFilter.Select(DataSourceSelectArguments.Empty);

EDIT: Actually, it appears that all controls within the DetailsControl are null, but only hwen added dynamically. I must need to call some function to trigger their initialization.

2 Answers 2

1

A long shot, since I don't have access to your source code...

But maybe you haven't properly constructed your objects? Where is the SqlDataSourceFilter object being constructed? Init method ? Class constructor?

Sign up to request clarification or add additional context in comments.

1 Comment

I dragged the data source into the designer. I cannot actually find where in the code the data source is constructed (ASP.NET probably does it when it parses the aspx page?)
1

Where do you do: foreach (DataRow dr in dv.Table.Rows)?

Try to move this to the Page_Init, that should help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.