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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:42:44+00:00 2026-05-27T20:42:44+00:00

I have what seems to be a simple question but its killing me trying

  • 0

I have what seems to be a simple question but its killing me trying to find out.

I have a form in which I have a ListView. In this ListView I would like to populate it with data from a SQL Server 2008 database table.

public void LoadList()
{
   DataTable dtable = budget_MainDataSetReceipt.Tables["Receipt"];
   listView1.Items.Clear();

   for (int i = 0; i < dtable.Rows.Count; i++)
   {
      DataRow drow = dtable.Rows[i];

      if (drow.RowState != DataRowState.Deleted)
      {
         ListViewItem lvi = new ListViewItem(drow["ReceiptID"].ToString());
         lvi.SubItems.Add(drow["DateCleared"].ToString());
         lvi.SubItems.Add(drow["CategoryID"].ToString());
         lvi.SubItems.Add(drow["Amount"].ToString());
         lvi.SubItems.Add(drow["Store"].ToString());
         lvi.SubItems.Add(drow["DateEntered"].ToString());

         listView1.Items.Add(lvi);
      }
   }
} 

I keep getting an

Object reference not set to an instance of an object

error, and I can’t figure out why. There are about 5 rows of data in my database, so in my mind, there should be 5 rows of data within the list view.

Can anyone tell me what I am missing? I can post more code if that would be helpful.

I have tried calling the LoadList() method in several ways:

  • Before the method itself
  • With the InitializeComponent() method
  • I have tried the following syntax

    this.LoadList(); 
    this.Form1.LoadList();`
    

I have also tried to initialize the DataTables type with the following:

DataTables dt = new DataTables //did not work
  • 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-27T20:42:44+00:00Added an answer on May 27, 2026 at 8:42 pm

    My hunch would be: you’re assuming for all columns in your DataRow that they’re present and not null – that’s a bit of a dangerous assumption.

    I would change your assignments to use a method that checks for DBNull before returning the string:

    public string SafeGetString(DataRow row, string columnName)
    {
        if(row[columnName] != null && row[columnName] != DBNull.Value)
        {
            return row[ColumName].ToString();
        }
    
        return string.Empty;
    }
    

    so your could would look like:

    ListViewItem lvi = new ListViewItem(SafeGetString(drow, "ReceiptID"));
    lvi.SubItems.Add(SafeGetString(drow, "DateCleared"));
    // and so forth
    

    This way, if any of the columns should contain a NULL, you would get back an empty string – instead of running into a NULL.ToString() that causes the error you’re seeing.

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

Sidebar

Related Questions

This seems like a simple question, but I haven't been able to find the
I have a question that seems simple but I cannot figure out what is
This seems like a simple question on its surface but I thought I'd reach
I have a what seems to be a simple question but for the life
I have what is undoubtedly a simple question, but I can't seem to find
This seems like such a simple question. I have several Edit boxes on my
I have a very simple question for which I can't seem to find an
Simple question which I can't seem to find an answer of: I have two
I know this might seem like a simple question, but its answer has been
This seems simple, but I haven't been able to find anything that does precisely

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.