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&&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 "<i>Replace</i>" 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> </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. </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> <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">///<summary> /// Class to add data in to a Ms Access 2000 database /// book.mdb using OleDb.NET . ///</summary></span> public class DataAdd :Form { <span class="Cmt">/// <summary> /// Required by the Win Forms designer /// </summary></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">///<summary> /// The Constructor of the class DataAdd /// <para> /// 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 /// </para> ///</summary></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">/// <summary> /// Clean up any resources being used /// </summary></span> public override void Dispose() { base.Dispose(); components.Dispose(); } <span class="Cmt">/// <summary> /// The main entry point for the application. /// </summary></span> public static void Main(string[] args) { Application.Run(new DataAdd()); } <span class="Cmt">///<summary> /// <para> /// 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. /// </para> ///</summary></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="Please Wait, Connecting ...." ; string strConn="Provider=Microsoft.Jet.OLEDB.4.0;"+ strConn+="Data Source=book.mdb" ; <span class="Cmt">//Make the Connection Object</span> OleDbConnection myConn = new OleDbConnection(strConn) ; <span class="Cmt">//Make a Select Command</span> string strCom = "Select bookid from bookstock" ; 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="Connected - Now you can Add records"; reader.Close(); myConn.Close(); } catch(Exception e) { MessageBox.Show("Error in connecting! "+e.ToString(), "Error"); } } <span class="Cmt">/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with an editor /// </summary></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">///<summary> /// <para> /// This method is called when the "Save" 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. /// </para> ///</summary></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!=""&&t_booktitle.Text!=""&&t_bookauthor.Text!="" &&t_bookprice.Text!=""&&t_bookstock.Text!="") { string strConn="Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=book.mdb"; OleDbConnection myConn = new OleDbConnection(strConn) ; myConn.Open(); <span class="Cmt">//the string to get values from the textboxes and form an "INSERT INTO" // statement.</span> string strInsert = "INSERT INTO bookstock (bookid, booktitle, bookauthor, "; strInsert+="bookprice, bookstock) VALUES ( "; strInsert += t_bookid.Text+", '"; strInsert += t_booktitle.Text+"', '"; strInsert += t_bookauthor.Text+"', "; strInsert += t_bookprice.Text+", "; strInsert += t_bookstock.Text+")"; OleDbCommand inst = new OleDbCommand(strInsert,myConn) ; <span class="Cmt">//Execute the statement</span> inst.ExecuteNonQuery() ; statusBar.Text="Data Added to Database " ; <span class="Cmt">//reset all the textboxes</span> int i=int.Parse(t_bookid.Text); i++; t_bookid.Text=i.ToString() ; t_booktitle.Text="" ; t_bookauthor.Text="" ; t_bookprice.Text="" ; t_bookstock.Text="" ; statusBar.Text="Connected - Now you can Add records"; myConn.Close() ; } else { MessageBox.Show("All fields must be completed.", "Error"); } } catch(Exception ed) { MessageBox.Show("Error in Saving "+ed.ToString(), "Error"); } } <span class="Cmt">///<summary> /// This method is called when the help button is clicked. ///</summary></span> protected void helpClick(object sender, System.EventArgs e) { MessageBox.Show("Book Stock for .NET SDK beta2- Data Addtion program, by Saurabh Nandu, http://www.MasterCSharp.com", "About ..."); } } }</pre> </td> </tr> </table> <P> </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">///<summary> /// Class to demonstrate how to view data from a database using OleDb.NET /// This example also uses data binding. ///</summary></span> public class DataView : System.Windows.Forms.Form { <span class="Cmt">/// <summary> /// Required by the Win Forms designer /// </summary></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">///<summary> /// <para> /// This is the constructor of the class which calls 2 methods. /// GetConnected() to connect to the database and get the data /// into a database. /// </para> /// <para> /// InitilizeComponents() this method is called to initialize the Form. /// </para> ///</summary></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">/// <summary> /// Clean up any resources being used /// </summary></span> public override void Dispose() { base.Dispose(); components.Dispose(); } <span class="Cmt">/// <summary> /// The main entry point for the application. /// </summary></span> public static void Main(string[] args) { Application.Run(new DataView()); } <span class="Cmt">///<summary> /// This method connects to the database and returns a Dataset ///</summary></span> public void GetConnected() { try{ <span class="Cmt">//make a OleDbConnection</span> string strCon="Provider=Microsoft.Jet.OLEDB.4.0 ;"; strCon+=Data Source=book.mdb" ; OleDbConnection myConn=new OleDbConnection(strCon) ; string strCom="SELECT * FROM bookstock" ; <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,"bookstock") ; <span class="Cmt">//Close the OleDbConnection</span> myConn.Close() ; } catch(Exception e) { MessageBox.Show("Error in connecting! "+e.ToString(), "Error"); } } <span class="Cmt">/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with an editor /// </summary></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 "DataBindings" property of the //textbox control. Since we are DataBinding to the "Text" 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("Text", myDataSet, "bookstock.bookid"); 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("Text", myDataSet, "bookstock.bookstock"); 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("Text", myDataSet, "bookstock.booktitle"); 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("Text", myDataSet, "bookstock.bookprice"); 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("Text", myDataSet, "bookstock.bookauthor"); <span class="Cmt">//DataBind the Controls with the DataSet object //and 'bookstock' table</span> myBind= this.BindingContext [myDataSet, "bookstock"]; } <span class="Cmt">///<summary> /// To navigate the BindingManagerBase, increment the Position property. ///</summary></span> private void MoveNext() { if (myBind.Position == myBind.Count -1) MessageBox.Show("End of records"); else myBind.Position += 1; } <span class="Cmt">///<summary> /// To navigate the BindingManagerBase, decrement the Position property. ///</summary></span> private void MovePrevious(){ if (myBind.Position == 0) MessageBox.Show("First record"); else myBind.Position -= 1; } <span class="Cmt">///<summary> /// Move to position 0 in the list. ///</summary></span> private void MoveFirst(){ myBind.Position = 0; } <span class="Cmt">///<summary> /// Move to the count -1 position. ///</summary></span> private void MoveLast(){ myBind.Position = myBind.Count - 1; } <span class="Cmt">///<summary> /// Get Help ///</summary></span> protected void GoHelp(object sender, System.EventArgs e) { MessageBox.Show("Book Stock- Data View for .NET SDK beta2, by Saurabh Nandu, http://www.MasterCSharp.com", "About ..."); } <span class="Cmt">///<summary> /// Last Record Button Clicked ///</summary></span> protected void GoLast(object sender, System.EventArgs e) { MoveLast(); } <span class="Cmt">///<summary> /// Next Record Button Clicked ///</summary></span> protected void GoNext(object sender, System.EventArgs e) { MoveNext(); } <span class="Cmt">///<summary> /// Previous Record Button Clicked ///</summary></span> protected void GoPrevious(object sender, System.EventArgs e) { MovePrevious(); } <span class="Cmt">///<summary> /// First Record Button Clicked ///</summary></span> protected void GoFirst(object sender, System.EventArgs e) { MoveFirst(); } } }</pre> </td> </tr> </table><P> </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">/// <summary> /// Class for viewing , editing and deleting data in a Ms Access 2000 /// database book.mdb using .NET SDK beta2 /// </summary></span> public class DataEdit : System.Windows.Forms.Form { <span class="Cmt">/// <summary> /// Required by the Win Forms designer /// </summary></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">///<summary> /// <para> /// 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 /// </para> ///</summary></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">/// <summary> /// Clean up any resources being used /// </summary></span> public override void Dispose() { base.Dispose(); components.Dispose(); } <span class="Cmt">///<summary> /// This method connects to the database and returns a Dataset ///</summary></span> public void GetConnected() { try{ statusBar.Text="Please Wait Connecting to Database..."; <span class="Cmt">//make a OleDbConnection</span> string strCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=book.mdb"; OleDbConnection myConn=new OleDbConnection(strCon) ; string strCom="SELECT * FROM bookstock" ; <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,"bookstock") ; <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 "DataBindings" 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("Text", myDataSet, "bookstock.bookid"); t_booktitle.DataBindings.Add("Text",myDataSet,"bookstock.booktitle"); t_bookauthor.DataBindings.Add("Text",myDataSet,"bookstock.bookauthor"); t_bookprice.DataBindings.Add("Text",myDataSet,"bookstock.bookprice"); t_bookstock.DataBindings.Add("Text",myDataSet,"bookstock.bookstock"); <span class="Cmt">//Set the BindingManagerBase //Pass the DataSet object and the TableName to bind</span> myBind= this.BindingContext [myDataSet, "bookstock"]; isBound=true ; } statusBar.Text="Connected - Now you can Update or Delete Records."; } catch(Exception e) { MessageBox.Show("Error in connecting! "+e.ToString(), "Error"); } } <span class="Cmt">/// <summary> /// The main entry point for the application. /// </summary></span> public static void Main(string[] args) { Application.Run(new DataEdit()); } <span class="Cmt">/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with an editor /// </summary></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">///<summary> /// Delete Button Clicked /// <para> /// This first deletes the row from the DataSet object and then /// It updates the database with the updated DataSet /// </para> ///</summary></span> protected void GoDelete(object sender, System.EventArgs e) { try{ <span class="Cmt">//connect to the database</span> string strCon="Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=book.mdb" ; OleDbConnection myConn = new OleDbConnection(strCon) ; myConn.Open() ; string strDele="DELETE FROM bookstock WHERE bookid= "+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["bookstock"].Rows[myBind.Position].Delete() ; myDataSet.Tables["bookstock"].AcceptChanges(); statusBar.Text="Record Deleted" ; myConn.Close() ; } catch(Exception ed) { MessageBox.Show("Error in Deleting! "+ed.ToString(), "Error"); } } <span class="Cmt">///<summary> ///Update Button Clicked /// <para> /// This first connects to the database and updates the row /// Then is calls the GetConnected() /// which again reinitializes the DataSet. /// </para> ///</summary></span> protected void GoUpdate(object sender, System.EventArgs e) { int i=myBind.Position ; try{ <span class="Cmt">//connecting to the database</span> string strCon="Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=book.mdb"; OleDbConnection myConn = new OleDbConnection(strCon) ; myConn.Open() ; <span class="Cmt">//update the database</span> string strUpdt = "UPDATE bookstock SET booktitle='" +t_booktitle.Text+"', bookauthor='" +t_bookauthor.Text+"', bookprice=" +t_bookprice.Text+", bookstock=" +t_bookstock.Text+" WHERE bookid= "+t_bookid.Text; OleDbCommand myCommand = new OleDbCommand(strUpdt,myConn); myCommand.ExecuteNonQuery(); statusBar.Text="Record Updated" ; 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("Error in Updating! "+ed.ToString(), "Error"); } myBind.Position=i ; } <span class="Cmt">///<summary> /// To navigate the BindingManagerBase, increment the Position ///</summary></span> private void MoveNext() { if (myBind.Position == myBind.Count -1) MessageBox.Show("End of records"); else myBind.Position += 1; } <span class="Cmt">///<summary> /// To navigate the BindingManagerBase, decrement the Position ///</summary></span> private void MovePrevious(){ if (myBind.Position == 0) MessageBox.Show("First record"); else myBind.Position -= 1; } <span class="Cmt">///<summary> /// Move to position 0 in the list. ///</summary></span> private void MoveFirst(){ myBind.Position = 0; } <span class="Cmt">///<summary> /// Move to the count -1 position. ///</summary></span> private void MoveLast(){ myBind.Position = myBind.Count - 1; } <span class="Cmt">///<summary> /// Get Help ///</summary> </span> protected void GoHelp(object sender, System.EventArgs e) { MessageBox.Show("Book Stock- Data Editing for .NET SDK beta2, Deleting program, by Saurabh Nandu, http://www.MasterCSharp.com", "About.."); } <span class="Cmt">///<summary> /// Last Record Button Clicked ///</summary></span> protected void GoLast(object sender, System.EventArgs e) { MoveLast(); } <span class="Cmt">///<summary> /// Next Record Button Clicked ///</summary></span> protected void GoNext(object sender, System.EventArgs e) { MoveNext(); } <span class="Cmt">///<summary> /// Previous Record Button Clicked ///</summary></span> protected void GoPrevious(object sender, System.EventArgs e) { MovePrevious(); } <span class="Cmt">///<summary> /// First Record Button Clicked ///</summary></span> protected void GoFirst(object sender, System.EventArgs e) { MoveFirst(); } } }</pre> </td> </tr> </table>