944,047 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 2362
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 7th, 2009
0

getting binary data out of the database

Expand Post »
I am doing an app which inclueds richTextBox, for inserting text. I put some text into richTextBox and save it into my sql database (column is varbinary(MAX) data type, with this code:
C# Syntax (Toggle Plain Text)
  1. byte[] myFile = Encoding.UTF8.GetBytes(richTextBox1.Text);
Here the text is changed into binary data, and them I add parameters and all whats needed, and finally:
C# Syntax (Toggle Plain Text)
  1. cmd1.ExecuteNonQuery();
Then, when I want to open it, it opens in word (the code is made, that it opens in this program - depending on an extention and a file type).

So, when the Word is starting up, 1st it asks me if I want to change it (to use some other coding). I select nothing and click Ok. The text is there but it is not in the shape I inserted it into richTextBox. The font are all the same and are in Courier New (I didn`t save the the text in with this font, and even the text was not all the same).

Anyone has any clue how to get the same text out, as I saw inserted into richTextBox? Is this maybe anything to do with richTextBox, or this is only a matter of coding?
Similar Threads
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 8th, 2009
0
Re: getting binary data out of the database
Are you first saving the encoded text as a Word document?
If so, are you using any Office Automation for this or just saving bytes?

Have you thought about saving it as XML, then loading it in WORD?

You can get the WORD XML tags by saving a simple document from WORD as XML -- then extract out all of the XML shell and plug your text inside.
Featured Poster
Reputation Points: 304
Solved Threads: 277
Posting Virtuoso
thines01 is online now Online
1,693 posts
since Oct 2009
Nov 10th, 2009
0
Re: getting binary data out of the database
I only save vbytes:
C# Syntax (Toggle Plain Text)
  1. byte[] DobiDatoteko = File.ReadAllBytes(myFullPath);

Do you have any example of transforming the text (or bytes) into xml?
Last edited by Mitja Bonca; Nov 10th, 2009 at 4:53 am.
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 10th, 2009
0
Re: getting binary data out of the database
If you write the byte content of the RTF directly to disk (bypassing the SQL Server) and open it with word do you receive the same formatting message when opening the document? I suspect that you will. If you do this eliminates SQL as a culprit in this issue. It is probably Word complaining that you're asking it to open a non-native .docx file, or an RTF file it didn't create.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 10th, 2009
0
Re: getting binary data out of the database
Click to Expand / Collapse  Quote originally posted by sknake ...
If you write the byte content of the RTF directly to disk (bypassing the SQL Server) and open it with word do you receive the same formatting message when opening the document? I suspect that you will. If you do this eliminates SQL as a culprit in this issue. It is probably Word complaining that you're asking it to open a non-native .docx file, or an RTF file it didn't create.
No. if I open it in the defualt program, or with word or with acrobat reader, the text IS the same as before.
I save it into the database with the :
C# Syntax (Toggle Plain Text)
  1. byte[] DobiDatoteko = File.ReadAllBytes(myFullPath);

I open the file with:
C# Syntax (Toggle Plain Text)
  1. byte[] buffer = (byte[])cmd1.ExecuteScalar();
  2. povezava.Close();
  3. FileStream fs = new FileStream(System.IO.Path.GetTempPath() + @"\MojaMapa\" + listView1.SelectedItems[0].Text, FileMode.Create); //, FileAccess.ReadWrite);
  4. fs.Write(buffer, 0, buffer.Length);
  5. fs.Close();
As said, this works without any problem.
But if I want to open it in a richTextbox with:
C# Syntax (Toggle Plain Text)
  1. byte[] buffer = (byte[])cmd1.ExecuteScalar();
  2. String myString = Encoding.ASCII.GetString(buffer);
I got those strange signs.
Maybe I can not transform the bytes to string... there is maybe the wrong transformation.
Last edited by Mitja Bonca; Nov 10th, 2009 at 5:20 am.
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 10th, 2009
0
Re: getting binary data out of the database
Impossible. From your earlier code example you showed us this:
C# Syntax (Toggle Plain Text)
  1. byte[] myFile = Encoding.UTF8.GetBytes(richTextBox1.Text);

richTextBox1.Text returns the unformatting string of words in the RTF textbox. Now richTextBox1.Rtf will give you the formatted rich text. It is impossible that you saw the formatted rich text using the code provided earlier, something else is wrong.

Now this is how you can save an RTF control to a file:
C# Syntax (Toggle Plain Text)
  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. //This works OK
  4. const string fName = @"C:\rtf.rtf";
  5. if (System.IO.File.Exists(fName))
  6. System.IO.File.Delete(fName);
  7. System.IO.File.WriteAllBytes(fName, Encoding.UTF8.GetBytes(richTextBox1.Rtf));
  8. System.Diagnostics.Process.Start("wordpad", "\"" + fName + "\"");
  9. }

And using this SQL table:
sql Syntax (Toggle Plain Text)
  1. IF OBJECT_ID('RTFStore', 'U') IS NOT NULL DROP TABLE RTFStore
  2. CREATE TABLE RTFStore
  3. (
  4. RecordId INT identity(1000, 1) PRIMARY KEY,
  5. RTF VARBINARY(max)
  6. )

You can store/retrieve RTF to a database using:
C# Syntax (Toggle Plain Text)
  1. private void button3_Click(object sender, EventArgs e)
  2. {
  3. const string connStr = "Data Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;";
  4. int rtfRecordId;
  5.  
  6. using (SqlConnection conn = new SqlConnection(connStr))
  7. {
  8. conn.Open();
  9. const string insQuery = "Insert Into RTFStore (RTF) Values (@RTF) Select CAST(SCOPE_IDENTITY() as int) As Result";
  10. using (SqlCommand cmd = new SqlCommand(insQuery, conn))
  11. {
  12. cmd.Parameters.Add(new SqlParameter("@RTF", SqlDbType.VarBinary)).Value = System.Text.ASCIIEncoding.UTF8.GetBytes(richTextBox1.Rtf);
  13. rtfRecordId = Convert.ToInt32(cmd.ExecuteScalar());
  14. }
  15. }
  16. using (SqlConnection conn = new SqlConnection(connStr))
  17. {
  18. conn.Open();
  19. const string selQuery = "Select RTF From RTFStore Where RecordId = @RecordId";
  20. using (SqlCommand cmd = new SqlCommand(selQuery, conn))
  21. {
  22. cmd.Parameters.Add(new SqlParameter("@RecordId", SqlDbType.Int)).Value = rtfRecordId;
  23. object oBuffer = cmd.ExecuteScalar();
  24. if (oBuffer != DBNull.Value)
  25. {
  26. byte[] buffer = (byte[])oBuffer;
  27. const string fName = @"C:\rtf2.rtf";
  28. File.WriteAllBytes(fName, buffer);
  29. System.Diagnostics.Process.Start("wordpad", "\"" + fName + "\"");
  30. }
  31. }
  32. }
  33. }

I'm not sure what is wrong with your original code since you haven't posted the complete sample but give this a shot.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 10th, 2009
0
Re: getting binary data out of the database
Click to Expand / Collapse  Quote originally posted by sknake ...
Impossible. From your earlier code example you showed us this:
C# Syntax (Toggle Plain Text)
  1. byte[] myFile = Encoding.UTF8.GetBytes(richTextBox1.Text);
No. I do not get the text form richTextBox. I get the bytes from database and I would like to put this bytes (of course before thatr I have to transform them to string (text) into richTextBox.

Do we understand each other now?


This is now I get the bytes out of the database:
C# Syntax (Toggle Plain Text)
  1. byte[] buffer = (byte[])cmd1.ExecuteScalar();

And this is how I transform bytes to string:
C# Syntax (Toggle Plain Text)
  1. String myString = Encoding.ASCII.GetString(buffer);

And this is my problem - the transformation doesn`t work correctly. I o not get the correct text into "myString", but just some strange signs (as said earlier).
Here the richTextBox has nothing to do with this, richTextBox comes after that.

I would like to know how to do the correct transformtion from byte aray to string. This is my problem.


EDIT: I just did this code which should suppose to do the transformation:
C# Syntax (Toggle Plain Text)
  1. byte[] buffer = (byte[])cmd1.ExecuteScalar();
  2. //String myString = Encoding.ASCII.GetString(buffer);
  3. System.Text.Encoding myEncoding = System.Text.Encoding.UTF8; //or .ASCII, or what ever I try here
  4. String myString = myEncoding.GetString(buffer);

But again no luck - the coding is incorrect.
Last edited by Mitja Bonca; Nov 10th, 2009 at 9:05 am.
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 10th, 2009
0
Re: getting binary data out of the database
That depends on how the data was originally stored in to the SQL Server. Are you putting the text there? If so please post the code.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 10th, 2009
0
Re: getting binary data out of the database
1.
This is for the files from the hdd:
C# Syntax (Toggle Plain Text)
  1. string myPath = treeView1.SelectedNode.FullPath;
  2. string myFileName = listView1.FocusedItem.Text;
  3. string myFullPath = Path.Combine(myPath, myFileName);
  4. byte[] DobiDatoteko = File.ReadAllBytes(myFullPath);
Is this OK?

2.
And this is how I save from a richTextBox - damn sorry you were right. Didnt even noticed that I save it this way. Iwill repair for the richTextBox.
C# Syntax (Toggle Plain Text)
  1. byte[] saveText = Encoding.UTF8.GetBytes(richTextBox1.Text);
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 10th, 2009
0
Re: getting binary data out of the database
Upload your project man. You haven't been giving me complete answers for this whole thread

Where is the code that inserts the byte array in to the sql server? where is the code that writes the byte[] buffer to a file on the disk.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Security of Videos?
Next Thread in C# Forum Timeline: Need help...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC