Thursday, June 26, 2008

Better way to maintain Rich Text Format in SQL

In my previous blog i discussed how can we maintain rich text format in SQL.

I got better and easier way to maintain rich text format in SQL.

Here is the code to save the rich text format in SQL (Create a table myRTF with column name Descriptionrtf):

string conString = "DB Connection string";
SqlConnection con = new SqlConnection(conString);
SqlDataAdapter da = new SqlDataAdapter("Select Descriptionrtf From myRTF ", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;

char[] arr = richTextBox1.Rtf.ToCharArray();
string str = string.Empty;
for(int i = 0; i < arr.Length; i++)
str += arr[i].ToString();

DataRow myRow;
myRow["Descriptionrtf"] = str;
da.Update(ds, "MyImages");

Code to load date from SQL to rich text box :

string conString = "DB Connection string";
SqlConnection connection = new SqlConnection(conString );
SqlCommand command = new SqlCommand("Select Descriptionrtf From MyImages", connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
richTextBox1.Rtf = reader.GetString(0) ;
catch(Exception ex1)

Happy Coding :)

1 comment:

digital signature software said...

I think everyone work on SQL so this post is useful for everyone. This post explains a better way to maintain a rich text format in SQL. The method is very simple to understand. The logic behind the code is easily understandable. Thanks for sharing your experience with us.

Site Meter