Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Multitabled DataSet App

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Configuration;   public class MainForm : Form   {     private DataSet yourDBDS = new DataSet("YourDB");     private SqlCommandBuilder sqlCBInventory;     private SqlCommandBuilder sqlCBCustomers;     private SqlCommandBuilder sqlCBOrders;     private SqlDataAdapter invTableAdapter;     private SqlDataAdapter custTableAdapter;     private SqlDataAdapter ordersTableAdapter;     private string cnStr = "SqlProvider";     public MainForm()     {       InitializeComponent();              invTableAdapter = new SqlDataAdapter("Select * from Inventory", cnStr);       custTableAdapter = new SqlDataAdapter("Select * from Customers", cnStr);       ordersTableAdapter = new SqlDataAdapter("Select * from Orders", cnStr);       sqlCBInventory = new SqlCommandBuilder(invTableAdapter);       sqlCBOrders = new SqlCommandBuilder(ordersTableAdapter);       sqlCBCustomers = new SqlCommandBuilder(custTableAdapter);       invTableAdapter.Fill(yourDBDS, "Inventory");       custTableAdapter.Fill(yourDBDS, "Customers");       ordersTableAdapter.Fill(yourDBDS, "Orders");       BuildTableRelationship();       dataGridViewInventory.DataSource = yourDBDS.Tables["Inventory"];       dataGridViewCustomers.DataSource = yourDBDS.Tables["Customers"];       dataGridViewOrders.DataSource = yourDBDS.Tables["Orders"];     }     private void BuildTableRelationship()     {       DataRelation dr = new DataRelation("CustomerOrder",yourDBDS.Tables["Customers"].Columns["CustID"],yourDBDS.Tables["Orders"].Columns["CustID"]);       yourDBDS.Relations.Add(dr);       dr = new DataRelation("InventoryOrder",yourDBDS.Tables["Inventory"].Columns["CarID"],yourDBDS.Tables["Orders"].Columns["CarID"]);       yourDBDS.Relations.Add(dr);     }     private void btnUpdate_Click(object sender, EventArgs e)     {       try       {         invTableAdapter.Update(yourDBDS, "Inventory");         custTableAdapter.Update(yourDBDS, "Customers");         ordersTableAdapter.Update(yourDBDS, "Orders");       }       catch (Exception ex)       {         MessageBox.Show(ex.Message);       }     }     private void btnGetOrderInfo_Click(object sender, System.EventArgs e)     {       string strOrderInfo = string.Empty;       DataRow[] drsCust = null;       DataRow[] drsOrder = null;       int custID = int.Parse(this.txtCustID.Text);       drsCust = yourDBDS.Tables["Customers"].Select(string.Format("CustID = {0}", custID));       strOrderInfo += string.Format("Customer {0}: {1} {2}\n",          drsCust[0]["CustID"].ToString(),         drsCust[0]["FirstName"].ToString().Trim(),         drsCust[0]["LastName"].ToString().Trim());       drsOrder = drsCust[0].GetChildRows(yourDBDS.Relations["CustomerOrder"]);       foreach (DataRow r in drsOrder)         strOrderInfo += string.Format("Order Number: {0}\n", r["OrderID"]);       DataRow[] drsInv = drsOrder[0].GetParentRows(yourDBDS.Relations["InventoryOrder"]);       foreach (DataRow r in drsInv)       {         strOrderInfo += r["Make"];         strOrderInfo += r["Color"];         strOrderInfo += r["PetName"];       }       MessageBox.Show(strOrderInfo, "Order Details");     }         private void InitializeComponent()         {           this.dataGridViewInventory = new System.Windows.Forms.DataGridView();           this.label1 = new System.Windows.Forms.Label();           this.label2 = new System.Windows.Forms.Label();           this.dataGridViewCustomers = new System.Windows.Forms.DataGridView();           this.label3 = new System.Windows.Forms.Label();           this.dataGridViewOrders = new System.Windows.Forms.DataGridView();           this.btnUpdateDatabase = new System.Windows.Forms.Button();           this.btnGetOrderInfo = new System.Windows.Forms.Button();           this.txtCustID = new System.Windows.Forms.TextBox();           this.label4 = new System.Windows.Forms.Label();           this.groupBox1 = new System.Windows.Forms.GroupBox();           ((System.ComponentModel.ISupportInitialize)(this.dataGridViewInventory)).BeginInit();           ((System.ComponentModel.ISupportInitialize)(this.dataGridViewCustomers)).BeginInit();           ((System.ComponentModel.ISupportInitialize)(this.dataGridViewOrders)).BeginInit();           this.groupBox1.SuspendLayout();           this.SuspendLayout();           //            // dataGridViewInventory           //            this.dataGridViewInventory.Location = new System.Drawing.Point(12, 34);           this.dataGridViewInventory.Name = "dataGridViewInventory";           this.dataGridViewInventory.Size = new System.Drawing.Size(509, 82);           this.dataGridViewInventory.TabIndex = 0;           this.dataGridViewInventory.Text = "dataGridView1";           //            // label1           //            this.label1.AutoSize = true;           this.label1.Location = new System.Drawing.Point(11, 18);           this.label1.Name = "label1";           this.label1.Size = new System.Drawing.Size(88, 13);           this.label1.TabIndex = 1;           this.label1.Text = "Current Inventory";           //            // label2           //            this.label2.AutoSize = true;           this.label2.Location = new System.Drawing.Point(12, 126);           this.label2.Name = "label2";           this.label2.Size = new System.Drawing.Size(93, 13);           this.label2.TabIndex = 3;           this.label2.Text = "Current Customers";           //            // dataGridViewCustomers           //            this.dataGridViewCustomers.Location = new System.Drawing.Point(13, 142);           this.dataGridViewCustomers.Name = "dataGridViewCustomers";           this.dataGridViewCustomers.Size = new System.Drawing.Size(509, 90);           this.dataGridViewCustomers.TabIndex = 2;           this.dataGridViewCustomers.Text = "dataGridView2";           //            // label3           //            this.label3.AutoSize = true;           this.label3.Location = new System.Drawing.Point(12, 244);           this.label3.Name = "label3";           this.label3.Size = new System.Drawing.Size(75, 13);           this.label3.TabIndex = 5;           this.label3.Text = "Current Orders";           //            // dataGridViewOrders           //            this.dataGridViewOrders.Location = new System.Drawing.Point(13, 260);           this.dataGridViewOrders.Name = "dataGridViewOrders";           this.dataGridViewOrders.Size = new System.Drawing.Size(509, 90);           this.dataGridViewOrders.TabIndex = 4;           this.dataGridViewOrders.Text = "dataGridView3";           //            // btnUpdateDatabase           //            this.btnUpdateDatabase.Location = new System.Drawing.Point(407, 368);           this.btnUpdateDatabase.Name = "btnUpdateDatabase";           this.btnUpdateDatabase.Size = new System.Drawing.Size(114, 23);           this.btnUpdateDatabase.TabIndex = 6;           this.btnUpdateDatabase.Text = "Update Database";           this.btnUpdateDatabase.Click += new System.EventHandler(this.btnUpdate_Click);           //            // btnGetOrderInfo           //            this.btnGetOrderInfo.Location = new System.Drawing.Point(84, 71);           this.btnGetOrderInfo.Name = "btnGetOrderInfo";           this.btnGetOrderInfo.Size = new System.Drawing.Size(110, 23);           this.btnGetOrderInfo.TabIndex = 7;           this.btnGetOrderInfo.Text = "Get Order Details";           this.btnGetOrderInfo.Click += new System.EventHandler(this.btnGetOrderInfo_Click);           //            // txtCustID           //            this.txtCustID.Location = new System.Drawing.Point(84, 29);           this.txtCustID.Name = "txtCustID";           this.txtCustID.Size = new System.Drawing.Size(110, 20);           this.txtCustID.TabIndex = 8;           //            // label4           //            this.label4.AutoSize = true;           this.label4.Location = new System.Drawing.Point(10, 29);           this.label4.Name = "label4";           this.label4.Size = new System.Drawing.Size(68, 13);           this.label4.TabIndex = 9;           this.label4.Text = "Customer ID:";           //            // groupBox1           //            this.groupBox1.Controls.Add(this.btnGetOrderInfo);           this.groupBox1.Controls.Add(this.label4);           this.groupBox1.Controls.Add(this.txtCustID);           this.groupBox1.Location = new System.Drawing.Point(15, 368);           this.groupBox1.Name = "groupBox1";           this.groupBox1.Size = new System.Drawing.Size(200, 100);           this.groupBox1.TabIndex = 10;           this.groupBox1.TabStop = false;           this.groupBox1.Text = "Lookup Customer Order";           //            // MainForm           //            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);           this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;           this.ClientSize = new System.Drawing.Size(534, 480);           this.Controls.Add(this.groupBox1);           this.Controls.Add(this.btnUpdateDatabase);           this.Controls.Add(this.label3);           this.Controls.Add(this.dataGridViewOrders);           this.Controls.Add(this.label2);           this.Controls.Add(this.dataGridViewCustomers);           this.Controls.Add(this.label1);           this.Controls.Add(this.dataGridViewInventory);           this.Name = "MainForm";           this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;           this.Text = "YourDB Database Manipulator";           ((System.ComponentModel.ISupportInitialize)(this.dataGridViewInventory)).EndInit();           ((System.ComponentModel.ISupportInitialize)(this.dataGridViewCustomers)).EndInit();           ((System.ComponentModel.ISupportInitialize)(this.dataGridViewOrders)).EndInit();           this.groupBox1.ResumeLayout(false);           this.groupBox1.PerformLayout();           this.ResumeLayout(false);           this.PerformLayout();         }         private System.Windows.Forms.DataGridView dataGridViewInventory;         private System.Windows.Forms.Label label1;         private System.Windows.Forms.Label label2;         private System.Windows.Forms.DataGridView dataGridViewCustomers;         private System.Windows.Forms.Label label3;         private System.Windows.Forms.DataGridView dataGridViewOrders;         private System.Windows.Forms.Button btnUpdateDatabase;         private System.Windows.Forms.Button btnGetOrderInfo;         private System.Windows.Forms.TextBox txtCustID;       private System.Windows.Forms.Label label4;       private System.Windows.Forms.GroupBox groupBox1;         [STAThread]         static void Main()         {             Application.EnableVisualStyles();             Application.Run(new MainForm());         }     }