Still facing Opendatabase() exception

Oct 7, 2010 at 6:40 PM
Edited Oct 7, 2010 at 6:45 PM

Hey guys,

I am developing an application for windows phone, and I needed to implement a database with it....

This db is great and simple and made my life easier, but when I was trying to open an exsiting database with the method opendatabase(db)

though, I've downloaded the latest version, it still gives me this error...

Here's the details:

SilverlightPhoneDatabase.Exceptions.OpenException was unhandled
  Message=Cannot open database. See InnerException for details
  StackTrace:
       at SilverlightPhoneDatabase.Database.OpenDatabase(String databaseName, String password, Boolean useLazyLoading)
       at SilverlightPhoneDatabase.Database.OpenDatabase(String databaseName)
       at BudGet.MainPage.addAccounts(Object sender, RoutedEventArgs e)
       at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
  InnerException: System.InvalidOperationException
       Message=There is an error in XML document (3, 4).
       StackTrace:
            at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
            at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
            at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
            at SilverlightPhoneDatabase.Database.LoadTable(IsolatedStorageFileStream tableStream, Type tableType)
            at SilverlightPhoneDatabase.Database.OpenDatabase(String databaseName, String password, Boolean useLazyLoading)
            at SilverlightPhoneDatabase.Database.OpenDatabase(String databaseName)
            at BudGet.MainPage.addAccounts(Object sender, RoutedEventArgs e)
            at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
            at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
       InnerException: System.InvalidOperationException
            Message=BudGet.Account cannot be serialized because it does not have a parameterless constructor.
            StackTrace:
                 at System.Xml.Serialization.SerializationHelper.CreateInstance(Type type)
                 at System.Xml.Serialization.XmlSerializationReader.DeserializeComplexElement(LogicalType deserializeAs, Accessor accessor, Fixup fixup, Object fixupTarget, String identifier, Boolean emptyValue, Boolean nullValue)
                 at System.Xml.Serialization.XmlSerializationReader.deserializeElement(Accessor accessor, Fixup fixup, Object fixupTarget)
                 at System.Xml.Serialization.XmlSerializationReader.deserializeLiteralArray(Accessor accessor, LogicalType deserializeAs, Fixup fixup, Object fixupTarget, Boolean isNull)
                 at System.Xml.Serialization.XmlSerializationReader.DeserializeArrayLikeElement(LogicalType deserializeAs, Accessor accessor, Fixup fixup, Object fixupTarget, String identifier, Boolean emptyValue, Boolean nullValue)
                 at System.Xml.Serialization.XmlSerializationReader.deserializeElement(Accessor accessor, Fixup fixup, Object fixupTarget)
                 at System.Xml.Serialization.XmlSerializationReader.DeserializeElement(Accessor accessor, Fixup fixup)
                 at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
                 at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
                 at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
                 at SilverlightPhoneDatabase.Database.LoadTable(IsolatedStorageFileStream tableStream, Type tableType)
                 at SilverlightPhoneDatabase.Database.OpenDatabase(String databaseName, String password, Boolean useLazyLoading)
                 at SilverlightPhoneDatabase.Database.OpenDatabase(String databaseName)
                 at BudGet.MainPage.addAccounts(Object sender, RoutedEventArgs e)
                 at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
                 at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

and here's my Table: Account

    public class Account
    {
        public int id { get; set; }
        public String name { get; set; }
        public String comment { get; set; }
        public double balance{ get; set; }
        public String balnceFormated { get; set; }

        public Account(String name, String comment, double balance)
        {
            this.name = name;
            this.comment = comment;
            this.balance = balance;
            this.balnceFormated = balance.ToString("c");
        }

        public Account(String name, String Comment)
        {
            this.name = name;
            this.comment = comment;
            this.balance = 0;
            this.balnceFormated = balance.ToString("c");
        }

        public Account(String name)
        {
            this.name = name;
            this.comment = "";
            this.balance = 0;
            this.balnceFormated = balance.ToString("c");
        }

        public String balanceFormated()
        {
            return balance.ToString("c");
        }


    }

 help is appreciated

Oct 8, 2010 at 11:18 PM

You need to add paramterless constructor to your class:

public Account() {  }


Without that serizlizer cannot work properly.  If you want to make sure you do not use it, just mark it with Obsolete attribute and watch for warning during compilation.

Thanks

Oct 9, 2010 at 2:37 PM

Thanks a lot Barskiy, I really appreciate it.

It works like a charm now...