1 Replies - 850 Views - Last Post: 28 September 2012 - 02:45 PM Rate Topic: -----

#1 sundog1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 11-September 12

Relational Data Using DataSets

Posted 28 September 2012 - 06:41 AM

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:

    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()
    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";
    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",
    //Inserting Data
    DataRow oOrderRow = oDS.Tables["Customers"].NewRow();
    oOrderRow["customerName"] = "Customer ABC";
    oOrderRow["postCode"] = "EX5 2UL";
    DataRow oDetailRow = oDS.Tables["Orders"].NewRow();
    oDetailRow["ProductID"] = 1;
    oDetailRow["ProductName"] = "Product Name Weee";
    oDetailRow["UnitPrice"] = 1;
    oDetailRow["Quantity"] = 3;
    //Updating the DataSet
    oOrdersDataAdapater.Update(oDS, "Customers");
    oOrderDetailsDataAdapter.Update(oDS, "Orders");
    MessageBox.Show("Connection Closed");

The Code seems to work upto the following line:

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))

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

Many thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Relational Data Using DataSets

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Relational Data Using DataSets

Posted 28 September 2012 - 02:45 PM

It makes that jump because an exception happened - which then caused the application to close - So the form was disposed of - so the dispose() method was the next code to execute.

You need to work out why that line threw an exception, and what the exception was. My first guess would be that one of your objects/variable is is null - or that your index was out of range.


either oDetailRow is null or oOrderRow is - but that's a quick guess without actually building a solution and dumping your code into it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1