Web Listing 1: C# Routine That Saves RTF Data to a SQL Server 2005 Database USE [AdventureWorks] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[tblRichTextData]( [RichTextData] [nvarchar](max) COLLATE Latin1_General_CI_AS NULL ) ON [PRIMARY] GO -- Insert a new row, which we'll update through our example INSERT INTO tblRichTextData (RichTextData) VALUES ('TEST DATA') Let's write some C# code to bring life to Form1 Please make sure code behind form1 looks like the following: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WinRichText { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // change the font fontDialog1.ShowDialog(); richTextBox1.SelectionFont = fontDialog1.Font; } private void button2_Click(object sender, EventArgs e) { // change the color colorDialog1.ShowDialog(); richTextBox1.SelectionColor = colorDialog1.Color; } private void button3_Click(object sender, EventArgs e) { //declare connection string using windows security string cnString = "Data Source=(local); Initial Catalog=AdventureWorks;Integrated Security=SSPI;"; //declare Connection, command and other related objects SqlConnection conSave = new SqlConnection(cnString); SqlCommand cmdSave = new SqlCommand(); try { //open connection conSave.Open(); cmdSave.CommandType = CommandType.Text; cmdSave.Connection = conSave; cmdSave.CommandText = "UPDATE tblRichTextData SET RichTextData = @prichTextBox1"; SqlParameter parrichTextBox1 = new SqlParameter("@prichTextBox1", SqlDbType.VarChar); parrichTextBox1.Value = richTextBox1.Rtf; cmdSave.Parameters.Add(parrichTextBox1); cmdSave.ExecuteNonQuery(); conSave.Close(); MessageBox.Show("Data saved to Database"); } catch (Exception ex) { //display generic error message back to user MessageBox.Show(ex.Message); } finally { //check if connection is still open then attempt to close it if (conSave.State == ConnectionState.Open) { conSave.Close(); } } } private void button4_Click(object sender, EventArgs e) { //declare connection string string cnString = "Data Source=(local); Initial Catalog=AdventureWorks;Integrated Security=SSPI;"; //declare Connection, command and other related objects SqlConnection conGet = new SqlConnection(cnString); SqlCommand cmdGet = new SqlCommand(); try { //open connection conGet.Open(); cmdGet.CommandType = CommandType.Text; cmdGet.Connection = conGet; cmdGet.CommandText = "SELECT TOP 1 RichTextData FROM tblRichTextData"; richTextBox2.Rtf = cmdGet.ExecuteScalar().ToString(); conGet.Close(); MessageBox.Show("Data loaded from Database"); } catch (Exception ex) { //display generic error message back to user MessageBox.Show(ex.Message); } finally { //check if connection is still open then attempt to close it if (conGet.State == ConnectionState.Open) { conGet.Close(); } } } } } Code Analysis using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WinRichText { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // change the font fontDialog1.ShowDialog(); richTextBox1.SelectionFont = fontDialog1.Font; } private void button2_Click(object sender, EventArgs e) { // change the color colorDialog1.ShowDialog(); richTextBox1.SelectionColor = colorDialog1.Color; } private void button3_Click(object sender, EventArgs e) { //declare connection string using windows security string cnString = "Data Source=(local); Initial Catalog=AdventureWorks;Integrated Security=SSPI;"; //declare Connection, command and other related objects SqlConnection conSave = new SqlConnection(cnString); SqlCommand cmdSave = new SqlCommand(); try { //open connection conSave.Open(); cmdSave.CommandType = CommandType.Text; cmdSave.Connection = conSave; cmdSave.CommandText = "UPDATE tblRichTextData SET RichTextData = @prichTextBox1"; SqlParameter parrichTextBox1 = new SqlParameter("@prichTextBox1", SqlDbType.VarChar); parrichTextBox1.Value = richTextBox1.Rtf; cmdSave.Parameters.Add(parrichTextBox1); cmdSave.ExecuteNonQuery(); conSave.Close(); MessageBox.Show("Data saved to Database"); } catch (Exception ex) { //display generic error message back to user MessageBox.Show(ex.Message); } finally { //check if connection is still open then attempt to close it if (conSave.State == ConnectionState.Open) { conSave.Close(); } } } private void button4_Click(object sender, EventArgs e) { //declare connection string string cnString = "Data Source=(local); Initial Catalog=AdventureWorks;Integrated Security=SSPI;"; //declare Connection, command and other related objects SqlConnection conGet = new SqlConnection(cnString); SqlCommand cmdGet = new SqlCommand(); try { //open connection conGet.Open(); cmdGet.CommandType = CommandType.Text; cmdGet.Connection = conGet; cmdGet.CommandText = "SELECT TOP 1 RichTextData FROM tblRichTextData"; richTextBox2.Rtf = cmdGet.ExecuteScalar().ToString(); conGet.Close(); MessageBox.Show("Data loaded from Database"); } catch (Exception ex) { //display generic error message back to user MessageBox.Show(ex.Message); } finally { //check if connection is still open then attempt to close it if (conGet.State == ConnectionState.Open) { conGet.Close(); } } } } }