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

#1 sundog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • 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:

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Relational Data Using DataSets

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5676
  • View blog
  • Posts: 12,194
  • 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.


oDetailRow.SetParentRow(oOrderRow);


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