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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:09:51+00:00 2026-06-18T02:09:51+00:00

This is a windows forms application. I have a class Program.cs and a form

  • 0

This is a windows forms application. I have a class Program.cs and a form DeployerConsole.cs with a ListBox on it. I am attempting to loop through the results from the SQL query. I am having trouble and getting a ‘Object reference not set to an instance of an object.’ error when I try to access the data and insert it into my listbox on the form.

EDIT: The SQL Query is completing successfully and the Console.WriteLine is outputting to the output window properly with the correct data.

    static void LoadServers()
    {
        DeployerConsole DC = (DeployerConsole)Application.OpenForms["DeployerConsole"];
        //DeployerConsole DC = new DeployerConsole();
        SqlConnection myConnection = new SqlConnection("server=XXX; database=XXX; uid=XXX; pwd=XXX;Integrated Security=true;Connection Lifetime=5;Trusted_Connection=yes;");
        myConnection.Open();
        DataSet ds = new DataSet();
        SqlCommand myCommand = new SqlCommand("SELECT ServerName FROM DeployServers", myConnection);
        SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
        adapter.Fill(ds);
        DataTable dt = ds.Tables[0];
        foreach (DataRow dr in dt.Rows)
        {
            DC.listBox1.Items.Add(dr["ServerName"].ToString());
            Console.WriteLine(dr["ServerName"].ToString());
        }
       
    }

EDIT: Added Screenshot

Screenshot of Error

Anyone have any suggestions or provide guidance as to what I am doing wrong?

EDIT (ANSWER): Well, I moved the code from the class to the form. No problems accessing the listBox now. However, I still would like to know the solution to this.

  • 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-18T02:09:53+00:00Added an answer on June 18, 2026 at 2:09 am

    It might be worth a try (just for testing purposes) to try:

    if(DC.listBox1 != null)
    {
      DC.listBox1.Items.Add(dr["ServerName"].ToString());
    }
    

    That could give you a clue, if which you might need to make sure someplace listBox1 is instantiated.

     DC.listBox1 = new ListBox();
    

    EDIT

    It might be that DC is not being correctly passed by reference to the static method.
    It might be better to change the signature of the Method to accept the form.

    static void LoadServers(Form form)
    { 
     ...
     form.listBox1.Items.Add(...)
     ... etc.
    }
    

    and call it assuming from the form:

    Helper.LoadServers(this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .NET application that contains a checkbox (System.Windows.Forms.Checkbox). This component (WindowsForms10.BUTTON.app.0.378734a1) is
I have a C# Windows Form application that contains a menu with this event:
I am using C# with a Windows Application Form. In this I have a
I have a windows form without running it in an application. this is the
I have created a windows form application: A presentation library with several windows forms
In a Windows Forms application I kill a process like this: Process[] ps =
In System.Windows.Forms.Application there are two properties called LocalUserAppDataPath and UserAppDataPath . On this computer
I am using this text editor for my windows forms application This works great
We are currently developing a Windows Forms application in VS 2008 C#. This application
I've got an ASP.NET (.NET 4.0) application that uses Windows Forms Authentication. This authenticates

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.