Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Application Settings not saving using custom class

Tags:

Here's the class im trying to store

 [Serializable]
[XmlRoot(ElementName = "Database", IsNullable = false, Namespace = "http://somesite.com")]

class Database
{
    [XmlAttribute(AttributeName = "Name")]   
    public string Name { get; set; }

    [XmlAttribute(AttributeName = "Provider")]
    public DatabaseProvider Provider { get; set; }

    [XmlAttribute(AttributeName = "Driver")]
    public string Driver { get; set; }

    [XmlElement("DatabaseEntry")]
    public List<DatabaseEntry> SavedEntries { get; set; }

    public Database()
    {
        SavedEntries = new List<DatabaseEntry>();
    }

    public Database(string type, string provider, string driver)
    {

        Name = type;
        Driver = driver;
        Provider = DatabaseProvider.SqlClient;

        Provider = SetProvider(provider);
        SavedEntries = new List<DatabaseEntry>();
    }

    private DatabaseProvider SetProvider(string provider)
    {
        switch (provider)
        {
            case "OleDb":
                return DatabaseProvider.OleDb;
            case "SqlClient":
                return DatabaseProvider.SqlClient;
            default:
                return DatabaseProvider.SqlClient;
        }
    }
}

[Serializable]
[XmlRoot(ElementName = "DatabaseEntry", IsNullable = false, Namespace = "http://somesite.com")]

class DatabaseEntry

{
    [XmlAttribute(AttributeName = "Name")]
    public string Name { get; set; }

    [XmlAttribute(AttributeName = "ConnectionString")]
    public string ConnectionString { get; set; }

    [XmlAttribute(AttributeName = "DsnString")]
    public string DsnString { get; set; }

    public DatabaseEntry()
    { }
}

Database is the parent class that I'm trying store, anyone see anything wrong? When I instantiate the class and save it, nothing gets saved. What gives?

EDIT! The code that is being used to save:

if (Settings.Default.SqlDatabase == null) 
   { 
      Settings.Default.SqlDatabase = CreateNewDatabase();
      Settings.Default.Save();
   }

private Database CreateNewDatabase()
{
    Database data = new Database("SQL Server", "SqlClient", "SQL Native Client");
    data.SavedEntries.Add(new DatabaseEntry()
    {
        Name = "Default",
        ConnectionString = @"Hello1",
        DsnString = @"Hello2"
    });
    return data;
}

I'm simply just trying to store an instance of a Database class into the AppSettings

like image 986
Tom Avatar asked May 18 '09 21:05

Tom


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

You could try making the classes public - XmlSerializer is fussy about that.

However, I seem to recall that the settings code is much more interested in type-converters; you could try writing a TypeConverter for it?

like image 73
Marc Gravell Avatar answered Oct 12 '22 23:10

Marc Gravell