473,320 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How can I save the formatted text from a C# RichTextBox to an Access memo field?

1
Hi everybody!

I have three textboxes (txtSourceType, txtSourceText, txtSourceDesc) to receive values for a table in Access. The value for txtSourceType will be converted to "int" type, and the other two will be text type. I have an OleHelper class in place and the code for adding a record to the table works well as follows.

Expand|Select|Wrap|Line Numbers
  1. public int SourceAdd() //This method is defined here and referenced elsewhere in this project.
  2.         {
  3.             string strCmdText = "INSERT INTO [tbl_Source] (fld_SourceType, fld_SourceText, fld_SourceDesc) VALUES ("
  4.                 + this.iSourceType + ", '" + this.sSourceText + "', '" + this.sSourceDesc + "')";
  5.             try
  6.             {
  7.                 return OleHelper.ExecuteNonQuery(OleHelper.CONN_STRING, CommandType.Text, strCmdText);
  8.             }
  9.             catch (InvalidCastException e)
  10.             {
  11.                 return -1;
  12.             }
  13.         }
  14.  
Now, I want to replace the textbox "txtSourceDesc" with a richtextbox "rtfSourceDesc" in order to take advantage of its formatting functionality. I guess I have to use parameterized queries for the rtf text to be stored in the memo field of Access while I still have to do string combination. I don't really have an idea how to do this. I'm sort of an outsider and someone else helped me with lots of the existing code. I'd appreciate it if I could have the specific code that works for this step but does not affect the referencing of this method elsewhere in this project. Million thanks! (By the way, I'm using Visual Studio 2012 + Access 2013)

The following is the OleHelper.

Expand|Select|Wrap|Line Numbers
  1. class OleHelper
  2.     {
  3.         public static readonly string CONN_STRING = ConfigurationManager.ConnectionStrings["LenguaGenius2013_Engl_Voca.Properties.Settings.LenguaGenius2013ConnectionString"].ToString();
  4.         private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
  5.  
  6.         #region PrepareCommand
  7.  
  8.         public static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  9.         {
  10.             if (conn.State != ConnectionState.Open)
  11.             {
  12.                 conn.Open();
  13.             }
  14.             cmd.Connection = conn;
  15.             cmd.CommandText = cmdText;
  16.             if (trans != null)
  17.             {
  18.                 cmd.Transaction = trans;
  19.             }
  20.             cmd.CommandType = cmdType;
  21.  
  22.             if (cmdParms != null)
  23.             {
  24.                 foreach (OleDbParameter parm in cmdParms)
  25.                 {
  26.                     cmd.Parameters.Add(parm);
  27.                 }
  28.             }
  29.         }
  30.  
  31.         public static void CacheParameters(string cacheKey, params OleDbParameter[] cmdParms)
  32.         {
  33.             parmCache[cacheKey] = cmdParms;
  34.         }
  35.  
  36.         public static OleDbParameter[] GetCachedParameters(string cacheKey)
  37.         {
  38.             OleDbParameter[] cachedParms = (OleDbParameter[])parmCache[cacheKey];
  39.  
  40.             if (cachedParms == null)
  41.                 return null;
  42.  
  43.             OleDbParameter[] clonedParms = new OleDbParameter[cachedParms.Length];
  44.  
  45.             for (int i = 0, j = cachedParms.Length; i < j; i++)
  46.                 clonedParms[i] = (OleDbParameter)((ICloneable)cachedParms[i]).Clone();
  47.  
  48.             return clonedParms;
  49.         }
  50.  
  51.         #endregion
  52.  
  53.         #region  ExecuteNonQuery
  54.  
  55.         public static int ExecuteNonQuery(OleDbTransaction trans, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  56.         {
  57.             OleDbCommand cmd = new OleDbCommand();
  58.             PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
  59.             int val = cmd.ExecuteNonQuery();
  60.             cmd.Parameters.Clear();
  61.             if (cmd.Connection.State == ConnectionState.Open)
  62.             {
  63.                 cmd.Connection.Close();
  64.             }
  65.             return val;
  66.         }
  67.  
  68.         public static int ExecuteNonQuery(OleDbConnection conn, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  69.         {
  70.             OleDbCommand cmd = new OleDbCommand();
  71.  
  72.             PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
  73.             int val = cmd.ExecuteNonQuery();
  74.             cmd.Parameters.Clear();
  75.             if (conn.State == ConnectionState.Open)
  76.             {
  77.                 conn.Close();
  78.             }
  79.             return val;
  80.         }
  81.  
  82.         public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParams)
  83.         {
  84.             OleDbCommand cmd = new OleDbCommand();
  85.             using (OleDbConnection conn = new OleDbConnection(connString))
  86.             {
  87.                 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParams);
  88.                 int val = cmd.ExecuteNonQuery();
  89.                 cmd.Parameters.Clear();
  90.                 if (cmd.Connection.State == ConnectionState.Open)
  91.                 {
  92.                     cmd.Connection.Close();
  93.                 }
  94.                 return val;
  95.             }
  96.         }
  97.  
  98.         public static int ExecuteNonQuery(OleDbTransaction trans, CommandType cmdType, string cmdText)
  99.         {
  100.             return ExecuteNonQuery(trans, cmdType, cmdText, null);
  101.         }
  102.  
  103.         public static int ExecuteNonQuery(OleDbConnection conn, CommandType cmdType, string cmdText)
  104.         {
  105.             return ExecuteNonQuery(conn, cmdType, cmdText, null);
  106.         }
  107.  
  108.         public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText)
  109.         {
  110.             return ExecuteNonQuery(connString, cmdType, cmdText, null);
  111.         }
  112.  
  113.         #endregion
  114.  
  115.         #region ExecuteReader
  116.  
  117.         public static OleDbDataReader ExecuteReader(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  118.         {
  119.             OleDbCommand cmd = new OleDbCommand();
  120.             OleDbConnection conn = new OleDbConnection(connString);
  121.  
  122.             try
  123.             {
  124.                 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
  125.                 OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  126.                 cmd.Parameters.Clear();
  127.                 if (conn.State == ConnectionState.Open)
  128.                 {
  129.                     conn.Close();
  130.                 }
  131.                 return rdr;
  132.             }
  133.             catch
  134.             {
  135.                 conn.Close();
  136.                 throw;
  137.             }
  138.         }
  139.  
  140.         public static OleDbDataReader ExecuteReader(string connString, CommandType cmdType, string cmdText)
  141.         {
  142.             return ExecuteReader(connString, cmdType, cmdText, (OleDbParameter[])null);
  143.         }
  144.  
  145.         #endregion
  146.  
  147.         #region ExecuteDataset
  148.  
  149.         public static DataSet ExecuteDataset(OleDbConnection conn, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  150.         {
  151.             OleDbCommand cmd = new OleDbCommand();
  152.             PrepareCommand(cmd, conn, (OleDbTransaction)null, cmdType, cmdText, cmdParms);
  153.             OleDbDataAdapter da = new OleDbDataAdapter(cmd);
  154.             DataSet ds = new DataSet();
  155.             da.Fill(ds);
  156.             cmd.Parameters.Clear();
  157.             if (conn.State == ConnectionState.Open)
  158.             {
  159.                 conn.Close();
  160.             }
  161.             return ds;
  162.         }
  163.  
  164.         public static DataSet ExecuteDataset(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  165.         {
  166.             using (OleDbConnection conn = new OleDbConnection(connString))
  167.             {
  168.                 conn.Open();
  169.                 return ExecuteDataset(conn, cmdType, cmdText, cmdParms);
  170.             }
  171.         }
  172.  
  173.         public static DataSet ExecuteDataset(string connString, CommandType cmdType, string cmdText)
  174.         {
  175.             return ExecuteDataset(connString, cmdType, cmdText, (OleDbParameter[])null);
  176.         }
  177.  
  178.         public static DataSet ExecuteDataset(OleDbConnection conn, CommandType cmdType, string cmdText)
  179.         {
  180.             return ExecuteDataset(conn, cmdType, cmdText, (OleDbParameter[])null);
  181.         }
  182.  
  183.         #endregion
  184.  
  185.         #region ExecuteDataTable
  186.  
  187.         public static DataTable ExecuteDataTable(OleDbConnection conn, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  188.         {
  189.             OleDbCommand cmd = new OleDbCommand();
  190.             PrepareCommand(cmd, conn, (OleDbTransaction)null, cmdType, cmdText, cmdParms);
  191.             OleDbDataAdapter da = new OleDbDataAdapter(cmd);
  192.             DataSet ds = new DataSet();
  193.             da.Fill(ds);
  194.             cmd.Parameters.Clear();
  195.             if (conn.State == ConnectionState.Open)
  196.             {
  197.                 conn.Close();
  198.             }
  199.  
  200.             return ds.Tables[0];
  201.         }
  202.  
  203.         public static DataTable ExecuteDataTable(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  204.         {
  205.             using (OleDbConnection conn = new OleDbConnection(connString))
  206.             {
  207.                 conn.Open();
  208.                 return ExecuteDataTable(conn, cmdType, cmdText, cmdParms);
  209.             }
  210.         }
  211.  
  212.         public static DataTable ExecuteDataTable(string connString, CommandType cmdType, string cmdText)
  213.         {
  214.             return ExecuteDataTable(connString, cmdType, cmdText, (OleDbParameter[])null);
  215.         }
  216.  
  217.         public static DataTable ExecuteDataTable(OleDbConnection conn, CommandType cmdType, string cmdText)
  218.         {
  219.             return ExecuteDataTable(conn, cmdType, cmdText, (OleDbParameter[])null);
  220.         }
  221.  
  222.         #endregion
  223.  
  224.         #region ExecuteScalar
  225.  
  226.         public static object ExecuteScalar(OleDbConnection conn, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  227.         {
  228.             OleDbCommand cmd = new OleDbCommand();
  229.             PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
  230.             object val = cmd.ExecuteScalar();
  231.             cmd.Parameters.Clear();
  232.             if (conn.State == ConnectionState.Open)
  233.             {
  234.                 conn.Close();
  235.             }
  236.             return val;
  237.         }
  238.  
  239.         public static object ExecuteScalar(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  240.         {
  241.             OleDbCommand cmd = new OleDbCommand();
  242.             using (OleDbConnection conn = new OleDbConnection(connString))
  243.             {
  244.                 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
  245.                 object val = cmd.ExecuteScalar();
  246.                 cmd.Parameters.Clear();
  247.                 if (conn.State == ConnectionState.Open)
  248.                 {
  249.                     conn.Close();
  250.                 }
  251.                 return val;
  252.             }
  253.         }
  254.  
  255.         public static object ExecuteScalar(string connString, CommandType cmdType, string cmdText)
  256.         {
  257.             return ExecuteScalar(connString, cmdType, cmdText, null);
  258.         }
  259.  
  260.         public static object ExecuteScalar(OleDbConnection conn, CommandType cmdType, string cmdText)
  261.         {
  262.             return ExecuteScalar(conn, cmdType, cmdText, null);
  263.         }
  264.  
  265.         #endregion
  266.     }
  267.  
Jun 5 '13 #1
0 1384

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Olaf Winterton | last post by:
I'm trying to insert this text into a memo field: Set cnn = CreateObject("ADODB.Connection") str = Server.MapPath("database/alumni.mdb") strDB = "Provider=MICROSOFT.JET.OLEDB.4.0;Data Source=" &...
3
by: Hugh Welford | last post by:
Hi ... My application saves variable length user-generated messages in an ACCESS memo field. ASP writes these messages correctly retaining CR LF to generate new paragraphs. (have checked this in...
2
by: Vincent Lau | last post by:
Hello, I'm new on ASP and Access. I stored several lines of information in a Access memo field. I use follow ASP response.write statement to display contain of the memo field. Response.Write...
6
by: Matt | last post by:
I'm having difficulty with trying to update a Access memo field through an SQL statement where the value I'm trying to pass is longer than 255 characters. The field is being truncated. I'm using...
1
by: ProDevel | last post by:
Hi, I'm desperately trying to figure out how to take rich-formatted text from a rich text box and store it in a memo field (or any other type of field!) in an Access database. Can anyone help??...
3
by: M. Noroozi Eghbali | last post by:
Hi All, I used the below code to get the information from a MS Access database file: ---------------------------------------------------- bdpConnection1.Open();...
1
by: dangier | last post by:
I have an Access 2003 table with an XML field that is tagged as follows: <Calendar CompactMode="1"><CustomProperties><CustomProperty Name="startdate" Value="3/26/2007"...
2
by: ACF | last post by:
Hello, I’m trying to format only selected text in an RTF Memo field in Access 2007 I’m able to copy selected text in a Memo field to the Windows clipboard with following code: ...
5
by: cosmodango | last post by:
I have spent several days on this. I hope someone can help me. I am using Acccess 2000. I have a memo field where I have cut and pasted bulleted/numbered lists from Microsoft Word. I want to...
1
by: jollyroger | last post by:
I have searched the web forums, and can't seem to find an answer to this particular problem I have. In an excel sheet, cells in one column have formatted text in the "wrapped" cells. For many of...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.