Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Okay, so I'm trying to save ListView with its data through serialization. Using these two methods:

C#
public static T BinaryFileDeserialize<T>(string filePath)
        {
            FileStream fileStream = null;
            Object obj;
            try
            {
                if (!File.Exists(filePath))
                    throw new FileNotFoundException("The file" + "was not found.", filePath);
                fileStream = new FileStream(filePath, FileMode.Open);
                BinaryFormatter b = new BinaryFormatter();
                obj = b.Deserialize(fileStream);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
            }

            return (T)obj;
        }

and
C#
public static void BinaryFileSerialize(Object obj, string filePath)
        {
            FileStream fileStream = null;

            try
            {
                fileStream = new FileStream(filePath, FileMode.Create);

                BinaryFormatter b =
                    new BinaryFormatter();
                b.Serialize(fileStream, obj);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
            }
        }


Also, since I didn't include or write the methods to fit only this particular purpose of saving ListViews I'm trying - ye, that's correct, trying - to use these two in addition, like helpers:
C#
public ArrayList SaveListView(ListView LV)
        {
            ArrayList alSavedLV = new ArrayList();

            for (int i = 0; (i <= animalList.Count - 1); i++)
            {
                alSavedLV.Add(LV.Items[i]);
            }

            return alSavedLV;
        }

        public ListView LoadListView(ArrayList AL)
        {
            ListView lv = new ListView();

            for (int i = 0; (i <= (AL.Count - 1)); i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi = ((ListViewItem)(AL[i]));
                lv.Items.Add(lvi);
            }

            return lv;
        }


However, as far as saving goes, I think it works. The deserialization part on the other hand doesn't come through, or at least it doesn't fill my ListView with the saved data. Any ideas? Where might I be going wrong?

Happy easter!
Posted
Comments
toATwork 31-Mar-13 12:46pm    
What is the exact error?
leprechauny 1-Apr-13 1:13am    
There is no actual error. Or, depending on one's viewpoint, there is. I'm unable to save a file with the current ListView data and then load it again, so to speak. The saving part seems to work, the problems lies with the loading of the saved data.
toATwork 1-Apr-13 12:07pm    
Have you enabled exceptions? Did you step by step debug the saving?

1 solution

Don't serialize/deserialize controls. Have a data layer, serialize the data and use it to populate the controls, and update.

—SA
 
Share this answer
 
Comments
Zoltán Zörgő 1-Apr-13 16:08pm    
Of course. +5!
Sergey Alexandrovich Kryukov 1-Apr-13 16:10pm    
Thank you, Zoltán.
—SA
leprechauny 3-Apr-13 16:41pm    
Ye, Sergey, that was pretty much what I was going for. And also what those helper methods was for, but obviously that was where I went wrong, it seems. The idea was that the data would be stored in the ArrayList, in order to avoid serializing the whole ListView control.

Anyways, could you specify or give me a hint of a data layer example? So I get an idea of where I'm to be headed.
Sergey Alexandrovich Kryukov 3-Apr-13 16:47pm    
ArrayList is the obsolete class; it was made obsolete by the time of the .NET v.2.0. You should use System.Collections.Generic.List<> instead.
—SA
leprechauny 3-Apr-13 17:41pm    
Alright. So I'll make some changes to the helper methods so that they return a List<ListViewItem> - that also seems more plausible. Wouldn't one be able to populate the ListView quite easily with such a list as well?

Thanks for the quick and great comments by the way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900