Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


BookStock v2 - Samples to Show Simple OleDb.NET Connectivity

Add Comment
 

 
<div align="center"> <table border="0" width="70%" class="outline"> <tr> <td width="25%" class="outline"><b>Download File</b></td> <td width="25%" class="outline"><b>SDK Version</b></td> </tr> <tr> <td width="25%" class="outline"><a href="../../file/bookstockv2.zip" class="wbox">bookstockv2.zip</a> (41kb)</td> <td width="25%" class="outline">beta2</td> </tr> </table> </div> <p> <span class="wboxheado">Introduction</span><br> The <a href="article.aspx?ArticleID=3&amp;&amp;TopicID=1" class="wbox"> bookstock example</a> I had previously created for .NET SDK beta1 consisted of 3 samples to show how to perform basic ADO.NET connectivity with a Ms Access 2000 Database and how to Add, View , Edit , Delete records from it. It also demonstrates how to <b>DataBind</b> Textboxes to a <b>DataSet</b> object. Since a lot of changes have been made from beta1 to beta2, I have updated this sample to show what are the necessary changes required to convert the code from beta1 to beta2. As you would know that in Beta2 there is no <i> System.Data.ADO namespace</i> but it has been replaced by <i>System.Data.OleDb</i> namespace. Also the <i> System.WinForms </i> namespace has been changed to <i>System.Windows.Forms</i>. Converting the beta1 code to beta2 was very easy and I had to just run a few &quot;<i>Replace</i>&quot; operations on the code. One minor change that has occurred although is the change to the DataBinding method of the Controls. Now we no longer use <i>ListManager</i>, we use <b>BindingsManagerBase</b> class to manage DataBinding of controls. To help you have the exact view of what has changed from beta1 to beta2, in my examples I have made the minimum possible change to make the code working on beta2. Review both the codes side by side to have a bigger picture!</p> <p>Some major changes that have occurred from beta1 to beta2 <table border="0" width="100%" class="outline"> <tr> <td width="50%" class="outline"><b>.NET SDK Beta1</b></td> <td width="50%" class="outline"><b>.NET SDK Beta2</b></td> </tr> <tr> <td width="50%" class="outline">System.WinForms Namespace</td> <td width="50%" class="outline">System.Windows.Forms Namespace</td> </tr> <tr> <td width="50%" class="outline">System.ADO Namespace</td> <td width="50%" class="outline">System.OleDb Namespace</td> </tr> <tr> <td width="50%" class="outline">ADOConnection class</td> <td width="50%" class="outline">OleDbConnection class</td> </tr> <tr> <td width="50%" class="outline">ADOCommand class</td> <td width="50%" class="outline">OleDbCommand class</td> </tr> <tr> <td width="50%" class="outline">ADODataSetCommand class</td> <td width="50%" class="outline">OleDbDataAdapter class</td> </tr> <tr> <td width="50%" class="outline">ADODataReader class</td> <td width="50%" class="outline">OleDbDataReader class</td> </tr> <tr> <td width="50%" class="outline">ListManager class</td> <td width="50%" class="outline">BindingsManagerBase class</td> </tr> </table> <p>&nbsp;&nbsp;&nbsp;&nbsp;</p> <p><span class="wboxheado">Requirements</span><br> 1) .NET SDK Beta2 (This example might not work with the other versions.)<br> 2) Ms Access 2000 (Optional, required only if you want to change the database design.)</p> <span class="wboxheado">Database Design</span> <table border="0" width="90%" class="Outline"> <tr> <td width="100%" colspan="3" class="Outline"> <span class="wboxhead">Table Name - bookstock / File Name - book.mdb</span></td> </tr> <tr> <td width="20%" class="Outline"><b>Column Name</b></td> <td width="15%" class="Outline"><b>Data Type</b></td> <td width="65%" class="Outline"><b>Description</b></td> </tr> <tr> <td width="20%" class="Outline">bookid</td> <td width="15%" class="Outline">Integer</td> <td width="65%" class="Outline">Primary Key Field. Unique identity number of a book.</td> </tr> <tr> <td width="20%" class="Outline">booktitle</td> <td width="15%" class="Outline">Text</td> <td width="65%" class="Outline">Title of the book.</td> </tr> <tr> <td width="20%" class="Outline">bookauthor</td> <td width="15%" class="Outline">Text</td> <td width="65%" class="Outline">Author of the book.&nbsp;</td> </tr> <tr> <td width="20%" class="Outline">bookprice</td> <td width="15%" class="Outline">Integer</td> <td width="65%" class="Outline">Price of the book.</td> </tr> <tr> <td width="20%" class="Outline">bookstock</td> <td width="15%" class="Outline">Integer</td> <td width="65%" class="Outline">Stock available of the book.</td> </tr> </table> &nbsp; <p> <span class=wboxheado>Code</span><br> 1) <I><b>DataAdd.cs</b> :- Add Records in to the Database (only relevant code shown).</I> </p> <p><img border="0" src="../../img/bookstockdataadd.gif" width="400" height="400"></p> <table border="0" width="100%" class="Code" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <pre>namespace SaurabhData { using System; using System.Drawing; using System.Windows.Forms;<span class="Cmt">//A Change to System.WinForms in beta1</span> using System.Data.OleDb; <span class="Cmt">//A change to System.Data.ADO in beta1</span> using System.Data; using System.Threading ; <span class="Cmt">///&lt;summary&gt; /// Class to add data in to a Ms Access 2000 database /// book.mdb using OleDb.NET . ///&lt;/summary&gt;</span> public class DataAdd :Form { <span class="Cmt">/// &lt;summary&gt; /// Required by the Win Forms designer /// &lt;/summary&gt;</span> private System.ComponentModel.Container components; private System.Windows.Forms.Label title; private System.Windows.Forms.StatusBar statusBar; private System.Windows.Forms.Button helpme; private System.Windows.Forms.Button save; private System.Windows.Forms.TextBox t_bookstock; private System.Windows.Forms.TextBox t_bookprice; private System.Windows.Forms.TextBox t_bookauthor; private System.Windows.Forms.TextBox t_booktitle; private System.Windows.Forms.TextBox t_bookid; private System.Windows.Forms.Label l_bookstock; private System.Windows.Forms.Label l_bookprice; private System.Windows.Forms.Label l_bookauthor; private System.Windows.Forms.Label l_booktitle; private System.Windows.Forms.Label l_bookid; <span class="Cmt">///&lt;summary&gt; /// The Constructor of the class DataAdd /// &lt;para&gt; /// This constructor calls 2 methods InitializeComponent() /// to initialize the WinForm Components and /// starts a thread on the Method GetConnected() which /// connects to the Database and returns the number of records /// &lt;/para&gt; ///&lt;/summary&gt;</span> public DataAdd() { <span class="Cmt">// Required for Win Form Designer support</span> InitializeComponent(); <span class="Cmt">//put the connection to the database on a Thread so the Form //displays quickly..</span> ThreadStart tsgc = new ThreadStart(GetConnected) ; Thread tgc = new Thread(tsgc) ; tgc.Start() ; <span class="Cmt">//if you don't want threading then omit the above 3 lines and add //the below line //GetConnected();</span> } <span class="Cmt">/// &lt;summary&gt; /// Clean up any resources being used /// &lt;/summary&gt;</span> public override void Dispose() { base.Dispose(); components.Dispose(); } <span class="Cmt">/// &lt;summary&gt; /// The main entry point for the application. /// &lt;/summary&gt;</span> public static void Main(string[] args) { Application.Run(new DataAdd()); } <span class="Cmt">///&lt;summary&gt; /// &lt;para&gt; /// Required method to get connected with the Database /// and update the Book Id. textbox with the current number /// of records in the database plus one. /// &lt;/para&gt; ///&lt;/summary&gt;</span> public void GetConnected() { <span class="Cmt">//the code below is to connect to the database. //It is put inside a try-catch block to Catch any exceptions //that can occur while connecting to the database.</span> try{ statusBar.Text=&quot;Please Wait, Connecting ....&quot; ; string strConn=&quot;Provider=Microsoft.Jet.OLEDB.4.0;&quot;+ strConn+=&quot;Data Source=book.mdb&quot; ; <span class="Cmt">//Make the Connection Object</span> OleDbConnection myConn = new OleDbConnection(strConn) ; <span class="Cmt">//Make a Select Command</span> string strCom = &quot;Select bookid from bookstock&quot; ; OleDbCommand myCommand =new OleDbCommand(strCom,myConn); myConn.Open(); OleDbDataReader reader; <span class="Cmt">//Execute the command and get the DataReader //This has changed from beta1 where we used //myCommand.ExecuteReader(out reader)</span> reader =myCommand.ExecuteReader() ; int i=0 ; <span class="Cmt">//Get the current number of records present in the database.</span> while(reader.Read()) { i++ ; } i++ ; <span class="Cmt">//update the Book Id textbox with the Number of records present //plus one.</span> t_bookid.Text = i.ToString() ; statusBar.Text=&quot;Connected - Now you can Add records&quot;; reader.Close(); myConn.Close(); } catch(Exception e) { MessageBox.Show(&quot;Error in connecting! &quot;+e.ToString(), &quot;Error&quot;); } } <span class="Cmt">/// &lt;summary&gt; /// Required method for Designer support - do not modify /// the contents of this method with an editor /// &lt;/summary&gt;</span> private void InitializeComponent() { <span class="Cmt">//The code here is used to design the form //Please download the file for the complete code</span> } <span class="Cmt">///&lt;summary&gt; /// &lt;para&gt; /// This method is called when the &quot;Save&quot; Button is Clicked. /// It checks if Data is entered into all the fields, if 'yes' /// then it proceeds with opening an connection with /// the database and inserting the new data in it. /// &lt;/para&gt; ///&lt;/summary&gt;</span> protected void saveClick(object sender, System.EventArgs e) { <span class="Cmt">//code to save the inputted data in to the database //No code to validate the data implemented</span> try { if(t_bookid.Text!=&quot;&quot;&amp;&amp;t_booktitle.Text!=&quot;&quot;&amp;&amp;t_bookauthor.Text!=&quot;&quot; &amp;&amp;t_bookprice.Text!=&quot;&quot;&amp;&amp;t_bookstock.Text!=&quot;&quot;) { string strConn=&quot;Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=book.mdb&quot;; OleDbConnection myConn = new OleDbConnection(strConn) ; myConn.Open(); <span class="Cmt">//the string to get values from the textboxes and form an &quot;INSERT INTO&quot; // statement.</span> string strInsert = &quot;INSERT INTO bookstock (bookid, booktitle, bookauthor, &quot;; strInsert+=&quot;bookprice, bookstock) VALUES ( &quot;; strInsert += t_bookid.Text+&quot;, '&quot;; strInsert += t_booktitle.Text+&quot;', '&quot;; strInsert += t_bookauthor.Text+&quot;', &quot;; strInsert += t_bookprice.Text+&quot;, &quot;; strInsert += t_bookstock.Text+&quot;)&quot;; OleDbCommand inst = new OleDbCommand(strInsert,myConn) ; <span class="Cmt">//Execute the statement</span> inst.ExecuteNonQuery() ; statusBar.Text=&quot;Data Added to Database &quot; ; <span class="Cmt">//reset all the textboxes</span> int i=int.Parse(t_bookid.Text); i++; t_bookid.Text=i.ToString() ; t_booktitle.Text=&quot;&quot; ; t_bookauthor.Text=&quot;&quot; ; t_bookprice.Text=&quot;&quot; ; t_bookstock.Text=&quot;&quot; ; statusBar.Text=&quot;Connected - Now you can Add records&quot;; myConn.Close() ; } else { MessageBox.Show(&quot;All fields must be completed.&quot;, &quot;Error&quot;); } } catch(Exception ed) { MessageBox.Show(&quot;Error in Saving &quot;+ed.ToString(), &quot;Error&quot;); } } <span class="Cmt">///&lt;summary&gt; /// This method is called when the help button is clicked. ///&lt;/summary&gt;</span> protected void helpClick(object sender, System.EventArgs e) { MessageBox.Show(&quot;Book Stock for .NET SDK beta2- Data Addtion program, by Saurabh Nandu, http://www.MasterCSharp.com&quot;, &quot;About ...&quot;); } } }</pre> </td> </tr> </table> <P>&nbsp;</P> <P>2)<I> <b>DataView.cs</b>:- View Records from Database and DataBind (Only relevant code).</I></P> <P><img border="0" src="../../img/bookstockdataview.gif" width="400" height="400"></P> <table border="0" width="100%" class="Code"> <tr> <td width="100%"> <pre>namespace SaurabhData { using System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; using System.Data.OleDb; using System.Data; <span class="Cmt">///&lt;summary&gt; /// Class to demonstrate how to view data from a database using OleDb.NET /// This example also uses data binding. ///&lt;/summary&gt;</span> public class DataView : System.Windows.Forms.Form { <span class="Cmt">/// &lt;summary&gt; /// Required by the Win Forms designer /// &lt;/summary&gt;</span> private System.ComponentModel.Container components; private System.Windows.Forms.Button helpme; private System.Windows.Forms.Button lastrec; private System.Windows.Forms.Button nextrec; private System.Windows.Forms.Button previousrec; private System.Windows.Forms.Button firstrec; private System.Windows.Forms.TextBox t_bookstock; private System.Windows.Forms.TextBox t_bookprice; private System.Windows.Forms.TextBox t_bookauthor; private System.Windows.Forms.TextBox t_booktitle; private System.Windows.Forms.TextBox t_bookid; private System.Windows.Forms.Label l_bookstock; private System.Windows.Forms.Label l_bookprice; private System.Windows.Forms.Label l_bookauthor; private System.Windows.Forms.Label l_booktitle; private System.Windows.Forms.Label l_bookid; private System.Windows.Forms.Label label1; private System.Windows.Forms.StatusBar statusBar; private System.Data.DataSet myDataSet ; private BindingManagerBase myBind; <span class="Cmt">//This replaces ListManager from beta1</span> <span class="Cmt">///&lt;summary&gt; /// &lt;para&gt; /// This is the constructor of the class which calls 2 methods. /// GetConnected() to connect to the database and get the data /// into a database. /// &lt;/para&gt; /// &lt;para&gt; /// InitilizeComponents() this method is called to initialize the Form. /// &lt;/para&gt; ///&lt;/summary&gt;</span> public DataView() { <span class="Cmt">//Connect to the Database.</span> GetConnected() ; <span class="Cmt">// Required for Win Form Designer support</span> InitializeComponent(); } <span class="Cmt">/// &lt;summary&gt; /// Clean up any resources being used /// &lt;/summary&gt;</span> public override void Dispose() { base.Dispose(); components.Dispose(); } <span class="Cmt">/// &lt;summary&gt; /// The main entry point for the application. /// &lt;/summary&gt;</span> public static void Main(string[] args) { Application.Run(new DataView()); } <span class="Cmt">///&lt;summary&gt; /// This method connects to the database and returns a Dataset ///&lt;/summary&gt;</span> public void GetConnected() { try{ <span class="Cmt">//make a OleDbConnection</span> string strCon=&quot;Provider=Microsoft.Jet.OLEDB.4.0 ;&quot;; strCon+=Data Source=book.mdb&quot; ; OleDbConnection myConn=new OleDbConnection(strCon) ; string strCom=&quot;SELECT * FROM bookstock&quot; ; <span class="Cmt">//Make a DataSet</span> myDataSet = new DataSet() ; myConn.Open() ; <span class="Cmt">//Using the OleDbDataAdapter execute the </span><span class="Cmt">query</span> OleDbDataAdapter myCommand=new OleDbDataAdapter(strCom,myConn); <span class="Cmt">//Fill the Data set with the Table 'bookstock'</span> myCommand.Fill(myDataSet,&quot;bookstock&quot;) ; <span class="Cmt">//Close the OleDbConnection</span> myConn.Close() ; } catch(Exception e) { MessageBox.Show(&quot;Error in connecting! &quot;+e.ToString(), &quot;Error&quot;); } } <span class="Cmt">/// &lt;summary&gt; /// Required method for Designer support - do not modify /// the contents of this method with an editor /// &lt;/summary&gt;</span> private void InitializeComponent() { <span class="Cmt">//Only DataBinding Code shown</span> t_bookid.Location = new System.Drawing.Point(184, 56); t_bookid.TabIndex = 0; t_bookid.Size = new System.Drawing.Size(80, 20); <span class="Cmt">//DataBind the TextBox using the &quot;DataBindings&quot; property of the //textbox control. Since we are DataBinding to the &quot;Text&quot; property //of the TextBox we pass it as the first parameter //The DataSet object is passed as the second parameter //The TableName with the FieldName is passed as the third </span><span class="Cmt">parameter</span> t_bookid.DataBindings.Add(&quot;Text&quot;, myDataSet, &quot;bookstock.bookid&quot;); t_bookstock.Location = new System.Drawing.Point(184, 264); t_bookstock.TabIndex = 4; t_bookstock.Size = new System.Drawing.Size(80, 20); t_bookstock.DataBindings.Add(&quot;Text&quot;, myDataSet, &quot;bookstock.bookstock&quot;); t_booktitle.Location = new System.Drawing.Point(184, 108); t_booktitle.TabIndex = 1; t_booktitle.Size = new System.Drawing.Size(176, 20); t_booktitle.DataBindings.Add(&quot;Text&quot;, myDataSet, &quot;bookstock.booktitle&quot;); t_bookprice.Location = new System.Drawing.Point(184, 212); t_bookprice.TabIndex = 3; t_bookprice.Size = new System.Drawing.Size(80, 20); t_bookprice.DataBindings.Add(&quot;Text&quot;, myDataSet, &quot;bookstock.bookprice&quot;); t_bookauthor.Location = new System.Drawing.Point(184, 160); t_bookauthor.TabIndex = 2; t_bookauthor.Size = new System.Drawing.Size(128, 20); t_bookauthor.DataBindings.Add(&quot;Text&quot;, myDataSet, &quot;bookstock.bookauthor&quot;); <span class="Cmt">//DataBind the Controls with the DataSet object //and 'bookstock' table</span> myBind= this.BindingContext [myDataSet, &quot;bookstock&quot;]; } <span class="Cmt">///&lt;summary&gt; /// To navigate the BindingManagerBase, increment the Position property. ///&lt;/summary&gt;</span> private void MoveNext() { if (myBind.Position == myBind.Count -1) MessageBox.Show(&quot;End of records&quot;); else myBind.Position += 1; } <span class="Cmt">///&lt;summary&gt; /// To navigate the BindingManagerBase, decrement the Position property. ///&lt;/summary&gt;</span> private void MovePrevious(){ if (myBind.Position == 0) MessageBox.Show(&quot;First record&quot;); else myBind.Position -= 1; } <span class="Cmt">///&lt;summary&gt; /// Move to position 0 in the list. ///&lt;/summary&gt;</span> private void MoveFirst(){ myBind.Position = 0; } <span class="Cmt">///&lt;summary&gt; /// Move to the count -1 position. ///&lt;/summary&gt;</span> private void MoveLast(){ myBind.Position = myBind.Count - 1; } <span class="Cmt">///&lt;summary&gt; /// Get Help ///&lt;/summary&gt;</span> protected void GoHelp(object sender, System.EventArgs e) { MessageBox.Show(&quot;Book Stock- Data View for .NET SDK beta2, by Saurabh Nandu, http://www.MasterCSharp.com&quot;, &quot;About ...&quot;); } <span class="Cmt">///&lt;summary&gt; /// Last Record Button Clicked ///&lt;/summary&gt;</span> protected void GoLast(object sender, System.EventArgs e) { MoveLast(); } <span class="Cmt">///&lt;summary&gt; /// Next Record Button Clicked ///&lt;/summary&gt;</span> protected void GoNext(object sender, System.EventArgs e) { MoveNext(); } <span class="Cmt">///&lt;summary&gt; /// Previous Record Button Clicked ///&lt;/summary&gt;</span> protected void GoPrevious(object sender, System.EventArgs e) { MovePrevious(); } <span class="Cmt">///&lt;summary&gt; /// First Record Button Clicked ///&lt;/summary&gt;</span> protected void GoFirst(object sender, System.EventArgs e) { MoveFirst(); } } }</pre> </td> </tr> </table><P>&nbsp;</P> <P>3) <I><b>DataEdit.cs</b> :- View, Edit, Delete Records in to the Database (only relevant code shown).</I></P> <P><img border="0" src="../../img/bookstockdataedit.gif" width="400" height="450"></P> <table border="0" width="100%" class="Code"> <tr> <td width="100%"> <pre>namespace SaurabhData { using System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; using System.Data.OleDb; using System.Data; <span class="Cmt">/// &lt;summary&gt; /// Class for viewing , editing and deleting data in a Ms Access 2000 /// database book.mdb using .NET SDK beta2 /// &lt;/summary&gt;</span> public class DataEdit : System.Windows.Forms.Form { <span class="Cmt">/// &lt;summary&gt; /// Required by the Win Forms designer /// &lt;/summary&gt;</span> private System.ComponentModel.Container components; private System.Windows.Forms.Button delete; private System.Windows.Forms.Button update; private System.Windows.Forms.Button helpme; private System.Windows.Forms.Button lastrec; private System.Windows.Forms.Button nextrec; private System.Windows.Forms.Button previousrec; private System.Windows.Forms.Button firstrec; private System.Windows.Forms.TextBox t_bookstock; private System.Windows.Forms.TextBox t_bookprice; private System.Windows.Forms.TextBox t_bookauthor; private System.Windows.Forms.TextBox t_booktitle; private System.Windows.Forms.TextBox t_bookid; private System.Windows.Forms.Label l_bookstock; private System.Windows.Forms.Label l_bookprice; private System.Windows.Forms.Label l_bookauthor; private System.Windows.Forms.Label l_booktitle; private System.Windows.Forms.Label l_bookid; private System.Windows.Forms.Label label1; private System.Windows.Forms.StatusBar statusBar; private System.Data.DataSet myDataSet ; private BindingManagerBase myBind; <span class="Cmt">//In beta2 we use BindingManagerBase instead of ListManager</span> private bool isBound=false; <span class="Cmt">//Variable to check if controls are bound</span> <span class="Cmt">///&lt;summary&gt; /// &lt;para&gt; /// This is the Constructor of the class, it calls 2 methods in it. /// InitializeComponent()This method initializes the WinForm /// GetConnected()This method gets connected to the database /// &lt;/para&gt; ///&lt;/summary&gt;</span> public DataEdit() { <span class="Cmt">// Required for Win Form Designer support</span> InitializeComponent(); <span class="Cmt">//Connect to the DataBase.</span> GetConnected() ; } <span class="Cmt">/// &lt;summary&gt; /// Clean up any resources being used /// &lt;/summary&gt;</span> public override void Dispose() { base.Dispose(); components.Dispose(); } <span class="Cmt">///&lt;summary&gt; /// This method connects to the database and returns a Dataset ///&lt;/summary&gt;</span> public void GetConnected() { try{ statusBar.Text=&quot;Please Wait Connecting to Database...&quot;; <span class="Cmt">//make a OleDbConnection</span> string strCon=&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=book.mdb&quot;; OleDbConnection myConn=new OleDbConnection(strCon) ; string strCom=&quot;SELECT * FROM bookstock&quot; ; <span class="Cmt">//Make a DataSet</span> myDataSet = new DataSet() ; myConn.Open() ; <span class="Cmt">//Using the OleDbDataAdapter execute the query //In beta2 we use OleDataAdapter instead of ADODataSetCommand //which was used in beta1 to fill a DataSet</span> OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom,myConn); <span class="Cmt">//Fill the Data set with the Table 'bookstock' //The FillDataSet method of ADODataSetCommand has changed to //Fill method of OleDbDataAdapter</span> myCommand.Fill(myDataSet,&quot;bookstock&quot;) ; <span class="Cmt">//Close the OleDbConnection</span> myConn.Close() ; <span class="Cmt">//DataBind the TextBoxes</span> if(!isBound) { <span class="Cmt">//In beta2 we use the &quot;DataBindings&quot; property of the TextBox //control to DataBind the Control //Since we are Binding to the Text property of the TextBox we pass //it as the first parameter //As the second parameter we pass the DataSet object //The third parameter contains the TableName followed by the FieldName //to which the control will databind</span> t_bookid.DataBindings.Add(&quot;Text&quot;, myDataSet, &quot;bookstock.bookid&quot;); t_booktitle.DataBindings.Add(&quot;Text&quot;,myDataSet,&quot;bookstock.booktitle&quot;); t_bookauthor.DataBindings.Add(&quot;Text&quot;,myDataSet,&quot;bookstock.bookauthor&quot;); t_bookprice.DataBindings.Add(&quot;Text&quot;,myDataSet,&quot;bookstock.bookprice&quot;); t_bookstock.DataBindings.Add(&quot;Text&quot;,myDataSet,&quot;bookstock.bookstock&quot;); <span class="Cmt">//Set the BindingManagerBase //Pass the DataSet object and the TableName to bind</span> myBind= this.BindingContext [myDataSet, &quot;bookstock&quot;]; isBound=true ; } statusBar.Text=&quot;Connected - Now you can Update or Delete Records.&quot;; } catch(Exception e) { MessageBox.Show(&quot;Error in connecting! &quot;+e.ToString(), &quot;Error&quot;); } } <span class="Cmt">/// &lt;summary&gt; /// The main entry point for the application. /// &lt;/summary&gt;</span> public static void Main(string[] args) { Application.Run(new DataEdit()); } <span class="Cmt">/// &lt;summary&gt; /// Required method for Designer support - do not modify /// the contents of this method with an editor /// &lt;/summary&gt;</span> private void InitializeComponent() { <span class="Cmt">//The code here is used to design the form //Please download the file for the complete code</span> } <span class="Cmt">///&lt;summary&gt; /// Delete Button Clicked /// &lt;para&gt; /// This first deletes the row from the DataSet object and then /// It updates the database with the updated DataSet /// &lt;/para&gt; ///&lt;/summary&gt;</span> protected void GoDelete(object sender, System.EventArgs e) { try{ <span class="Cmt">//connect to the database</span> string strCon=&quot;Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=book.mdb&quot; ; OleDbConnection myConn = new OleDbConnection(strCon) ; myConn.Open() ; string strDele=&quot;DELETE FROM bookstock WHERE bookid= &quot;+t_bookid.Text; OleDbCommand myCommand = new OleDbCommand(strDele,myConn); <span class="Cmt">//Delete the record from the database</span> myCommand.ExecuteNonQuery(); <span class="Cmt">//Delete the record from the DataSet</span> myDataSet.Tables[&quot;bookstock&quot;].Rows[myBind.Position].Delete() ; myDataSet.Tables[&quot;bookstock&quot;].AcceptChanges(); statusBar.Text=&quot;Record Deleted&quot; ; myConn.Close() ; } catch(Exception ed) { MessageBox.Show(&quot;Error in Deleting! &quot;+ed.ToString(), &quot;Error&quot;); } } <span class="Cmt">///&lt;summary&gt; ///Update Button Clicked /// &lt;para&gt; /// This first connects to the database and updates the row /// Then is calls the GetConnected() /// which again reinitializes the DataSet. /// &lt;/para&gt; ///&lt;/summary&gt;</span> protected void GoUpdate(object sender, System.EventArgs e) { int i=myBind.Position ; try{ <span class="Cmt">//connecting to the database</span> string strCon=&quot;Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=book.mdb&quot;; OleDbConnection myConn = new OleDbConnection(strCon) ; myConn.Open() ; <span class="Cmt">//update the database</span> string strUpdt = &quot;UPDATE bookstock SET booktitle='&quot; +t_booktitle.Text+&quot;', bookauthor='&quot; +t_bookauthor.Text+&quot;', bookprice=&quot; +t_bookprice.Text+&quot;, bookstock=&quot; +t_bookstock.Text+&quot; WHERE bookid= &quot;+t_bookid.Text; OleDbCommand myCommand = new OleDbCommand(strUpdt,myConn); myCommand.ExecuteNonQuery(); statusBar.Text=&quot;Record Updated&quot; ; myConn.Close() ; <span class="Cmt">//make the DataSet object 'null' so that we can give then new values</span> myDataSet=null ; myBind= null; <span class="Cmt">//remove the bindings to the textboxes</span> if(isBound) { t_bookid.DataBindings.Clear(); t_booktitle.DataBindings.Clear(); t_bookauthor.DataBindings.Clear(); t_bookprice.DataBindings.Clear(); t_bookstock.DataBindings.Clear(); isBound=false; } <span class="Cmt">//call the GetConnected method</span> GetConnected(); } catch(Exception ed) { MessageBox.Show(&quot;Error in Updating! &quot;+ed.ToString(), &quot;Error&quot;); } myBind.Position=i ; } <span class="Cmt">///&lt;summary&gt; /// To navigate the BindingManagerBase, increment the Position ///&lt;/summary&gt;</span> private void MoveNext() { if (myBind.Position == myBind.Count -1) MessageBox.Show(&quot;End of records&quot;); else myBind.Position += 1; } <span class="Cmt">///&lt;summary&gt; /// To navigate the BindingManagerBase, decrement the Position ///&lt;/summary&gt;</span> private void MovePrevious(){ if (myBind.Position == 0) MessageBox.Show(&quot;First record&quot;); else myBind.Position -= 1; } <span class="Cmt">///&lt;summary&gt; /// Move to position 0 in the list. ///&lt;/summary&gt;</span> private void MoveFirst(){ myBind.Position = 0; } <span class="Cmt">///&lt;summary&gt; /// Move to the count -1 position. ///&lt;/summary&gt;</span> private void MoveLast(){ myBind.Position = myBind.Count - 1; } <span class="Cmt">///&lt;summary&gt; /// Get Help ///&lt;/summary&gt; </span> protected void GoHelp(object sender, System.EventArgs e) { MessageBox.Show(&quot;Book Stock- Data Editing for .NET SDK beta2, Deleting program, by Saurabh Nandu, http://www.MasterCSharp.com&quot;, &quot;About..&quot;); } <span class="Cmt">///&lt;summary&gt; /// Last Record Button Clicked ///&lt;/summary&gt;</span> protected void GoLast(object sender, System.EventArgs e) { MoveLast(); } <span class="Cmt">///&lt;summary&gt; /// Next Record Button Clicked ///&lt;/summary&gt;</span> protected void GoNext(object sender, System.EventArgs e) { MoveNext(); } <span class="Cmt">///&lt;summary&gt; /// Previous Record Button Clicked ///&lt;/summary&gt;</span> protected void GoPrevious(object sender, System.EventArgs e) { MovePrevious(); } <span class="Cmt">///&lt;summary&gt; /// First Record Button Clicked ///&lt;/summary&gt;</span> protected void GoFirst(object sender, System.EventArgs e) { MoveFirst(); } } }</pre> </td> </tr> </table>

Comments

Add Comment