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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:15:18+00:00 2026-05-25T02:15:18+00:00

I try to make an app which manages client’s contacts. The form is very

  • 0

I try to make an app which manages client’s contacts. The form is very very simple. There are: panel (inside this panel are labels with names, emails, etc.) and a listbox which shows all saved contacts in sdf file. I have one big problem. I want to refresh the listbox content via a method in another class. I set the listbox as public, debugger shows no errors during the project build. When the form loads, this message is prompted (via try-catch exception): “Object reference not set to an instance of an object.”. I tried to do this project differently – everything was written in one class. Here’s the code:

database.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace Contacts
{
    class Database
    {
        public static void RefreshListBox()
        {
            SqlCeConnection connection = null;
                try
                {
                var form1 = Form.ActiveForm as Form1;
                connection = new SqlCeConnection("Datasource = C:\\Kontakty.sdf");
                connection.Open();
                SqlCeCommand command = new SqlCeCommand("SELECT * FROM Contacts", connection);
                SqlCeDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    form1.listBox1.Items.Add(reader.GetString(0) + " " + reader.GetString(1));
                }
                }
                catch(Exception exc)
                {
                    MessageBox.Show(exc.Message);

        }
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Contacts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Database.RefreshListBox();
        }


    }
}

Does anyone here know what is wrong?

PS: kontakty = contacts (in Czech ;-))

  • 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-25T02:15:19+00:00Added an answer on May 25, 2026 at 2:15 am

    This might help:

    public static void RefreshListBox(ListBox listBox)
      ...
      while (reader.Read())
      {
        listBox.Items.Add(reader.GetString(0) + " " + reader.GetString(1));
      }
    

    And then:

    private void Form1_Load(object sender, EventArgs e)
    {
      Database.RefreshListBox(this.listBox1);
    }
    

    Other than that, you really shouldn’t be passing forms or listboxes into database classes. It should be the other way around– your form should just get the data from the class and populate the listbox there.

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

Sidebar

Related Questions

I'm new to Web programming! I try to make an app (something like Client
Hi I'm trying to make an app in which there's a search box, I've
I try to make a registration form. When user post the fields, i check
Let me try to explain my challenge. I'm building a Silverlight app which will
I am building a simple C# web app which will act like an online
I'm trying to make app which will log my incoming and outgoing calls and
I am attempting to make an app in which you can load an image
I try to make a single page app with Rails 3.2 and Backbone.js with
I am trying to make a metro app which involves the user sending out
I'm creating an app which communicates alot with a server. I want to make

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.