Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 2nd, 2004, 08:58 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default filling RichTextBox using MemoryStream

Dear all:
I faced a new problem & asking my old friends was my last source!
I have a [u]RichTextBox </u>wich I wanna fill it using LoafFile method. datasource is a datafield(Content) & an Image type in MS-SQL Server including rtf file folmat's content witch I saved before & now I wanna read it.

Now I fill my dataset & I use following code to read but it gives me an error: [u]"Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."</u>

Code:
Byte[] Txt = (Byte[]) dataSet11.LetterSel.Rows[0]["Content"];
MemoryStream ms = new MemoryStream();
ms.Write(Txt, 0, (int)Txt.Length);
richTextBox1.LoadFile(ms, RichTextBoxStreamType.RichText);
ms.Close();
I'll be so happy if u could help me to solve it cuz I give up, Tnx in advance.

Always:),
Hovik Melkomian.
__________________
Always,
Hovik Melkomian.
 
Old December 2nd, 2004, 08:00 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think all you have to do is return the stream to the start after you've written your data.
Code:
ms.Write(Txt, 0, (int)Txt.Length);
ms.Seek(0, SeekOrigin.Begin);
richTextBox1.LoadFile(ms, RichTextBoxStreamType.RichText);
That should do the trick.

Kep.
 
Old December 4th, 2004, 01:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Tnx for ur reply dear Kep, but it doenst work, Its just has changed its error message to "Invalid file format." now!

Plz help:(:(

Always:),
Hovik Melkomian.
 
Old December 4th, 2004, 09:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

actually when I open a .RTF file & push it to a buffer, & display it in RichTextBox is OK, but when I store it in DB & read it from stored Data to show in RichTextBox gives me that error!
my code is lie belove:
Code:
string file = openFileDialog1.FileName;
FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read);

richTextBox1.LoadFile(stream, RichTextBoxStreamType.RichText);

// Conver & write in Buffer to save in DB as a image datatype
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
stream.Close();

//Save in DB
DataRow TxtRow;
TxtRow = dataSet11.LetterSel.NewRow();
TxtRow["Number"] = 1;
PicRow["Content"] = buffer;
dataSet11.LetterSel.Rows.Add(TxtRow);
AdapterLetter.Update(dataSet11, "Letter");
buffer = null;

// Read data from DB & Filling data
dataSet11.Letter.Clear();
AdapterLetter.SelectCommand.Parameters["Number"].Value = 1;
AdapterLetter.Fill(dataSet11, "Letter");

// write to buffer as a byte
Byte[] Txt = (Byte[]) dataSet11.Letter.Rows[0]["Content"];
MemoryStream ms = new MemoryStream();
ms.Write(Txt, 0, (int)Txt.Length);
ms.Seek(0, SeekOrigin.Begin);

// show it in richTextBox witch gives error
richTextBox1.LoadFile(ms, RichTextBoxStreamType.RichText);
ms.Close();
any idea?!
Tnx in advance.

Always:),
Hovik Melkomian.
 
Old December 5th, 2004, 07:03 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The only thing I can see in your code is the line
Code:
PicRow["Content"] = buffer;
What is PicRow?

I'm not sure if this is an error or a typo or something I don't understand.

Also, what sort of database are you using and what is the data type of "Content"?

Kep.
 
Old December 19th, 2004, 12:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Ok ppl I solve my problem. Tnx for ur time

Always:),
Hovik Melkomian.
 
Old December 22nd, 2004, 04:21 PM
Registered User
 
Join Date: Sep 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It would help others if you post your findings. :-)

Thanks
Arul

Quote:
quote:Originally posted by melvik
 Ok ppl I solve my problem. Tnx for ur time

Always:),
Hovik Melkomian.
 
Old September 21st, 2007, 09:01 AM
Registered User
 
Join Date: Sep 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

@melvik :

I have the problem, too. Can you post your solution for me ?

Thank you.

 
Old August 30th, 2011, 02:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Wink

Dear all I got messages to tell the solution! as I see in in 2004 just after my post & Then once in 2007, and now in 2011 again.
honestly i havent noticed the messages after my post, but honesty i forgot it. even i dunno what was my problem. oh 7 years...

plz contact me if u reach here and no answer
I'll do my best & would honor to help u
__________________
Always,
Hovik Melkomian.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Print RichTextBox marcusbris VB How-To 0 December 2nd, 2006 09:29 AM
RichTextBox Help Kaustav VB How-To 1 February 22nd, 2006 03:53 PM
Writing more than 1Kb to a MemoryStream owain Pro VB.NET 2002/2003 1 November 4th, 2004 09:59 AM
What is the use of a richtextbox? snowydust VB How-To 2 July 28th, 2004 11:05 AM
IO.MemoryStream from a string planoie ASP.NET 1.0 and 1.1 Professional 2 November 17th, 2003 05:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.