Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7512153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:46:44+00:00 2026-05-29T23:46:44+00:00

So I have an array of textboxes dynamically appear (The number of textboxes depends

  • 0

So I have an array of textboxes dynamically appear (The number of textboxes depends upon how a number from a database). They draw to the screen just fine.

    i = 0;

    while (i < size)
    {
        pnlTxtBoxes.Controls.Add(labels[i]);
        pnlTxtBoxes.Controls.Add(txtBoxes[i]);
        pnlTxtBoxes.Wrap = true;
        i++;
    }

Like I said, the textboxes appear and the labels are displaying correctly. But when I go to retrieve the text from them, I get the error “Object reference not set to an instance of an object.”

    i = 0;

    while (i < size)
    {
        values[i] = txtBoxes[i].Text;
        txtBoxes[i].Visible = false;
        labels[i].Visible = false;
        i++;
    } 

Does anybody have an idea as to why I’m getting this error (and what I can do to fix it)?

EDIT: Here is all of the code. This is just a development DB, so I am not worried about showing the password

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

public partial class dieClearanceCalc : System.Web.UI.Page
{
static string connectionString = "database=localhost;database=matedevdb;uid=dev;pwd=123;";
MySqlConnection con = new MySqlConnection(connectionString);
MySqlCommand cmd = new MySqlCommand("SELECT shapeName FROM tblShapes;");
MySqlDataReader reader;
int size;
TextBox[] txtBoxes;
Label[] labels;

protected void Page_Load(object sender, EventArgs e)
{
    cmd.Connection = con;
    try
    {
        con.Open();
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            shapeSelection.Items.Add(reader.GetString(0));
        }
        reader.Close();
    }
    catch (Exception ex)
    {
        Response.Write("<p style='Color:red'>Error:<br/>" + ex + "</p>");
    }
}
protected void shapeSelected(object sender, EventArgs e)
{
    string[] labelTxt;
    int i = 0;

    //Make current elements invisable
    lblShape.Visible = false;
    shapeSelection.Visible = false;
    btnSelectShape.Visible = false;

    // find the size of the arrays
    cmd.CommandText = "SELECT COUNT(varID) FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
    reader = cmd.ExecuteReader();
    if (reader.Read())
    {
        size = reader.GetInt32(0);
    }
    reader.Close();

    labelTxt = new string[size];
    labels = new Label[size];
    txtBoxes = new TextBox[size];

    // gather the labels from the db
    cmd.CommandText = "SELECT varDesc FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
    reader = cmd.ExecuteReader();

    i = 0;

    while (reader.Read())
    {
        labelTxt[i] = reader.GetString("varDesc");
        i++;
    }
    reader.Close();

    i = 0;

    while (i < size)
    {
        labels[i] = new Label();
        txtBoxes[i] = new TextBox();
        labels[i].Text = labelTxt[i];
        i++;
    }

    i = 0;

    while (i < size)
    {
        pnlTxtBoxes.Controls.Add(labels[i]);
        pnlTxtBoxes.Controls.Add(txtBoxes[i]);
        pnlTxtBoxes.Wrap = true;
        i++;
    }

    btnSendData.Visible = true;
    //Response.Write(size); test to see if the size variable is working
    Response.Write(size);

}

protected void calc(object sender, EventArgs e)
{
    //declarations  
    formula diagonal, periphery;
    string dFormula = "", pFormula = "";
    string[] variables;
    string[] values;
    int i = 0;
    //end of declarations

    // This value must be retrievd again, because somewhere size is getting a value of  0
    cmd.CommandText = "SELECT COUNT(varID) FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
    reader = cmd.ExecuteReader();
    if (reader.Read())
    {
        size = reader.GetInt32(0);
    }
    reader.Close();


    variables = new string[size];
    values = new string[size];

    i = 0;

    while (i < size)
    {
        values[i] = txtBoxes[i].Text;
        txtBoxes[i].Visible = false;
        labels[i].Visible = false;
        i++;
    }

    btnSendData.Visible = false;

    // retrieve the diagonal formula from the db
    cmd.CommandText = "SELECT diagonalFormula, peripheryFormula FROM tblShapes WHERE shapeName='" + shapeSelection.SelectedValue + "'";
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        dFormula = reader.GetString("diagonalFormula");
        pFormula = reader.GetString("peripheryFormula");
    }
    reader.Close();

    Response.Write(size);

    // gather the variable names from the db
    cmd.CommandText = "SELECT varName FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
    reader = cmd.ExecuteReader();

    while (reader.Read())
    {
        variables[i] = reader.GetString("varName");
        i++;
    }
    reader.Close();

    con.Close();

    diagonal = new formula(dFormula, variables, values);
    periphery = new formula(pFormula, variables, values);

    txtDiagonal.Visible = true;
    txtPeriphery.Visible = true;

    txtDiagonal.Text = diagonal.getEquation();
    txtPeriphery.Text = periphery.getEquation();
}

public static double Evaluate(string expression)
{
    System.Data.DataTable table = new System.Data.DataTable();
    table.Columns.Add("expression", string.Empty.GetType(), expression);
    System.Data.DataRow row = table.NewRow();
    table.Rows.Add(row);
    return double.Parse((string)row["expression"]);
}  

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:46:46+00:00Added an answer on May 29, 2026 at 11:46 pm

    An important thing to remember when programming with ASP.NET is that your whole object model on a page gets created with every web request again and again. This means that if you add some controls to you page dynamically in an event that does not happen each page load they are not going to survive the postback if you don’t add them again somehow.

    You can read about Asp.Net page Life Cycle here: http://msdn.microsoft.com/en-us/library/ms178472.aspx

    I don’t think there is a standard way to solve your problem, but what you are trying to do can be achieved in several ways. One was already mentioned to you – usage of ViewState. Another one would be hitting database on each post back, and making sure you are recreating the controls each time the page is served. One more way is to somehow encode the data that is required for recreating your controls and make sure that they are passed with each post back. The latter is essentially what ViewState does, but you can do it more efficiently if you want to reduce the size of you ViewState, and thus size in bytes of your postback and page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a table array dynamically generated from a data query and stored in
I am tying to add the values from an array to a number of
I have a following program, it takes strings from 4 textboxes and puts them
In WPF C#, in code behind, I have to dynamically create an Array of
I have array of two textboxes in a table, on blur of one text
I have a textbox array in my form. The textbox(s) are dynamically added using
I have an array of 5 objects and also 5 textboxes. I am wanting
I have several textboxes that are generated dynamically based on some results I get
I have a userform that I have created that contains a number of TextBoxes.
I have array of select tag. <select id='uniqueID' name=status> <option value=1>Present</option> <option value=2>Absent</option> </select>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.