Hi Guys.. I've found a little tutorial which seems to show exactly what I have been trying to do which seems to work upto a certian extent. The code I am using is the following:

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        System.Data.OleDb.OleDbConnection Conn;

        private void Form1_Load(object sender, EventArgs e)
        {
            //Create DataSet Object & New DataSet

            Conn = new System.Data.OleDb.OleDbConnection();

            //The Connection Below is for Access 2010 DataBase
            Conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/DemoDB1.accdb";

            Conn.Open();

            DataSet oDS = new DataSet();

            MessageBox.Show("Connection Open");

            //Create the DataTable "Customers" in the DataSet and the OrdersDataAdapter
            OleDbDataAdapter oOrdersDataAdapater = new OleDbDataAdapter(new OleDbCommand("Select * FROM Customers", Conn));

            OleDbCommandBuilder oOrdersCmdBuilder = new OleDbCommandBuilder(oOrdersDataAdapater);
            oOrdersDataAdapater.FillSchema(oDS, SchemaType.Source);

            DataTable pTable = oDS.Tables["Table"];
            pTable.TableName = "Customers";

            //Create the DataTable "Orders" in the DataSet and the OrderDetailsDataAdapater
            OleDbDataAdapter oOrderDetailsDataAdapter = new OleDbDataAdapter(new OleDbCommand( "SELECT * FROM Orders",Conn));
            OleDbCommandBuilder oOrderDetailsCmdBuilder = new OleDbCommandBuilder(oOrderDetailsDataAdapter);
            oOrderDetailsDataAdapter.FillSchema(oDS, SchemaType.Source);

            pTable = oDS.Tables["Table"];
            pTable.TableName = "Orders";


            //Create the relationship between the two tables
            oDS.Relations.Add(new DataRelation("ParentChild",
                oDS.Tables["Customers"].Columns["OrderID"],
                    oDS.Tables["Orders"].Columns["OrderID"]));

            //Inserting Data
            DataRow oOrderRow = oDS.Tables["Customers"].NewRow();
            oOrderRow["customerName"] = "Customer ABC";
            oOrderRow["postCode"] = "EX5 2UL";
            oDS.Tables["Customers"].Rows.Add(oOrderRow);

            DataRow oDetailRow = oDS.Tables["Orders"].NewRow();
            oDetailRow["ProductID"] = 1;
            oDetailRow["ProductName"] = "Product Name Weee";
            oDetailRow["UnitPrice"] = 1;
            oDetailRow["Quantity"] = 3;

            oDetailRow.SetParentRow(oOrderRow);
            oDS.Tables["Orders"].Rows.Add(oDetailRow);


            //Updating the DataSet
            oOrdersDataAdapater.Update(oDS, "Customers");
            oOrderDetailsDataAdapter.Update(oDS, "Orders");

            Conn.Close();

            MessageBox.Show("Connection Closed");

        }

The Code seems to work upto the following line:
oDetailRow.SetParentRow(oOrderRow); when stepping through the Code in the watch window the tables get created find in the datasets etc, etc.. but as soon as its hits the 'SetParentRow' line. the Code jumps to the 'Form1.Designers.cs' Window and runs through the following section of code:

      protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

Could anyone give me some advice on where I am going wrong on this?

Many thanks
Mark.

Are you getting an exception? That would be helpful to have.

At a quick glance, try adding the row oDetailRow to the table before calling SetParentRow().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.