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 8099615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:28:08+00:00 2026-06-05T22:28:08+00:00

Problem: I have a value in a database table. This value can either contain

  • 0

Problem:

I have a value in a database table. This value can either contain a number, or null. If its null I would like to show one group of controls. If its not null I would like to show another group of controls.


Previous Attempts:

I have tried creating the controls in the code behind depending on the value of the database. This worked. However, on postback I get a null reference exception. The control doesn’t exist on postback because the page is stateless. I’m building the controls in the page_load handler (depending on the value of the table column). Since I’m creating the controls in the page_load shouldn’t they exist on postback?

I also tried recreating the controls in the event handler for the button. I get a “theres already a control with this id” exception (presumably because I already created it in the page_load method).

I read a few posts about how I have to store the controls in a session. This seems like more work than it should be.

Questions:

  1. Am I going about this the wrong way? This seems like it should have been simple but is turning into a mess.
  2. If this is the correct way to do this, Where do I add the session information? I’ve been reading other posts and I’m kind of lost

Code:

    int bookId;
    string empName;

    protected void Page_Load(object sender, EventArgs e)
    {
        if(int.TryParse(Request.QueryString["id"], out bookId))
        {
            //This is where the value in the database comes into play. If its null Book.GetCopyOwner
            // returns a string with length 0

            empName = Book.GetCopyOwner(bookId, Request.QueryString["owner"]);
            if (empName.Trim().Length > 0)
            {
                CreateReturnControls();
            }
            else
            {
                CreateCheckoutControls();
            }
        }
    }

    protected void ReturnButton_Click(object sender, EventArgs e)
    {
    }

    protected void CheckOut_Click(object sender, EventArgs e)
    {
        int bookId;
        if (int.TryParse(Request.QueryString["id"], out bookId))
        {
            TextBox userId = (TextBox)this.Page.FindControl("UserId");

            //WHEN I TRY TO USE THE TEXTBOX userId HERE, I GET NULL REFERENCE EXCEPTION

            BookCopyStatusNode.Controls.Clear();
            CreateReturnControls();
        }
    }

    protected void CopyUpdate_Click(object sender, EventArgs e)
    {
    }

    private void CreateCheckoutControls()
    {
        TextBox userId = new TextBox();
        //userId.Text = "Enter Employee Number";
        //userId.Attributes.Add("onclick", "this.value=''; this.onclick=null");
        userId.ID = "UserId";

        Button checkOut = new Button();
        checkOut.Text = "Check Out";
        checkOut.Click += new EventHandler(CheckOut_Click);

        TableCell firstCell = new TableCell();
        firstCell.Controls.Add(userId);

        TableCell secondCell = new TableCell();
        secondCell.Controls.Add(checkOut);

        BookCopyStatusNode.Controls.Add(firstCell);
        BookCopyStatusNode.Controls.Add(secondCell);
    }

    private void CreateReturnControls()
    {
        Label userMessage = new Label();
        userMessage.Text = empName + " has this book checked out.";

        Button returnButton = new Button();
        returnButton.Text = "Return it";
        returnButton.Click += new EventHandler(ReturnButton_Click);

        TableCell firstCell = new TableCell();
        firstCell.Controls.Add(userMessage);

        TableCell secondCell = new TableCell();
        secondCell.Controls.Add(returnButton);

        BookCopyStatusNode.Controls.Add(firstCell);
        BookCopyStatusNode.Controls.Add(secondCell);
    }
  • 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-06-05T22:28:10+00:00Added an answer on June 5, 2026 at 10:28 pm

    It looks like you’re creating a static set of controls based on the database value. Why not simply have 2 Panels that contain the controls you want and simply set their visibility to true or false:

    if (!Page.IsPostBack)
    {
        if (int.TryParse(Request.QueryString["id"], out bookId))
        {
            empName = Book.GetCopyOwner(bookId, Request.QueryString["owner"]);
            var display = (empName.Trim().Length > 0);
    
            panelReturnControls.Visible = display;
            panelCheckoutControls.Visible = !display;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem in inserting the listbox value into mysql database in vb
i have a problem in inserting the listbox value into mysql database in vb
I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
I have this problem containing some inequations and requirement to minimize a value. After
I have a single database table that stores week entries. Id Value WeekId 1
Problem: I have a large database table (~500k records) which has a list of
I have a problem in php code inserting values into database (I use PHPMyAdmin).
Problem: I have a value that is set in $record, for instance 1.69. Then
I have a problem while converting a string whose value is dd.mm.yyyy to DateTime
Hey I have a problem comparing the value of a CGPoint (struct with two

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.