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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:15:01+00:00 2026-05-15T01:15:01+00:00

I have the following sample where the SourceData class would represent a DataView resulting

  • 0

I have the following sample where the SourceData class would represent a DataView resulting from an Sql query:

class MainClass
{
    private static SourceData Source;
    private static DataView View;
    private static DataView Destination;
        
    public static void Main (string[] args)
    {
        Source = new SourceData();
        View = new DataView(Source.Table);
        Destination = new DataView();
            
        Source.AddRowData("Table1", 100);
        Source.AddRowData("Table2", 1500);
        Source.AddRowData("Table3", 1300324);
        Source.AddRowData("Table4", 1122494);
        Source.AddRowData("Table5", 132545);

        Console.WriteLine(String.Format("Data View Records: {0}", View.Count));         

        foreach(DataRowView drvRow in View)
        {
            Console.WriteLine(String.Format("Source {0} has {1} records.", drvRow["table"], drvRow["records"]));
            DataRowView newRow = Destination.AddNew();
            newRow["table"] = drvRow["table"];
            newRow["records"] = drvRow["records"];
        }
            
        Console.WriteLine();
        Console.WriteLine(String.Format("Destination View Records: {0}", Destination.Count));
            
        foreach(DataRowView drvRow in Destination)
        {
            Console.WriteLine(String.Format("Destination {0} has {1} records.", drvRow["table"], drvRow["records"]));
        }

    }
}

class SourceData
{
    public DataTable Table
    {
        get{return dataTable;}
    }
        
    private DataTable dataTable;
    
    public SourceData()
    {
        dataTable = new DataTable("TestTable");
        dataTable.Columns.Add("table", typeof(string));
        dataTable.Columns.Add("records", typeof(int));
    }
        
    public void AddRowData(string tableName, int tableRows)
    {
        dataTable.Rows.Add(tableName, tableRows);
    }
}

My output is:

Data View Records: 5
Source Table1 has 100 records.

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at System.Data.DataView.AddNew () [0x0003e] in /usr/src/packages/BUILD/mono-2.4.2.3
/mcs/class/System.Data/System.Data/DataView.cs:344 at DataViewTest.MainClass.Main (System.String[] args) [0x000e8] in /home/david/Projects/DataViewTest/SourceData.cs:29

I did some reading here: DataView:AddNew Method…
…and it would appear that I am doing this the right way. How come I am getting the Object reference not set?

  • 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-15T01:15:01+00:00Added an answer on May 15, 2026 at 1:15 am

    The DataView has to be a view of something. The way you’ve got it, it’s not a view of anything (using the default constructor) – you need to at least set the DataView.Table property to something.

    I think you actually want to create a new DataTable for “destination”, not a DataView.

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

Sidebar

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.