January 28, 2021
Hot Topics:

Saving Rich Edit Control Text to SQL Server

  • By Tom Archer
  • Send Email »
  • More Articles »

Reading RTF Data from SQL Server

Reading the RTF data is even easier than saving it as no intermediary file is necessary. The first thing I do is to create the SqlConnection object the same way it was instantiated in the btnSave_Click method. I then instantiate a SqlCommand object, where I specify that I wish to read the Photo column from the Employees table where the EmployeeID value is equal to 1. The SqlCommand.ExecuteReader method executes the command and returns a SqlDataReader object. A call to SqlDataReader.Read should return the only record that matches my criteria. Just to be sure that valid data has been read, I then check the SqlDataReader.HasRows property and verify that the Photo column I'm reading is not null. Otherwise, an exception would be thrown when I attempt to read it.

Once I've determined that I have valid data, I then use the SqlDataReader.GetBytes method to read the binary data into a Byte array. You'll notice that there are actually two calls to GetBytes in the code sample below. I did this because GetBytes cannot be called to retrieve the data into a buffer until that buffer has been allocated. However, I know how big to make the buffer only after calling GetBytes! Therefore, the first call to GetBytes passes a null value for the object that is to receive the data, meaning that this call will return to me only the number of bytes contained in the Photo column. I then allocate the Byte array and call GetBytes a second time—this time passing the Byte array and receiving the data. Once the data is in the buffer, I can then use the ASCIIEncoding object to convert the data from a Byte array into a String object, which is finally used to update the rich-edit control:

private void btnRead_Click(object sender, System.EventArgs e)

  SqlConnection cn = null;
  SqlCommand cmd = null;
  SqlDataReader reader = null;
    cn = new SqlConnection("Database=Northwind;Integrated Security=true;");
    cmd = new SqlCommand("SELECT Photo FROM Employees WHERE EmployeeID=1", cn);
    reader = cmd.ExecuteReader();
    if (reader.HasRows)
      if (!reader.IsDBNull(0))
        Byte[] rtf = new Byte[Convert.ToInt32((reader.GetBytes(0, 0, null, 0, Int32.MaxValue)))];
        long bytesReceived = reader.GetBytes(0, 0, rtf, 0, rtf.Length);
        ASCIIEncoding encoding = new ASCIIEncoding();
        richTextBox1.Rtf = encoding.GetString(rtf, 0, Convert.ToInt32(bytesReceived));
  catch(Exception ex)
    if (null != reader) reader.Close();
    if (null != cn) cn.Close();

Download the Code

To download the accompanying source code for this tip, click here.

About the Author

The founder of the Archer Consulting Group (ACG), Tom Archer has been the project lead on three award-winning applications and is a best-selling author of 10 programming books as well as countless magazine and online articles.

Page 2 of 2

This article was originally published on June 28, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date