Let’s start with a very simple application.
Drop OpenFileDialog control in the application.
When user click on Pick File button using OpenFileDialog he will select a rtf file to display in richtextbox control.
The content of richtextbox control will be saved in the sql server table.
This is how our table looks like.
File content is the image data type column where we will store our rtf content.
<!–[if gte vml 1]> <![endif]–>
Put the following code in Pick file button click.
if(openFileDialog1.ShowDialog() == DialogResult.OK)
Put the following code in Save Button Click
SqlConnection conn=new SqlConnection(“Data Source=D-0824;Initial Catalog=WordInterOp; uid=sa;pwd=sa;”);
SqlCommand cmd=new SqlCommand();
cmd.CommandText = “Insert into RtfStore (fileName,fileContent) values ( @fname,@fcontent)”;
SqlParameter fileName=new SqlParameter(“@fname”,openFileDialog1.SafeFileName);
FileStream stream = new FileStream(@”c:\temp.rtf”, FileMode.Open, FileAccess.Read);
int size = Convert.ToInt32(stream.Length);
Byte rtf = new Byte[size];
stream.Read(rtf, 0, size);
SqlParameter fileContent=new SqlParameter();
MessageBox.Show(“Entered data successfully”);
openFileDialog1.SafeFileName- To get the name of file instead of the complete path.
rtfContent.SaveFile- Saving the content of the control in a rtf file.
RichTextBoxStreamType.RichText– This tells that we are saving richtext having some formatting and not just plain text.
Finally through filestream we are reading that file and saving it’s content in our sqlserver table.
Similary to read the rtf content from the database and display it in Richtextbox control, this is what we need to do
// cmd.commandText=”select fileContent from RtfStore where filename=’xyz’ “;
SqlDataReader dr = cmd.ExecuteReader();
Byte rtf = new Byte[Convert.ToInt32((dr.GetBytes(0, 0,
null, 0, Int32.MaxValue)))];
long bytesReceived = dr.GetBytes(0, 0, rtf, 0, rtf.Length);
ASCIIEncoding encoding = new ASCIIEncoding();
rtfContent.Rtf = encoding.GetString(rtf, 0, Convert.ToInt32(bytesReceived));
31 thoughts on “Saving and Retrieving richtextbox control’s content (.rtf file) in database”
Excellent article 🙂 — One thing I noticed is that RTF format supports pictures in the document. I tested your code and worked well with text, however if you insert a picture in a RTF document and save it to the DB, then retreive it from the DB to display all the text is there but no picture…
Have you encountered this is there a way around this?
i sit vs 2008?
hey, the safeFileName is not in 2005 version……….
i got an idea of how to display rtf data in rich text box.
Could be display it in Crystal Reports? In VS2008 Crystal Reports versions take it lika a Blob field :S Every one know how to fix it?
I recently came across your blog and have been reading along. I thought I would leave my first comment.
I don’t know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.
PowerBuilder Application development
I was searching the Internet and found your awesome blog. I have studies many PowerBuilder books on the market,If read your this site this is very informative, and your site gives planty of knowledge about PowerBuilder.
Can you tell me code VB.NET & database SQLCE
I thought it was going to be some boring old post, but it really compensated for my time.Thank you so much
gimana caranya menyimpan file.dat yang telah di import ke rich textbox ke database…??
Hi i want to get rtf document into Richtextbox with full formatting support … Is it possible????
If possible please show me the code….Thanx in advance
The code that is here in this post – it was something that i tried 4-5 years back, at that time i did face issue of things not appearing with proper formatting. So it might not be possible.
– Nishant Rana
I’m curious to find out what blog system you’re utilizing?
I’m experiencing some minor security issues with my latest site and I’d like to find something more secure.
Do you have any suggestions?
After going over a number of the articles on your website, I
honestly appreciate your technique of writing a blog.
I added it to my bookmark webpage list and will be
checking back in the near future. Take a look at my
web site as well and tell me what you think.
After exploring a handful of the articles on your web site, I really appreciate your way of writing a blog.
I saved it to my bookmark website list and will be checking back
in the near future. Please visit my website as
well and tell me your opinion.
Hello There. I found your weblog the usage of msn. This is an extremely smartly
written article. I’ll make sure to bookmark it and return to read extra of your useful information. Thank you for the post.
I will certainly comeback.
I do not even know how I ended up here, but I thought this post was good.
I do not know who you are but definitely you’re going to a famous blogger if you aren’t
already 😉 Cheers!
Hi, every time i used to check weblog posts here in the
early hours in the break of day, since i love to find out more and more.
Hi! Do you know if they make any plugins to help with SEO?
I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good results.
If you know of any please share. Many thanks!
I was recommended this website by my cousin. I’m not sure whether
this post is written by him as nobody else know such detailed about my
difficulty. You’re amazing! Thanks!
You actually make it seem so easy with your presentation but I find this
matter to be really something which I think I would never understand.
It seems too complex and very broad for me. I’m looking forward for your next post, I will try to get
the hang of it!
With these changes, we want to give Annie’s lane opponents some more room to breathe against
her early threat, but we’ve given Tibbers more late game playtime to
compensate. Empowered Battle Roar heal changed to 20 +
(10 x level) amplified by a 1% per 1% of Rengar’s missing health (from
40 + (20 x level) health). Then climb to the second elevator and
do ledge hangs all the way to the top.
how do you resist those strong temptations to eat
wrong foods and hurt your health and your light level.
In principle you should always only buy the least processed rice.
The more pronounced this process gets, the more serious the digestive issues
First of all I want to say terrific blog! I had a quick question in which I’d
like to ask if you do not mind. I was interested to find out how
you center yourself and clear your mind prior to writing.
I’ve had a tough time clearing my thoughts in getting
my thoughts out. I truly do take pleasure in writing however it just seems like the
first 10 to 15 minutes are usually lost simply just trying to
figure out how to begin. Any recommendations or hints?
I have read so many articles or reviews about the blogger lovers but this paragraph is actually a nice post,
keep it up.
Thanks for finally writing about >Saving and Retrieving richtextbox
control’s content (.rtf file) in database | Nishant Rana’s Weblog <Liked it!
Very nice post. I simply stumbled upon your blog and wanted to
say that I’ve truly enjoyed surfing around
your weblog posts. In any case I’ll be subscribing for
your feed and I hope you write once more soon!
Quality content is the important to attract the people to pay a
visit the website, that’s what this site is providing.
Thanks for sharing your thoughts about shaker bottle.
I’m really enjoying the theme/design of your site.
Do you ever run into any browser compatibility issues?
A number of my blog visitors have complained about my site
not operating correctly in Explorer but looks great in Safari.
Do you have any solutions to help fix this problem?