| | |
getting binary data out of the database
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2009
Posts: 46
Reputation:
Solved Threads: 0
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:
Here the text is changed into binary data, and them I add parameters and all whats needed, and finally:
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?
C# Syntax (Toggle Plain Text)
byte[] myFile = Encoding.UTF8.GetBytes(richTextBox1.Text);
C# Syntax (Toggle Plain Text)
cmd1.ExecuteNonQuery();
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?
•
•
Join Date: Oct 2009
Posts: 91
Reputation:
Solved Threads: 8
0
#2 15 Days Ago
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.
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.
•
•
Join Date: May 2009
Posts: 46
Reputation:
Solved Threads: 0
0
#3 13 Days Ago
I only save vbytes:
Do you have any example of transforming the text (or bytes) into xml?
C# Syntax (Toggle Plain Text)
byte[] DobiDatoteko = File.ReadAllBytes(myFullPath);
Do you have any example of transforming the text (or bytes) into xml?
Last edited by Mitja Bonca; 13 Days Ago at 4:53 am.
0
#4 13 Days Ago
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.
•
•
Join Date: May 2009
Posts: 46
Reputation:
Solved Threads: 0
0
#5 13 Days Ago
•
•
•
•
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.
I save it into the database with the :
C# Syntax (Toggle Plain Text)
byte[] DobiDatoteko = File.ReadAllBytes(myFullPath);
I open the file with:
C# Syntax (Toggle Plain Text)
byte[] buffer = (byte[])cmd1.ExecuteScalar(); povezava.Close(); FileStream fs = new FileStream(System.IO.Path.GetTempPath() + @"\MojaMapa\" + listView1.SelectedItems[0].Text, FileMode.Create); //, FileAccess.ReadWrite); fs.Write(buffer, 0, buffer.Length); fs.Close();
But if I want to open it in a richTextbox with:
C# Syntax (Toggle Plain Text)
byte[] buffer = (byte[])cmd1.ExecuteScalar(); String myString = Encoding.ASCII.GetString(buffer);
Maybe I can not transform the bytes to string... there is maybe the wrong transformation.
Last edited by Mitja Bonca; 13 Days Ago at 5:20 am.
0
#6 13 Days Ago
Impossible. From your earlier code example you showed us this:
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:
And using this SQL table:
You can store/retrieve RTF to a database using:
I'm not sure what is wrong with your original code since you haven't posted the complete sample but give this a shot.
C# Syntax (Toggle Plain Text)
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)
private void button2_Click(object sender, EventArgs e) { //This works OK const string fName = @"C:\rtf.rtf"; if (System.IO.File.Exists(fName)) System.IO.File.Delete(fName); System.IO.File.WriteAllBytes(fName, Encoding.UTF8.GetBytes(richTextBox1.Rtf)); System.Diagnostics.Process.Start("wordpad", "\"" + fName + "\""); }
And using this SQL table:
sql Syntax (Toggle Plain Text)
IF OBJECT_ID('RTFStore', 'U') IS NOT NULL DROP TABLE RTFStore CREATE TABLE RTFStore ( RecordId INT identity(1000, 1) PRIMARY KEY, RTF VARBINARY(max) )
You can store/retrieve RTF to a database using:
C# Syntax (Toggle Plain Text)
private void button3_Click(object sender, EventArgs e) { const string connStr = "Data Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;"; int rtfRecordId; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); const string insQuery = "Insert Into RTFStore (RTF) Values (@RTF) Select CAST(SCOPE_IDENTITY() as int) As Result"; using (SqlCommand cmd = new SqlCommand(insQuery, conn)) { cmd.Parameters.Add(new SqlParameter("@RTF", SqlDbType.VarBinary)).Value = System.Text.ASCIIEncoding.UTF8.GetBytes(richTextBox1.Rtf); rtfRecordId = Convert.ToInt32(cmd.ExecuteScalar()); } } using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); const string selQuery = "Select RTF From RTFStore Where RecordId = @RecordId"; using (SqlCommand cmd = new SqlCommand(selQuery, conn)) { cmd.Parameters.Add(new SqlParameter("@RecordId", SqlDbType.Int)).Value = rtfRecordId; object oBuffer = cmd.ExecuteScalar(); if (oBuffer != DBNull.Value) { byte[] buffer = (byte[])oBuffer; const string fName = @"C:\rtf2.rtf"; File.WriteAllBytes(fName, buffer); System.Diagnostics.Process.Start("wordpad", "\"" + fName + "\""); } } } }
I'm not sure what is wrong with your original code since you haven't posted the complete sample but give this a shot.
•
•
Join Date: May 2009
Posts: 46
Reputation:
Solved Threads: 0
0
#7 13 Days Ago
•
•
•
•
Impossible. From your earlier code example you showed us this:
C# Syntax (Toggle Plain Text)
byte[] myFile = Encoding.UTF8.GetBytes(richTextBox1.Text);
Do we understand each other now?
This is now I get the bytes out of the database:
C# Syntax (Toggle Plain Text)
byte[] buffer = (byte[])cmd1.ExecuteScalar();
And this is how I transform bytes to string:
C# Syntax (Toggle Plain Text)
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)
byte[] buffer = (byte[])cmd1.ExecuteScalar(); //String myString = Encoding.ASCII.GetString(buffer); System.Text.Encoding myEncoding = System.Text.Encoding.UTF8; //or .ASCII, or what ever I try here String myString = myEncoding.GetString(buffer);
But again no luck - the coding is incorrect.
Last edited by Mitja Bonca; 13 Days Ago at 9:05 am.
0
#8 13 Days Ago
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.
•
•
Join Date: May 2009
Posts: 46
Reputation:
Solved Threads: 0
0
#9 13 Days Ago
1.
This is for the files from the hdd:
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.
This is for the files from the hdd:
C# Syntax (Toggle Plain Text)
string myPath = treeView1.SelectedNode.FullPath; string myFileName = listView1.FocusedItem.Text; string myFullPath = Path.Combine(myPath, myFileName); byte[] DobiDatoteko = File.ReadAllBytes(myFullPath);
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)
byte[] saveText = Encoding.UTF8.GetBytes(richTextBox1.Text);
0
#10 13 Days Ago
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.

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.
![]() |
Similar Threads
- update sql server data database in c# (C#)
- Storing Binary Data in MySQL (MySQL)
- converting binary data into image in asp.net(vb code) (ASP.NET)
- Reading binary data from SPROC..help please... (C#)
- inser data into database (JSP)
- how to convert images to binary to store in database (Java)
- Login and retrieve user data from database (ASP.NET)
- php wont submit data into the database (PHP)
Other Threads in the C# Forum
- Previous Thread: Security of Videos?
- Next Thread: Using Xml.Linq and writing the schemaLocation
| Thread Tools | Search this Thread |
.net access algorithm animation array barchart bitmap box broadcast c# check checkbox client code combobox control conversion csharp custom database datagrid datagridview dataset datastructure datetime degrees development directrobot display draganddrop drawing encryption enum excel file form format formbox forms function gdi+ hash image index input install java label list listbox mandelbrot math mouseclick mp3 mysql native networking operator packaging path photoshop picturebox pixelinversion post print process programming radians regex remoting richtextbox safari server sleep snooze socket sql statistics stream string table tables tcp text textbox thread time timer treeview update usercontrol usercontrols validation visualstudio webbrowser wfa windows winforms wpf xml






