I have a simple application for taking Customer orders and their Products. I am using Visual Studio 2008 Express Edition and MS Access 2007.
My application uses multiple forms. One of the forms is an Add form from where a New Customer along with his/her details like address, phone, city, state etc can be added. Another form is for the Product Details to be added.
I want to know how to add Multiple Rows for the same CustomerID record, as a Customer can order multiple products. I am using the following code for the Add button:
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click MyBindingSource.EndEdit() DialogResult = Windows.Forms.DialogResult.OK End Sub
This adds a new Customer into the Database. I have another code for Auto Incrementing the CustomerID.
Now I need to know how to add 2 or more rows for this same CustomerID record.
How can I do this ?
Can anyone help please ?
Satyapriya NayakPosted Dec 9, 2011, 9:53 AM
Try this...
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 Data_relation
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oda;
DataSet ds;
DataRelation dr;
string str;
public Form1()
{
InitializeComponent();
}
private void btndisplay_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from product";
com = new OleDbCommand(str, con);
oda = new OleDbDataAdapter(com);
ds = new DataSet();
oda.Fill(ds, "product");
str = "select * from customer";
com = new OleDbCommand(str, con);
oda = new OleDbDataAdapter(com);
oda.Fill(ds, "customer");
con.Close();
DataColumn dc1 = new DataColumn();
DataColumn dc2 = new DataColumn();
dc1 = ds.Tables["product"].Columns["prodid"];
dc2 = ds.Tables["customer"].Columns["prodid"];
dr = new DataRelation("Customers taking this product are", dc1, dc2);
ds.Relations.Add(dr);
dataGrid1.DataSource = ds;
dataGrid1.DataMember = "product";
}
}
}
Thanks