Results 1 to 3 of 3

Thread: [RESOLVED] Accessing dataTable data with Dataset

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2013
    Posts
    66

    Resolved [RESOLVED] Accessing dataTable data with Dataset

    Just when I thought I had a grasp on DataTables.

    In the code below after making the Datatables and dataset I cant read the data from the Tables using the dataset.

    What am I missing?

    Thankyou.


    Code:
    using System;
    using System.Data;
    
    namespace JPADataSet
    {
        class Program
        {
            static void Main(string[] args)
            {
                
                DataTable pOD = new DataTable("pOD");
                DataTable pODDG = new DataTable("pODDG");
                DataSet POdataset = new DataSet("POdataset");
    
                pOD.Columns.Add("First Column");
                pODDG.Columns.Add("First Column");
    
                POdataset.Tables.Add("pOD");
                POdataset.Tables.Add("pODDG");
    
                pOD.Rows.Add("hello");
                pODDG.Rows.Add("goodbye");
    
                Console.WriteLine(Convert.ToString(pOD.Rows[0][0])); //Works
                Console.WriteLine(Convert.ToString(pODDG.Rows[0][0])); //Works
    
                Console.WriteLine(Convert.ToString(POdataset.Tables[0].Rows[0][0])); //Dosnt Work
                Console.WriteLine(Convert.ToString(POdataset.Tables[1].Rows[0][0])); // Doesnt work
                Console.Read();
    
               
            }
        }
    }
    //System.IndexOutOfRangeException
    //HResult = 0x80131508
    //Message = There is no row at position 0.
    // Source = System.Data.Common
    //StackTrace:
    //at System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex)
    // at System.Data.DataRowCollection.get_Item(Int32 index)
    // at JPADataSet.Program.Main(String[] args) in C: \Users\tonya\source\repos\JPADataSet\JPADataSet\Program.cs:line 27

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Accessing dataTable data with Dataset

    The problem is that you aren't adding the DataTables you created to your DataSet. You are telling the DataSet to create its own DataTables with the names you specify. This:
    csharp Code:
    1. POdataset.Tables.Add("pOD");
    says "create a new DataTable with the name "pOD" and add it to the DataSet". This:
    csharp Code:
    1. POdataset.Tables.Add(pOD);
    says "add the existing DataTable currently referred to by the pOD variable to the DataSet". It's not DataSets and DataTables that are the problem but variables and methods.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2013
    Posts
    66

    Re: Accessing dataTable data with Dataset

    Thank you very much jmcilhinney. I just couldn't see it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width