Solved

C# StreamWriter and Encoding Problem when creating a file

Posted on 2007-03-22
2
557 Views
Last Modified: 2013-11-26
I am trying to store an access database within a property in my class (as a string variable - is this correct? probably not!)

I can read the ms access db into my class using a streamreader but when I try to save the datbase back using a stream writer i get the following error:
------------------
Found a high surrogate char without a following low surrogate at index: 716.  The input may not be in this encoding, or may not contain valid Unicode (UTF-16) characters. Parameter name: chars
------------------

Here is my code for reading and writing the db:
Reading the database into my class:
System.IO.FileStream file = new System.IO.FileStream(dlgFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.StreamReader reader = new System.IO.StreamReader(file);
MyClass myClass = (MyClass)tvwPasswords.SelectedNode.Tag;
myClass .Document = reader.ReadToEnd();

Writing back to a file:
System.IO.FileStream file = new System.IO.FileStream(dlgFile.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
System.IO.StreamWriter writer = new System.IO.StreamWriter(file);
writer.Write(myClass.strDocument);
writer.Close();
file.Close();

Any Solutions Welcome!
0
Comment
Question by:benwilliamson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 250 total points
ID: 18784762
instead of storing into a string i would prefer byte stream.. excatly why are u storing this in a string
0
 

Author Comment

by:benwilliamson
ID: 18784943
Hi - thanks, yes you are right, a byte array did the trick.  I used a binaryreader/writer instead too and it seems to work - cheers.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decadeā€¦

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question