Question

I added a strongly-typed DataSet object to my project. It's type name is DocRetrieverDataSet. I also have in my project settings a row for a user-scope DataSet property named DocRetrieverDataSource to which I want to save an instance of DocRetrieverDataSet.

Here is boiled-down code:

using Settings = MyProjectNameSpace.Properties.Settings;
....
private DocRetrieverDataSet myDocRetrieverDataSet;

public myForm()
{
    Initialize();
    if (Settings.Default.DocRetrieverDataSource == null)
    {
        Settings.Default.DocRetrieverDataSource = new DocRetrieverDataSet();
        Settings.Default.Save();
    }
    this.myDocRetrieverDataSet = (DocRetrieverDataSet)Settings.Default.DocRetrieverDataSource;
}

The first time I run it, when Settings.Default.DocRetrieverDataSource is null, it works fine! However, when I run it the second time, I get an InvalidCastException at

this.myDocRetrieverDataSet = (DocRetrieverDataSet)Settings.Default.DocRetrieverDataSource;

It says

Unable to cast object of type 'System.Data.DataSet' to type 'DocRetriever.DocRetrieverDataSet'.

The funny thing is that it doesn't have this problem the first time around. What is going on and how can I fix it?

MORE INFO: Here's the relevant code from Settings.Designer.cs

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::System.Data.DataSet DocRetrieverDataSource {
    get {
        return ((global::System.Data.DataSet)(this["DocRetrieverDataSource"]));
    }
    set {
        this["DocRetrieverDataSource"] = value;
    }
}

And from app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=abcdefghijkl" >
            <section name="DocRetriever.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=abcdefghijkl" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <DocRetriever.Properties.Settings>
            <setting name="SpoolDirectoryPath" serializeAs="String">
                <value />
            </setting>
            <setting name="OutputDirectoryPath" serializeAs="String">
                <value />
            </setting>
        </DocRetriever.Properties.Settings>
    </userSettings>
</configuration>
Was it helpful?

Solution

You need to change the casting in your Settings file from DataSet to your DocRetrieverDataSet

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::System.Data.DataSet DocRetrieverDataSource {
    get {
        return ((global::FullNamespace.DocRetrieverDataSet)(this["DocRetrieverDataSource"]));
    }
    set {
        this["DocRetrieverDataSource"] = value;
    }
}

You can also do it via the Settings Designer, just go to your property and browse for you class Setting designer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top