 |
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
|
|
|

December 2nd, 2004, 08:58 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
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.
|

December 2nd, 2004, 08:00 PM
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

December 4th, 2004, 01:26 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
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.
|

December 4th, 2004, 09:10 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
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.
|

December 5th, 2004, 07:03 PM
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

December 19th, 2004, 12:39 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
Ok ppl I solve my problem. Tnx for ur time
Always:),
Hovik Melkomian.
|

December 22nd, 2004, 04:21 PM
|
Registered User
|
|
Join Date: Sep 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

September 21st, 2007, 09:01 AM
|
Registered User
|
|
Join Date: Sep 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
@melvik :
I have the problem, too. Can you post your solution for me ?
Thank you.
|

August 30th, 2011, 02:25 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
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.
|
|
 |