none
Getting error:End of Stream encountered before parsing was completed. when deserializing

    Question

  • Greetings!
    I'm trying to write a program that has a button that when pressed should get a phone number from a textbox then searches the number in the "customer.dat" file and if the phone number matches one of the phone numbers listed in the  file, it has to modify the other fields of the number (example the related name and etc.) Code is:

    private void button3_Click(object sender, EventArgs e)

    { bool t=false;

    int pos=0;

    FileStream output = new FileStream("customer.dat", FileMode.Open, FileAccess.Read);

    output.Seek(0, SeekOrigin.Begin);

    string f = textBox3.Text;

    BinaryFormatter reader = new BinaryFormatter();

    Record record1 = new Record();

    if (output.Length > 0)

    {

    while (true)

    {

    record1 = (Record)reader.Deserialize(output);

    if (record1.Phonenum == f)

    {

    t = true;

    pos = Convert.ToInt32(output.Position);

    break;

    }

    }



    if (t == false)

    {

    MessageBox.Show("Phone number not found");

    goto la;

    }

    string na = textBox1.Text;

    string fa = textBox2.Text;

    double due = Convert.ToDouble(textBox4.Text);

    record1.Fname = na;

    record1.Lname = fa;

    record1.Amountdue = due;

    output.Close();

    BinaryFormatter rd = new BinaryFormatter();

    FileStream write = new FileStream("customer.dat", FileMode.Open, FileAccess.Write);

    write.Seek(pos, SeekOrigin.Begin);

    rd.Serialize(write, record1);

    write.Close();

    MessageBox.Show("Phone number information has been successfuly re entered");

    la: output.Close();

    }

    }

    Ive also flushed and closed the stream properly when serializing the objects in the file before ,but don't know why I get  the error :"SerializationException was unhandled:End of Stream encountered before parsing was completed " at the spot I've highlighted yellow in the above code(record1 = (Record)reader.Deserialize(output)) .
    Any help would be very appreciated.
    Thanks in advance.

    Monday, January 12, 2009 11:32 PM

Answers

  •  

    Hello,

    Welcome to MSDN Forums!

    I reproduced the exception you encountered when the target phone number was not found in the serializable data file.   So when the deseralize process has scanned to the end of the FileStream, if we still ask BinaryFormatter to deseralize the stream, the exception will occur.  I have modified your codes in order to avoid this exception.   Besides, in my opinion “goto” is not recommended to use, because it will affect the logic of your code.  I tried to improve your code snippet, hope it helps.   For detail, please see the following code snipet:

            private void button3_Click(object sender, EventArgs e)

            {

                bool t = false;

                int pos = 0;

                using (FileStream output = new FileStream("customer.dat", FileMode.Open, FileAccess.Read))

                {

                    output.Seek(0, SeekOrigin.Begin);

                    string f = textBox3.Text;

                    BinaryFormatter reader = new BinaryFormatter();

                    Record record1 = new Record();

     

                    if (output.Length > 0)

                    {

                        while (output.Position != output.Length)

                        {

                            record1 = (Record)reader.Deserialize(output);

                            if (record1.Phonenum == f)

                            {

                                t = true;

                                pos = Convert.ToInt32(output.Position);

                                break;

                            }

                        }

                        if (t == false)

                        {

                            MessageBox.Show("Phone number not found");

                            return;

                        }

                        string na = textBox1.Text;

                        string fa = textBox2.Text;

                        double due = Convert.ToDouble(textBox4.Text);

                        record1.Fname = na;

                        record1.Lname = fa;

                        record1.Amountdue = due;

     

                        BinaryFormatter rd = new BinaryFormatter();

                        using (FileStream write = new FileStream("customer.dat", FileMode.Open, FileAccess.Write))

                        {

                            write.Seek(pos, SeekOrigin.Begin);

                            rd.Serialize(write, record1);

                            MessageBox.Show("Phone number information has been successfuly re entered");

                        }

                    }

                }

            }

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6
    Saturday, January 17, 2009 8:43 AM
    Moderator