locked
Binary, Serialization, record numbers RRS feed

  • Question

  • I wasn't sure how to or how I'm going to plain what I want to do,


    Ok, in a listview Control. I have 2 columns.
    ID     Name


    When I add a new record. How can I add a Record Number 'ID'

    I need a way to Id each record that I add.

    the now the harder part. Then I need to save it as a Serialized file and be able to load in again.

    I fount some samples in Binary. I still just don't under stand the methods or the classes.

    Can any one give me an idea. Any thing would help. LOL

    Joe

    I don't know of any other way to explain this to you.











    Toppers BBS http://toppersbbs.dtdns.net
    Saturday, November 21, 2009 9:21 PM

Answers

  • Hi,

    To serialize/deserialize all list items change the code in btnSerialize and btnDeserialize's click events like below.

            private void btnSerialize_Click(object sender, EventArgs e)
            {
                List<ListViewItem> lst = new List<ListViewItem>();
                foreach (ListViewItem lvi in listView1.Items)
                {
                    lst.Add(lvi);
                }
                byte[] b = ToBinary(lst);
                using (SaveFileDialog sfd = new SaveFileDialog())
                {
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
                        fs.Write(b, 0, b.Length);
                        fs.Close();
                    }
                }
            }
    
            private void btnDeserialize_Click(object sender, EventArgs e)
            {
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
    
                    if (ofd.ShowDialog()==DialogResult.OK)
                    {
                        FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
                        byte[] b=new byte[fs.Length];
                        fs.Read(b, 0,(int)fs.Length);
                        fs.Close();
                        List<ListViewItem> lst = (List<ListViewItem>)BinaryTo(b);
                        foreach (ListViewItem lvi in lst)
                        {
                            this.listView1.Items.Add(lvi);
                        }
                    }
                }
            }
    • Marked as answer by Joesoft11a Sunday, November 22, 2009 6:08 PM
    Sunday, November 22, 2009 5:34 PM

All replies

  • Hi,

    Here is a sample that covers all your issues.

    Hope it helps.

        public partial class Form1 : Form
        {
    
            public Form1()
            {
    
                InitializeComponent();
    
              
            }
            public byte[] ToBinary(object objToXml)
            {
                MemoryStream xStream = new MemoryStream();
    
                BinaryFormatter xFormatter = new BinaryFormatter();
    
                xFormatter.Serialize(xStream, objToXml);
                return xStream.ToArray();
            }
            public  object BinaryTo(byte[] str)
            {
                MemoryStream xStream = new MemoryStream(str);
                BinaryFormatter xFormatter = new BinaryFormatter();
    
                object xTemp = xFormatter.Deserialize(xStream);
    
                return xTemp;
            }
            private void btnAdd_Click(object sender, EventArgs e)
            {
                ListViewItem lvi = new ListViewItem(new string[]{(this.listView1.Items.Count+1).ToString(),"Foo" + this.listView1.Items.Count.ToString()});
                this.listView1.Items.Add(lvi);
            }
    
            private void btnSerialize_Click(object sender, EventArgs e)
            {
                byte[] b = ToBinary(this.listView1.Items[0]);
                using (SaveFileDialog sfd = new SaveFileDialog())
                {
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
                        fs.Write(b, 0, b.Length);
                        fs.Close();
                    }
                }
            }
    
            private void btnDeserialize_Click(object sender, EventArgs e)
            {
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
    
                    if (ofd.ShowDialog()==DialogResult.OK)
                    {
                        FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
                        byte[] b=new byte[fs.Length];
                        fs.Read(b, 0,(int)fs.Length);
                        fs.Close();
                        ListViewItem lvi = (ListViewItem)BinaryTo(b);
                        this.listView1.Items.Add(lvi);
                    }
                }
            }
    
        
    
        }
    Saturday, November 21, 2009 9:55 PM
  • Is there a chance that you can send me this. I under stand some of it. how ever most of it I don't under stand.

    send it too. joesft11a@hotmail.com

    thanks
    joe

    Toppers BBS http://toppersbbs.dtdns.net
    Saturday, November 21, 2009 10:34 PM
  • I just tried your sample above with a new project and nothing works or I just don't have it setup write.



    Toppers BBS http://toppersbbs.dtdns.net
    Saturday, November 21, 2009 11:54 PM
  • Is this a different issue than the other how to write binary files questions?
    Did you try my answer in:

    Have you looked at SQLCE or SQLExpress as a way to hold your data?

    I want to help, but I'm not sure I understand what you ultimately want to do.
    Sunday, November 22, 2009 4:17 AM
  • Hi,

    Here is my Form1.cs
    namespace WindowsFormsApplication1
    {
    
        public partial class Form1 : Form
        {
    
            public Form1()
            {
    
                InitializeComponent();
    
              
            }
            public byte[] ToBinary(object objToXml)
            {
                MemoryStream xStream = new MemoryStream();
    
                BinaryFormatter xFormatter = new BinaryFormatter();
    
                xFormatter.Serialize(xStream, objToXml);
                return xStream.ToArray();
            }
            public  object BinaryTo(byte[] str)
            {
                MemoryStream xStream = new MemoryStream(str);
                BinaryFormatter xFormatter = new BinaryFormatter();
    
                object xTemp = xFormatter.Deserialize(xStream);
    
                return xTemp;
            }
            private void btnAdd_Click(object sender, EventArgs e)
            {
                ListViewItem lvi = new ListViewItem(new string[]{(this.listView1.Items.Count+1).ToString(),"Foo" + this.listView1.Items.Count.ToString()});
                this.listView1.Items.Add(lvi);
            }
    
            private void btnSerialize_Click(object sender, EventArgs e)
            {
                byte[] b = ToBinary(this.listView1.Items[0]);
                using (SaveFileDialog sfd = new SaveFileDialog())
                {
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
                        fs.Write(b, 0, b.Length);
                        fs.Close();
                    }
                }
            }
    
            private void btnDeserialize_Click(object sender, EventArgs e)
            {
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
    
                    if (ofd.ShowDialog()==DialogResult.OK)
                    {
                        FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
                        byte[] b=new byte[fs.Length];
                        fs.Read(b, 0,(int)fs.Length);
                        fs.Close();
                        ListViewItem lvi = (ListViewItem)BinaryTo(b);
                        this.listView1.Items.Add(lvi);
                    }
                }
            }
    
        
    
        }
    
    
    }
    And Form1.designer.cs

    namespace WindowsFormsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.listView1 = new System.Windows.Forms.ListView();
                this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
                this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
                this.btnAdd = new System.Windows.Forms.Button();
                this.btnSerialize = new System.Windows.Forms.Button();
                this.btnDeserialize = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // listView1
                // 
                this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                this.columnHeader1,
                this.columnHeader2});
                this.listView1.Location = new System.Drawing.Point(0, 0);
                this.listView1.Name = "listView1";
                this.listView1.Size = new System.Drawing.Size(612, 388);
                this.listView1.TabIndex = 0;
                this.listView1.UseCompatibleStateImageBehavior = false;
                this.listView1.View = System.Windows.Forms.View.Details;
                // 
                // columnHeader1
                // 
                this.columnHeader1.Text = "ID";
                // 
                // columnHeader2
                // 
                this.columnHeader2.Text = "Name";
                // 
                // btnAdd
                // 
                this.btnAdd.Location = new System.Drawing.Point(47, 401);
                this.btnAdd.Name = "btnAdd";
                this.btnAdd.Size = new System.Drawing.Size(75, 23);
                this.btnAdd.TabIndex = 1;
                this.btnAdd.Text = "button1";
                this.btnAdd.UseVisualStyleBackColor = true;
                this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
                // 
                // btnSerialize
                // 
                this.btnSerialize.Location = new System.Drawing.Point(301, 401);
                this.btnSerialize.Name = "btnSerialize";
                this.btnSerialize.Size = new System.Drawing.Size(75, 23);
                this.btnSerialize.TabIndex = 2;
                this.btnSerialize.Text = "button2";
                this.btnSerialize.UseVisualStyleBackColor = true;
                this.btnSerialize.Click += new System.EventHandler(this.btnSerialize_Click);
                // 
                // btnDeserialize
                // 
                this.btnDeserialize.Location = new System.Drawing.Point(504, 401);
                this.btnDeserialize.Name = "btnDeserialize";
                this.btnDeserialize.Size = new System.Drawing.Size(75, 23);
                this.btnDeserialize.TabIndex = 3;
                this.btnDeserialize.Text = "button3";
                this.btnDeserialize.UseVisualStyleBackColor = true;
                this.btnDeserialize.Click += new System.EventHandler(this.btnDeserialize_Click);
                // 
                // Form1
                // 
                this.AllowDrop = true;
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(672, 436);
                this.Controls.Add(this.btnDeserialize);
                this.Controls.Add(this.btnSerialize);
                this.Controls.Add(this.btnAdd);
                this.Controls.Add(this.listView1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.ListView listView1;
            private System.Windows.Forms.ColumnHeader columnHeader1;
            private System.Windows.Forms.ColumnHeader columnHeader2;
            private System.Windows.Forms.Button btnAdd;
            private System.Windows.Forms.Button btnSerialize;
            private System.Windows.Forms.Button btnDeserialize;
    
    
    
    
    
    
    
        }
    }
    Sunday, November 22, 2009 5:39 AM
  • Ok, after playing with it I did get it to work. The problem is when I try and save it. It only saves the 1st record. I can't get it to save any thing else


    Toppers BBS http://toppersbbs.dtdns.net
    Sunday, November 22, 2009 5:28 PM
  • Hi,

    To serialize/deserialize all list items change the code in btnSerialize and btnDeserialize's click events like below.

            private void btnSerialize_Click(object sender, EventArgs e)
            {
                List<ListViewItem> lst = new List<ListViewItem>();
                foreach (ListViewItem lvi in listView1.Items)
                {
                    lst.Add(lvi);
                }
                byte[] b = ToBinary(lst);
                using (SaveFileDialog sfd = new SaveFileDialog())
                {
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
                        fs.Write(b, 0, b.Length);
                        fs.Close();
                    }
                }
            }
    
            private void btnDeserialize_Click(object sender, EventArgs e)
            {
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
    
                    if (ofd.ShowDialog()==DialogResult.OK)
                    {
                        FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
                        byte[] b=new byte[fs.Length];
                        fs.Read(b, 0,(int)fs.Length);
                        fs.Close();
                        List<ListViewItem> lst = (List<ListViewItem>)BinaryTo(b);
                        foreach (ListViewItem lvi in lst)
                        {
                            this.listView1.Items.Add(lvi);
                        }
                    }
                }
            }
    • Marked as answer by Joesoft11a Sunday, November 22, 2009 6:08 PM
    Sunday, November 22, 2009 5:34 PM
  • Is this a different issue than the other how to write binary files questions?
    Did you try my answer in:

    Have you looked at SQLCE or SQLExpress as a way to hold your data?

    I want to help, but I'm not sure I understand what you ultimately want to do.

    Yes it is a lot differant. That link was for a console application that I was making for a Text Files Section to a larger project. That's why I needed some ideas for it.

    The post here is for the larger project. Just a differant part of the project.
    I needed a way to ID Each Item entered. So when I add it to the learger project. I will be able to pick one from the list. Since my project is Part Windows Application and part Console Application.

    So If I select 1) from the list. That means the 1 st I added will match it up. So that the software can get, read and load that file in

    Heres a sample of what I'm trying to do

    ID    Name      Code
    1      Frank      User1
    2      Mary       User2


    Get the idea. When I select 2 from the console Application it will load User2.dat and I will have that information.
    I'm using a Windows Application to do the setup on it. and I use the Console Application as a Server. A web service or what ever I wanted to use it for.

    Does that give you a better idea of what I'm trying to do. Or it mite be I'm getting to old to Program. lol

    Joe









    Toppers BBS http://toppersbbs.dtdns.net
    Sunday, November 22, 2009 5:41 PM