locked
Adding a Row to a Table RRS feed

  • Question

  • Hi,

    I created a table (from a MS Access Database) called "Results" and have the following codes:

     DataRow newrow = DS1.Tables["Results"].NewRow();

     newrow["SPXT INDEX"] = 0.1;

    DS1.Tables["Results"].Rows.Add(newrow);

    Somehow there is an error relating to the first line -  DataRow newrow = DS1.Tables["Results"].NewRow();

    The error message is:

    ---------------------------------------------------------------------------------------------------------------------------

    An unhandled exception of type 'System.NullReferenceException' occurred in BT3.exe

    Additional information: Object reference not set to an instance of an object.

    ---------------------------------------------------------------------------------------------------------------------------------

    Can somebody figure out what went wrong?

    Thank you very much!

    Ed

    Monday, June 15, 2015 4:02 PM

Answers

  • You need to put a breakpoint on that line and verify which object is null by selecting the object name and right click Quick Watch in the debugger window.

    You must check this: DS1.Tables["Results"]


    Fouad Roumieh

    • Marked as answer by edwardlee007 Tuesday, June 16, 2015 3:26 PM
    Monday, June 15, 2015 4:44 PM
  • Ed,

    I've no problem at all running your code. However, probably you did initialize something wrong.

    using System;
    using System.Data;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataSet DS1 = new DataSet();
                DS1.Tables.Add(new DataTable("Results"));
                DS1.Tables["Results"].Columns.Add("SPXT INDEX");
                DataRow newrow = DS1.Tables["Results"].NewRow();
                newrow["SPXT INDEX"] = 0.1;
                DS1.Tables["Results"].Rows.Add(newrow);
                Console.WriteLine(DS1.Tables[0].Rows[0][0]);
                Console.ReadKey();
            }
        }
    }


    Success
    Cor

    • Marked as answer by edwardlee007 Tuesday, June 16, 2015 3:26 PM
    Tuesday, June 16, 2015 9:15 AM

All replies

  • You need to put a breakpoint on that line and verify which object is null by selecting the object name and right click Quick Watch in the debugger window.

    You must check this: DS1.Tables["Results"]


    Fouad Roumieh

    • Marked as answer by edwardlee007 Tuesday, June 16, 2015 3:26 PM
    Monday, June 15, 2015 4:44 PM
  • Unless you explicitly named the table as part of the query then it'll default to Table1.  If you only have a single table then use the zero-based index instead.  Tables[0]

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Monday, June 15, 2015 5:51 PM
  • Hi Ed,

    If only have one table in your DateSet, please try to use the following code

    DataRow newrow=dt.Tables[0].NewRow(); dt.Tables[0].Rows.Add(newrow);


    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, June 16, 2015 6:18 AM
  • Ed,

    I've no problem at all running your code. However, probably you did initialize something wrong.

    using System;
    using System.Data;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataSet DS1 = new DataSet();
                DS1.Tables.Add(new DataTable("Results"));
                DS1.Tables["Results"].Columns.Add("SPXT INDEX");
                DataRow newrow = DS1.Tables["Results"].NewRow();
                newrow["SPXT INDEX"] = 0.1;
                DS1.Tables["Results"].Rows.Add(newrow);
                Console.WriteLine(DS1.Tables[0].Rows[0][0]);
                Console.ReadKey();
            }
        }
    }


    Success
    Cor

    • Marked as answer by edwardlee007 Tuesday, June 16, 2015 3:26 PM
    Tuesday, June 16, 2015 9:15 AM
  • Dear everybody and in particular Cor,

    Thanks for being extremely helpful and in particular Cor even came out with the very useful code. 

    That exactly reminded me that I didn't really add the existing table "Results" to the DS1 Dataset I created. Please forgive me for this stupid mistake as I am really very new to C#. 

    But then I also realized that I couldn't simply add existing table "Results" to an existing Dataset DS1 in this case. I had to resort to:

    ----------------------------------------------------------------------

    OleDbDataAdapter adapter4 = new OleDbDataAdapter("SELECT * FROM Results", connection);
    connection.Open();
    adapter4.Fill(DS1, "Results"); 

    ----------------------------------------------------------------------

    The above is probably not the best way. Can somebody enlighten me as whether there is a simpler way?

    Thank you very much!

    Ed

    Tuesday, June 16, 2015 3:32 PM
  • Dear everybody and in particular Cor,

    Thanks for being extremely helpful and in particular Cor even came out with the very useful code. 

    That exactly reminded me that I didn't really add the existing table "Results" to the DS1 Dataset I created. Please forgive me for this stupid mistake as I am really very new to C#. 

    But then I also realized that I couldn't simply add existing table "Results" to an existing Dataset DS1 in this case. I had to resort to:

    ----------------------------------------------------------------------

    OleDbDataAdapter adapter4 = new OleDbDataAdapter("SELECT * FROM Results", connection);
    connection.Open();
    adapter4.Fill(DS1, "Results"); 

    ----------------------------------------------------------------------

    The above is probably not the best way. Can somebody enlighten me as whether there is a simpler way?

    Thank you very much!

    Ed

    @Ed,

    Since your original question is Adding a Row to a Table, now this issue is not the same as the original. Please only ask ONE question per thread. Start a new thread if you have a new question.

    Thanks for your understanding.

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, June 23, 2015 8:39 AM