[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
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:
POdataset.Tables.Add("pOD");
says "create a new DataTable with the name "pOD" and add it to the DataSet". This:
csharp Code:
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.
Re: Accessing dataTable data with Dataset
Thank you very much jmcilhinney. I just couldn't see it.