Click here to Skip to main content
15,884,099 members
Articles / Database Development / SQL Server

Database Helper Class Library to Ease Database Operation

Rate me:
Please Sign up or sign in to vote.
3.09/5 (9 votes)
14 Apr 2007CPOL4 min read 87.4K   3K   57  
Database Helper Class Library to Ease Database Operation
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using DBHelper;
using DBHelper.SqlClient;

namespace DBHelperQuickInsUpdDelSamples
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmInsUpdDel : System.Windows.Forms.Form
	{
		private SqlTableHelper mobjTableHelper;
		private SqlConnectionProvider mobjCnnProvider;
		private System.Windows.Forms.Button mbtnSelectAll;
		private System.Windows.Forms.DataGrid mdgrData;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button mbtnInsert;
		private System.Windows.Forms.TextBox mtxtItemOnHandInsert;
		private System.Windows.Forms.TextBox mtxtItemPriceInsert;
		private System.Windows.Forms.TextBox mtxtItemNameInsert;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox mtxtItemOnHandUpdate;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox mtxtItemPriceUpdate;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox mtxtItemNameUpdate;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.TextBox mtxtItemIDUpdate;
		private System.Windows.Forms.Button mbtnUpdateOne;
		private System.Windows.Forms.Button mbtnUpdateFilter;
		private System.Windows.Forms.Button mbtnUpdateAll;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.TextBox mtxtFilterExpUpd;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.TextBox mtxtFilterExpDel;
		private System.Windows.Forms.TextBox mtxtItemIDDel;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Button mbtnDelAll;
		private System.Windows.Forms.Button mbtnDelFilter;
		private System.Windows.Forms.Button mbtnDelOne;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.Button mbtnDBAdapterUpd;
		private System.Windows.Forms.Label label11;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmInsUpdDel()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//Initialize connection provider
			mobjCnnProvider = new SqlConnectionProvider();
			mobjCnnProvider.ConnectionString = "Initial Catalog=SwComponent;Data Source=amenthyst;UID=sa;Password=vios";
			
			//Initialize table helper
			mobjTableHelper = new SqlTableHelper("x_Item");
			mobjTableHelper.MainConnectionProvider = mobjCnnProvider;
			mobjTableHelper.FieldsToSelect = "ItemID;ItemName;ItemPrice;ItemOnHand";
			mobjTableHelper.Compile();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.mbtnSelectAll = new System.Windows.Forms.Button();
			this.mdgrData = new System.Windows.Forms.DataGrid();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.mbtnInsert = new System.Windows.Forms.Button();
			this.label3 = new System.Windows.Forms.Label();
			this.mtxtItemOnHandInsert = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.mtxtItemPriceInsert = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.mtxtItemNameInsert = new System.Windows.Forms.TextBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label8 = new System.Windows.Forms.Label();
			this.mtxtFilterExpUpd = new System.Windows.Forms.TextBox();
			this.mbtnUpdateAll = new System.Windows.Forms.Button();
			this.mbtnUpdateFilter = new System.Windows.Forms.Button();
			this.mtxtItemIDUpdate = new System.Windows.Forms.TextBox();
			this.label7 = new System.Windows.Forms.Label();
			this.mbtnUpdateOne = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			this.mtxtItemOnHandUpdate = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.mtxtItemPriceUpdate = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.mtxtItemNameUpdate = new System.Windows.Forms.TextBox();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.label9 = new System.Windows.Forms.Label();
			this.mtxtFilterExpDel = new System.Windows.Forms.TextBox();
			this.mbtnDelAll = new System.Windows.Forms.Button();
			this.mbtnDelFilter = new System.Windows.Forms.Button();
			this.mtxtItemIDDel = new System.Windows.Forms.TextBox();
			this.label10 = new System.Windows.Forms.Label();
			this.mbtnDelOne = new System.Windows.Forms.Button();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.mbtnDBAdapterUpd = new System.Windows.Forms.Button();
			this.label11 = new System.Windows.Forms.Label();
			((System.ComponentModel.ISupportInitialize)(this.mdgrData)).BeginInit();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.SuspendLayout();
			// 
			// mbtnSelectAll
			// 
			this.mbtnSelectAll.Location = new System.Drawing.Point(16, 144);
			this.mbtnSelectAll.Name = "mbtnSelectAll";
			this.mbtnSelectAll.Size = new System.Drawing.Size(120, 23);
			this.mbtnSelectAll.TabIndex = 7;
			this.mbtnSelectAll.Text = "Select All Items";
			this.mbtnSelectAll.Click += new System.EventHandler(this.mbtnSelectAll_Click);
			// 
			// mdgrData
			// 
			this.mdgrData.DataMember = "";
			this.mdgrData.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.mdgrData.Location = new System.Drawing.Point(8, 8);
			this.mdgrData.Name = "mdgrData";
			this.mdgrData.Size = new System.Drawing.Size(496, 120);
			this.mdgrData.TabIndex = 6;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.mbtnInsert);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.mtxtItemOnHandInsert);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.mtxtItemPriceInsert);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.mtxtItemNameInsert);
			this.groupBox1.Location = new System.Drawing.Point(16, 176);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(480, 120);
			this.groupBox1.TabIndex = 8;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Insert";
			// 
			// mbtnInsert
			// 
			this.mbtnInsert.Location = new System.Drawing.Point(272, 24);
			this.mbtnInsert.Name = "mbtnInsert";
			this.mbtnInsert.Size = new System.Drawing.Size(104, 23);
			this.mbtnInsert.TabIndex = 6;
			this.mbtnInsert.Text = "Insert";
			this.mbtnInsert.Click += new System.EventHandler(this.mbtnInsert_Click);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 88);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(80, 23);
			this.label3.TabIndex = 5;
			this.label3.Text = "On Hand Qty:";
			// 
			// mtxtItemOnHandInsert
			// 
			this.mtxtItemOnHandInsert.Location = new System.Drawing.Point(88, 88);
			this.mtxtItemOnHandInsert.Name = "mtxtItemOnHandInsert";
			this.mtxtItemOnHandInsert.Size = new System.Drawing.Size(168, 20);
			this.mtxtItemOnHandInsert.TabIndex = 4;
			this.mtxtItemOnHandInsert.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 56);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(80, 23);
			this.label2.TabIndex = 3;
			this.label2.Text = "Item Price:";
			// 
			// mtxtItemPriceInsert
			// 
			this.mtxtItemPriceInsert.Location = new System.Drawing.Point(88, 56);
			this.mtxtItemPriceInsert.Name = "mtxtItemPriceInsert";
			this.mtxtItemPriceInsert.Size = new System.Drawing.Size(168, 20);
			this.mtxtItemPriceInsert.TabIndex = 2;
			this.mtxtItemPriceInsert.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(80, 23);
			this.label1.TabIndex = 1;
			this.label1.Text = "Item Name:";
			// 
			// mtxtItemNameInsert
			// 
			this.mtxtItemNameInsert.Location = new System.Drawing.Point(88, 24);
			this.mtxtItemNameInsert.Name = "mtxtItemNameInsert";
			this.mtxtItemNameInsert.Size = new System.Drawing.Size(168, 20);
			this.mtxtItemNameInsert.TabIndex = 0;
			this.mtxtItemNameInsert.Text = "";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.label8);
			this.groupBox2.Controls.Add(this.mtxtFilterExpUpd);
			this.groupBox2.Controls.Add(this.mbtnUpdateAll);
			this.groupBox2.Controls.Add(this.mbtnUpdateFilter);
			this.groupBox2.Controls.Add(this.mtxtItemIDUpdate);
			this.groupBox2.Controls.Add(this.label7);
			this.groupBox2.Controls.Add(this.mbtnUpdateOne);
			this.groupBox2.Controls.Add(this.label4);
			this.groupBox2.Controls.Add(this.mtxtItemOnHandUpdate);
			this.groupBox2.Controls.Add(this.label5);
			this.groupBox2.Controls.Add(this.mtxtItemPriceUpdate);
			this.groupBox2.Controls.Add(this.label6);
			this.groupBox2.Controls.Add(this.mtxtItemNameUpdate);
			this.groupBox2.Location = new System.Drawing.Point(16, 312);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(480, 152);
			this.groupBox2.TabIndex = 9;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Update";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(272, 56);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(40, 23);
			this.label8.TabIndex = 12;
			this.label8.Text = "Filter: ";
			// 
			// mtxtFilterExpUpd
			// 
			this.mtxtFilterExpUpd.Location = new System.Drawing.Point(320, 56);
			this.mtxtFilterExpUpd.Name = "mtxtFilterExpUpd";
			this.mtxtFilterExpUpd.Size = new System.Drawing.Size(144, 20);
			this.mtxtFilterExpUpd.TabIndex = 11;
			this.mtxtFilterExpUpd.Text = "";
			// 
			// mbtnUpdateAll
			// 
			this.mbtnUpdateAll.Location = new System.Drawing.Point(272, 112);
			this.mbtnUpdateAll.Name = "mbtnUpdateAll";
			this.mbtnUpdateAll.Size = new System.Drawing.Size(192, 23);
			this.mbtnUpdateAll.TabIndex = 10;
			this.mbtnUpdateAll.Text = "Update all rec";
			this.mbtnUpdateAll.Click += new System.EventHandler(this.mbtnUpdateAll_Click);
			// 
			// mbtnUpdateFilter
			// 
			this.mbtnUpdateFilter.Location = new System.Drawing.Point(272, 80);
			this.mbtnUpdateFilter.Name = "mbtnUpdateFilter";
			this.mbtnUpdateFilter.Size = new System.Drawing.Size(192, 23);
			this.mbtnUpdateFilter.TabIndex = 9;
			this.mbtnUpdateFilter.Text = "Update rec using filter expression";
			this.mbtnUpdateFilter.Click += new System.EventHandler(this.mbtnUpdateFilter_Click);
			// 
			// mtxtItemIDUpdate
			// 
			this.mtxtItemIDUpdate.Location = new System.Drawing.Point(88, 24);
			this.mtxtItemIDUpdate.Name = "mtxtItemIDUpdate";
			this.mtxtItemIDUpdate.Size = new System.Drawing.Size(168, 20);
			this.mtxtItemIDUpdate.TabIndex = 8;
			this.mtxtItemIDUpdate.Text = "";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(8, 24);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(72, 23);
			this.label7.TabIndex = 7;
			this.label7.Text = "Item ID:";
			// 
			// mbtnUpdateOne
			// 
			this.mbtnUpdateOne.Location = new System.Drawing.Point(272, 24);
			this.mbtnUpdateOne.Name = "mbtnUpdateOne";
			this.mbtnUpdateOne.Size = new System.Drawing.Size(192, 23);
			this.mbtnUpdateOne.TabIndex = 6;
			this.mbtnUpdateOne.Text = "Update rec using primary key";
			this.mbtnUpdateOne.Click += new System.EventHandler(this.mbtnUpdate_Click);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 118);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(80, 23);
			this.label4.TabIndex = 5;
			this.label4.Text = "On Hand Qty:";
			// 
			// mtxtItemOnHandUpdate
			// 
			this.mtxtItemOnHandUpdate.Location = new System.Drawing.Point(88, 118);
			this.mtxtItemOnHandUpdate.Name = "mtxtItemOnHandUpdate";
			this.mtxtItemOnHandUpdate.Size = new System.Drawing.Size(168, 20);
			this.mtxtItemOnHandUpdate.TabIndex = 4;
			this.mtxtItemOnHandUpdate.Text = "";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 86);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(80, 23);
			this.label5.TabIndex = 3;
			this.label5.Text = "Item Price:";
			// 
			// mtxtItemPriceUpdate
			// 
			this.mtxtItemPriceUpdate.Location = new System.Drawing.Point(88, 86);
			this.mtxtItemPriceUpdate.Name = "mtxtItemPriceUpdate";
			this.mtxtItemPriceUpdate.Size = new System.Drawing.Size(168, 20);
			this.mtxtItemPriceUpdate.TabIndex = 2;
			this.mtxtItemPriceUpdate.Text = "";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(8, 54);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(80, 23);
			this.label6.TabIndex = 1;
			this.label6.Text = "Item Name:";
			// 
			// mtxtItemNameUpdate
			// 
			this.mtxtItemNameUpdate.Location = new System.Drawing.Point(88, 54);
			this.mtxtItemNameUpdate.Name = "mtxtItemNameUpdate";
			this.mtxtItemNameUpdate.Size = new System.Drawing.Size(168, 20);
			this.mtxtItemNameUpdate.TabIndex = 0;
			this.mtxtItemNameUpdate.Text = "";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.label9);
			this.groupBox3.Controls.Add(this.mtxtFilterExpDel);
			this.groupBox3.Controls.Add(this.mbtnDelAll);
			this.groupBox3.Controls.Add(this.mbtnDelFilter);
			this.groupBox3.Controls.Add(this.mtxtItemIDDel);
			this.groupBox3.Controls.Add(this.label10);
			this.groupBox3.Controls.Add(this.mbtnDelOne);
			this.groupBox3.Location = new System.Drawing.Point(16, 480);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(480, 120);
			this.groupBox3.TabIndex = 10;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Delete";
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(8, 56);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(40, 23);
			this.label9.TabIndex = 12;
			this.label9.Text = "Filter: ";
			// 
			// mtxtFilterExpDel
			// 
			this.mtxtFilterExpDel.Location = new System.Drawing.Point(88, 56);
			this.mtxtFilterExpDel.Name = "mtxtFilterExpDel";
			this.mtxtFilterExpDel.Size = new System.Drawing.Size(168, 20);
			this.mtxtFilterExpDel.TabIndex = 11;
			this.mtxtFilterExpDel.Text = "";
			// 
			// mbtnDelAll
			// 
			this.mbtnDelAll.Location = new System.Drawing.Point(272, 88);
			this.mbtnDelAll.Name = "mbtnDelAll";
			this.mbtnDelAll.Size = new System.Drawing.Size(192, 23);
			this.mbtnDelAll.TabIndex = 10;
			this.mbtnDelAll.Text = "Delete all rec";
			this.mbtnDelAll.Click += new System.EventHandler(this.mbtnDelAll_Click);
			// 
			// mbtnDelFilter
			// 
			this.mbtnDelFilter.Location = new System.Drawing.Point(272, 56);
			this.mbtnDelFilter.Name = "mbtnDelFilter";
			this.mbtnDelFilter.Size = new System.Drawing.Size(192, 23);
			this.mbtnDelFilter.TabIndex = 9;
			this.mbtnDelFilter.Text = "Delete rec using filter expression";
			this.mbtnDelFilter.Click += new System.EventHandler(this.mbtnDelFilter_Click);
			// 
			// mtxtItemIDDel
			// 
			this.mtxtItemIDDel.Location = new System.Drawing.Point(88, 24);
			this.mtxtItemIDDel.Name = "mtxtItemIDDel";
			this.mtxtItemIDDel.Size = new System.Drawing.Size(168, 20);
			this.mtxtItemIDDel.TabIndex = 8;
			this.mtxtItemIDDel.Text = "";
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(8, 24);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(72, 23);
			this.label10.TabIndex = 7;
			this.label10.Text = "Item ID:";
			// 
			// mbtnDelOne
			// 
			this.mbtnDelOne.Location = new System.Drawing.Point(272, 24);
			this.mbtnDelOne.Name = "mbtnDelOne";
			this.mbtnDelOne.Size = new System.Drawing.Size(192, 23);
			this.mbtnDelOne.TabIndex = 6;
			this.mbtnDelOne.Text = "Delete rec using primary key";
			this.mbtnDelOne.Click += new System.EventHandler(this.mbtnDelOne_Click);
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.Add(this.label11);
			this.groupBox4.Controls.Add(this.mbtnDBAdapterUpd);
			this.groupBox4.Location = new System.Drawing.Point(504, 176);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(200, 144);
			this.groupBox4.TabIndex = 11;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "DB Adapter Update";
			// 
			// mbtnDBAdapterUpd
			// 
			this.mbtnDBAdapterUpd.Location = new System.Drawing.Point(16, 96);
			this.mbtnDBAdapterUpd.Name = "mbtnDBAdapterUpd";
			this.mbtnDBAdapterUpd.Size = new System.Drawing.Size(144, 32);
			this.mbtnDBAdapterUpd.TabIndex = 0;
			this.mbtnDBAdapterUpd.Text = "Update using standard DB Adpater Update";
			this.mbtnDBAdapterUpd.Click += new System.EventHandler(this.mbtnDBAdapterUpd_Click);
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(8, 24);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(176, 56);
			this.label11.TabIndex = 1;
			this.label11.Text = "Insert, Update, and Delete using data filled in every section. Will perform all a" +
				"ction using standard DBAdapter.Update.";
			// 
			// frmInsUpdDel
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(720, 614);
			this.Controls.Add(this.groupBox4);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.mbtnSelectAll);
			this.Controls.Add(this.mdgrData);
			this.Name = "frmInsUpdDel";
			this.Text = "Insert, Update, or Delete";
			((System.ComponentModel.ISupportInitialize)(this.mdgrData)).EndInit();
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmInsUpdDel());
		}

		private void mbtnSelectAll_Click(object sender, System.EventArgs e)
		{
			DataTable tblData;
			//open connection first, not really neccessary as table helper will open
			//and close connection itself if no available open connection is exists.
			mobjCnnProvider.OpenConnection();

			//It is good to clear table helper instance's data table first b4
			//start select any data
			mobjTableHelper.Data.Clear();
			
			//select all rows and return a data table instance
			tblData = mobjTableHelper.SelectAll();
			mdgrData.DataSource = tblData;

			//close connection
			mobjCnnProvider.CloseConnection();
		}

		private void mbtnInsert_Click(object sender, System.EventArgs e)
		{
			//retrive data table instance from table helper to prepare for insertion
			DataTable tblData = mobjTableHelper.Data;
			tblData.Rows.Clear();
			
			//prepare new row to insert
			DataRow row = tblData.NewRow();
			row["ItemName"] = mtxtItemNameInsert.Text.Trim();
			row["ItemPrice"] = Convert.ToDecimal(mtxtItemPriceInsert.Text);
			row["ItemOnHand"] = Convert.ToInt32(mtxtItemOnHandInsert.Text);
			tblData.Rows.Add(row);

			mobjCnnProvider.OpenConnection();
			mobjTableHelper.Insert();
			mobjCnnProvider.CloseConnection();
		}

		private void mbtnUpdate_Click(object sender, System.EventArgs e)
		{
			DataTable tblData;
			DataRow row;
			
			//update 1 rec for all updated fields
			mobjTableHelper.FieldsToUpdate = null;
			mobjTableHelper.Compile();

			//must clear the records first to avoid confusion with any previous selection
			mobjTableHelper.Data.Rows.Clear();
			//select the records that need to update
			mobjTableHelper.SelectCriteria = String.Format("ItemID = {0}", mtxtItemIDUpdate.Text.Trim());
			tblData = mobjTableHelper.SelectSome();

			row = tblData.Rows[0];

			//update is through data row retrieved from data table instance
			row.BeginEdit();
			row["ItemName"] = mtxtItemNameUpdate.Text.Trim();
			row["ItemPrice"] = Convert.ToDecimal(mtxtItemPriceUpdate.Text);
			row["ItemOnHand"] = Convert.ToInt32(mtxtItemOnHandUpdate.Text);

			//update using primary key
			mobjTableHelper.CriteriaType = DBCriteria.UsePrimaryKey;
		
			mobjCnnProvider.OpenConnection();
			mobjTableHelper.Update();
			mobjCnnProvider.CloseConnection();

			Console.WriteLine("Original:" + row["ItemName", DataRowVersion.Original]);
			Console.WriteLine("Default:" + row["ItemName", DataRowVersion.Default]);

			tblData.AcceptChanges();

			MessageBox.Show(String.Format("{0} rec is updated.", mobjTableHelper.RowsAffected));
		}

		private void mbtnUpdateFilter_Click(object sender, System.EventArgs e)
		{
			DataTable tblData;
			DataRow row;
			
			//update some recs for item price field ONLY
			mobjTableHelper.FieldsToUpdate = "ItemPrice";
			mobjTableHelper.Compile();

			//must clear the records first to avoid confusion with any previous selection
			tblData = mobjTableHelper.Data;
			tblData.Rows.Clear();

			//prepare row for update using filter expression
			row = tblData.NewRow();
			row["ItemPrice"] = Convert.ToDecimal(mtxtItemPriceUpdate.Text);
			tblData.Rows.Add(row);

			//update using primary key
			mobjTableHelper.CriteriaType = DBCriteria.UseFilterExpression;
			mobjTableHelper.UpdateCriteria = mtxtFilterExpUpd.Text.Trim();
		
			mobjCnnProvider.OpenConnection();
			mobjTableHelper.Update();
			mobjCnnProvider.CloseConnection();

			MessageBox.Show(String.Format("{0} rec is updated.", mobjTableHelper.RowsAffected));
		}

		private void mbtnUpdateAll_Click(object sender, System.EventArgs e)
		{
			DataTable tblData;
			DataRow row;
			
			//update some recs for item price field ONLY
			mobjTableHelper.FieldsToUpdate = "ItemPrice";
			mobjTableHelper.Compile();

			//must clear the records first to avoid confusion with any previous selection
			tblData = mobjTableHelper.Data;
			tblData.Rows.Clear();

			//prepare row for update all recs
			row = tblData.NewRow();
			row["ItemPrice"] = Convert.ToDecimal(mtxtItemPriceUpdate.Text);
			tblData.Rows.Add(row);

			//update using primary key
			mobjTableHelper.CriteriaType = DBCriteria.None;
		
			mobjCnnProvider.OpenConnection();
			mobjTableHelper.Update();
			mobjCnnProvider.CloseConnection();

			MessageBox.Show(String.Format("{0} rec is updated.", mobjTableHelper.RowsAffected));
		}

		private void mbtnDelOne_Click(object sender, System.EventArgs e)
		{
			DataTable tblData;
			DataRow row;

			//clear records in data table instance first to avoid confusion with previous selection
			tblData = mobjTableHelper.Data;
			tblData.Rows.Clear();

			//prepare data row to delete
			row = tblData.NewRow();
			row["ItemID"] = Convert.ToInt32(mtxtItemIDDel.Text.Trim());
			tblData.Rows.Add(row);

			//delete using primary key
			mobjTableHelper.CriteriaType = DBCriteria.UsePrimaryKey;

			mobjCnnProvider.OpenConnection();
			mobjTableHelper.Delete();
			mobjCnnProvider.CloseConnection();

			MessageBox.Show(String.Format("{0} rec is deleted.", mobjTableHelper.RowsAffected));
		}

		private void mbtnDelFilter_Click(object sender, System.EventArgs e)
		{
			DataTable tblData;
			DataRow row;

			//clear records in data table instance first to avoid confusion with previous selection
			tblData = mobjTableHelper.Data;
			tblData.Rows.Clear();

			//delete using filter expression
			mobjTableHelper.CriteriaType = DBCriteria.UseFilterExpression;
			mobjTableHelper.DeleteCriteria = mtxtFilterExpDel.Text.Trim();

			mobjCnnProvider.OpenConnection();
			mobjTableHelper.Delete();
			mobjCnnProvider.CloseConnection();

			MessageBox.Show(String.Format("{0} rec is deleted.", mobjTableHelper.RowsAffected));		
		}

		private void mbtnDelAll_Click(object sender, System.EventArgs e)
		{
			DataTable tblData;
			DataRow row;

			//clear records in data table instance first to avoid confusion with previous selection
			tblData = mobjTableHelper.Data;
			tblData.Rows.Clear();

			//delete all, so must set criteria type to NONE
			mobjTableHelper.CriteriaType = DBCriteria.None;

			mobjCnnProvider.OpenConnection();
			mobjTableHelper.Delete();
			mobjCnnProvider.CloseConnection();

			MessageBox.Show(String.Format("{0} rec is deleted.", mobjTableHelper.RowsAffected));		
		}

		private void mbtnDBAdapterUpd_Click(object sender, System.EventArgs e)
		{
			DataTable tblData;
			DataRow row;
			
			tblData = mobjTableHelper.SelectAll();

			//prepare new row to insert
			row = tblData.NewRow();
			row["ItemName"] = mtxtItemNameInsert.Text.Trim();
			row["ItemPrice"] = Convert.ToDecimal(mtxtItemPriceInsert.Text);
			row["ItemOnHand"] = Convert.ToInt32(mtxtItemOnHandInsert.Text);
			tblData.Rows.Add(row);

			//update 1 row
			row = tblData.Select(String.Format("ItemID = {0}", mtxtItemIDUpdate.Text.Trim()))[0];
			row["ItemName"] = mtxtItemNameUpdate.Text.Trim();
			row["ItemPrice"] = Convert.ToDecimal(mtxtItemPriceUpdate.Text);
			row["ItemOnHand"] = Convert.ToInt32(mtxtItemOnHandUpdate.Text);

			//delete 1 row
			row = tblData.Select(String.Format("ItemID = {0}", mtxtItemIDDel.Text.Trim()))[0];
			row.Delete();

			//insert, update, and delete in 1 action
			mobjTableHelper.DBAdapterUpdate();
			tblData.AcceptChanges();

			MessageBox.Show(String.Format("{0} rec is updated.", mobjTableHelper.RowsAffected));
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Malaysia Malaysia
Had worked as analyst programmer for 4 years. Now helping in family business but still involved actively in .Net development whenever there is a free time.

Comments and Discussions