// instantiate object for FreeTable handling this.oFreeTable = new FreeTable();The click event of my web page' s Save button is shown below:
private void btnSave_Click(object sender, System.EventArgs e) { string lcID; lcID = this.mycustid; if ( !this.oFreeTable.LoadPatient(lcID) ) { this.lblErrorMessage.Text = oFreeTable.cErrormsg; return; } /// /// Update the DataRow of the arypat01 free table with /// the current values in the web page's textboxes. DataRow oRow = this.oFreeTable.oDS.Tables["arypat01"].Rows[0]; oRow["custid"] = this.txtcustid.Text; oRow["custphon"] = this.txthomephone.Text; oRow["custname"] = this.txtfirstname.Text; oRow["title"] = this.txtlastname.Text; oRow["appts"] = this.txtappts.Text; /// Save the row into the dataset if (!this.oFreeTable.SavePatient()) { this.lblErrorMessage.Text = oFreeTable.cErrormsg; } else { // this.lblSuccess.Text = "Record Saved..."; } }
using System; using System.Data; using System.Data.OleDb; using System.Text; namespace eCentric.GMG.Data.FreeTable { /// <summary> /// Summary description for FreeTable. /// </summary> /// Many thanks to all of the UT participants that /// guided my efforts in dealing with free tables in an ADO.net world. /// Including but not limited to: /// Kevin McNeish, Cathi Gero, Rick Strahl, Cetin Basoz, Rip Ryness, /// Bonnie (from 90 degrees), Morgan Everett and /// Paul Mrozowski. /// /// Rick Strahl and Cathi Gero provided the white papers /// that were the basis of the classes below, namely: /// http://msdn.microsoft.com/library/?url=/library/en-us/dnfoxgen7/html/usingaspnetwithvfp7.asp /// http://www.west-wind.com/presentations/VfpDotNetInterop/vfpDotNetInterop.htm /// /// <summary> /// Summary description for FreeTable. /// </summary> public class FreeTable : FreeTableBusObj { protected string FreeTableDataPath =""; protected string FreeTableName =""; protected string FreeTablePseudoPrimaryKey =""; protected string FreeTableDataEnvironment =""; public FreeTable() { // set up the properties for Free Table data access this.FreeTableDataPath="c:\\gmgwin\\main\\"; this.FreeTableName="arypat01"; this.FreeTablePseudoPrimaryKey="custid"; this.cConnectString = @"Provider=vfpoledb.1;Data Source="+FreeTableDataPath; this.FreeTableDataEnvironment="SET NULL OFF\r\nSET DELETED ON"; } protected override bool Open() { if (base.Open()) { try { // set up FreeTable Environment OleDbCommand oCommand = new OleDbCommand(); oCommand.Connection = this.oConn; oCommand.CommandText = this.FreeTableDataEnvironment; oCommand.ExecuteNonQuery(); } catch(Exception ex) { this.SetError(ex.Message); return false; } return true; } return false; } public bool LoadPatient(string lcID) { int lnCount = this.Execute("select * from " + this.FreeTableDataPath + this.FreeTableName + " where " + this.FreeTablePseudoPrimaryKey + "='" + lcID + "'",this.FreeTableName); if (this.lError) { return false; } return true; } public bool SavePatient() { if (!this.Open()) { return false; } OleDbDataAdapter oDSAdapter = new OleDbDataAdapter("select * from " + this.FreeTableDataPath + this.FreeTableName,this.oConn); //////////////////////////////////////////////////////////////////////// /// This builds the Update/InsertCommands for the data adapter /// NOTE: THIS DOESN'T WORK WITH THE VFP 8 OleDb Driver //OleDbCommandBuilder oCmdBuilder = new OleDbCommandBuilder(oDSAdapter); //oDSAdapter.UpdateCommand = oCmdBuilder.GetUpdateCommand(); //////////////////////////////////////////////////////////////////////// // So instead I have to manually build the update statement /// *or* I can simply issue the UpdateCommand directly over a connection DataRow oRow = this.oDS.Tables[this.FreeTableName].Rows[0]; string lcSQL = "UPDATE " + this.FreeTableDataPath + this.FreeTableName + " SET " + "custphon=" + "'" + oRow["custphon"].ToString() + "', " + "custname=" + "'" + oRow["custname"].ToString() + "', " + "title=" + "'" + oRow["title"].ToString() + "', " + "appts=" + "'" + oRow["appts"].ToString() + "' " + " where "+ this.FreeTablePseudoPrimaryKey + "='" + oRow["custid"].ToString()+ "'"; oDSAdapter.UpdateCommand = new OleDbCommand(lcSQL,this.oConn); int lnRows = 0; try { /// Take the changes in the dataset over to the database lnRows = oDSAdapter.Update(this.oDS,this.FreeTableName); // or send update command across the connection //oDSAdapter.UpdateCommand.ExecuteNonQuery(); } catch(Exception e) { this.cErrormsg = e.Message; return false; } return true; } public bool AddPatient() // skel code as of 10/30/2003. // no patient INSERTs via web as of 10/30/2003. // to be added at a later date. { if (!this.Open()) { return false; } OleDbCommand oCommand = new OleDbCommand(); oCommand.Connection = this.oConn; oCommand.CommandText = "InsertPatient()"; try { oCommand.ExecuteNonQuery(); } catch(Exception ex) { this.SetError(ex.Message); return false; } return true; } public bool DeletePatient(string lcId) { int lnResult = this.ExecuteNonQuery("delete from "+ this.FreeTableDataPath+this.FreeTableName + " where "+ this.FreeTablePseudoPrimaryKey +"='" + lcId +"'"); if (lnResult == -1) return false; return true; } } /// <summary> /// Helper Business Object /// </summary> public class FreeTableBusObj { public string cConnectString = ""; /// these properties along with Open,Close,Load,Save really belong /// in a data wrapper or high level business object. /// public OleDbConnection oConn = null; public DataSet oDS = null; public string cErrormsg = ""; public bool lError = false; public FreeTableBusObj() { } /// ********************* /// DATA UTILITY ROUTINES /// ********************* /// /// <summary> /// Sets up the connection for a query /// </summary> /// <returns></returns> protected virtual bool Open() { /// create if it doesn't exist already if (this.oConn == null) { try { this.oConn = new OleDbConnection(this.cConnectString); } catch(Exception e) { this.SetError( e.Message ); return false; } } /// check if connection is open - if not open it if (this.oConn.State != ConnectionState.Open) { try { oConn.Open(); } catch(Exception e) { this.SetError( e.Message ); return false; } } /// make sure our dataset object exists if (oDS == null) oDS = new DataSet(); return true; } protected virtual bool Close() { if (oConn.State == ConnectionState.Open) try { oConn.Close(); } catch(Exception e) { this.SetError( e.Message ); return false; } return true; } /// <summary> /// Executes a Select statement that returns a cursor. /// </summary> /// <param name="lcSQL"></param> /// <param name="lcCursor"></param> /// <returns></returns> public virtual int Execute(string lcSQL, string lcCursor) { if (!this.Open()) return 0; OleDbDataAdapter oDSAdapter = new OleDbDataAdapter(); oDSAdapter.SelectCommand = new OleDbCommand(lcSQL,oConn); /// remove the table if it exists - fail and ignore try { oDS.Tables.Remove(lcCursor); } catch(Exception) { } // Ignore the error try { oDSAdapter.Fill(oDS,lcCursor); } catch(Exception e) { this.SetError(e.Message); return 0; } return oDS.Tables[lcCursor].Rows.Count; } public virtual int ExecuteNonQuery(string lcCommand) { if (!this.Open()) return -1; OleDbCommand oCommand = new OleDbCommand(lcCommand,this.oConn); int lnResult; try { lnResult = oCommand.ExecuteNonQuery(); } catch(Exception ex) { this.cErrormsg = ex.Message; return -1; } return lnResult; } protected virtual void SetError(string lcErrormsg) { if (lcErrormsg.Length == 0) { cErrormsg = ""; lError = false; return; } cErrormsg = lcErrormsg; lError = true; } } }Thanx,