0

I have a C# application in .Net 4.0. I have a custom object that is called OrderBlock. This custom object contains a few properties and a list that contains another custom object called Order. When the application runs it saves the OrderBlock object to my settings file.

I can see in the settings file the OrderBlock has correctly saved the properties & list of orders. The issue I have though is next time I go to run the application and try to read the OrderBlock object is doesn't work. It just returns null.

 _orderBlockEntity = Properties.Settings.Default.MyOrderBlock;

Other settings I can read fine by using the a line similar to the line above. However the OrderBlock object is the only custom type I am using in the settings file.

The OrderBlock & Order objects contain properties with XmlElement attributes that rename the property to another name, this is for when sending an xml message - have no idea if this is an issue?

[XmlElement("tF_Transactions")]
    public List<Order> Orders
    {
        get { return _orders; }
        set { _orders = value; OnPropertyChanged("Orders"); }
    }

**** EDIT ******

Below is where I save my object to the settings file - which does work.

Properties.Settings.Default.MyOrderBlock = _orderBlock;
Properties.Settings.Default.Save();

The code in the Designer is as below.

 [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public global::FC.OMS.TF.Entities.OrderBlock MyOrderBlock {
        get {
            return ((global::FC.OMS.TF.Entities.OrderBlock)(this["MyOrderBlock"]));
        }
        set {
            this["MyOrderBlock"] = value;
        }
    }

Below is a snipet of my settings file - the rest is just more orders. The other settings shown below work fine in that after restarting my application their values can be read. The scope for all the settings below is User.

        <setting name="LastFileDateTime" serializeAs="String">
            <value>11/22/2013 09:01:29</value>
        </setting>
        <setting name="MessageSent" serializeAs="String">
            <value>True</value>
        </setting>
        <setting name="MessageRecieved" serializeAs="String">
            <value>True</value>
        </setting>
<setting name="MyOrderBlock" serializeAs="Xml">
            <value>                    
                    <SystemSettings>
                        <sCode>Key</sCode>
                        <sValue>msawyer131122090129848</sValue>
                    </SystemSettings>
                    <tF_Transactions>
                        <iId>17</iId>
                        <sSecurityId>GB0.144</sSecurityId>
                        <sPortfolio>OEE285</sPortfolio>
                        <sBuySell>B</sBuySell>
                        <sBuyCurrency>GBP</sBuyCurrency>
                        <sCostCurrency>GBP</sCostCurrency>
                        <iTransactionType>0</iTransactionType>
                        <dfBuyAmount>32000</dfBuyAmount>
                        <dfCostAmount xsi:nil="true" />
                        <dtDealDate>2013-11-22T09:01:29.8406559Z</dtDealDate>
                        <dtStartDate xsi:nil="true" />
                        <dtEndDate xsi:nil="true" />
                        <sUser>msawyer</sUser>
                        <sReason>FICLIENT</sReason>
                        <iStatus>0</iStatus>
                        <sComments>Bulk upload by msawyer at 22/11/2013 09:01:29</sComments>
                        <bGenerateDescription>true</bGenerateDescription>
                        <sDealer>msawyer</sDealer>
                    </tF_Transactions>
                    <tF_Transactions>
                        <iId>16</iId>
                        <sSecurityId>GB1.232</sSecurityId>
                        <sPortfolio>OEE285</sPortfolio>
                        <sBuySell>B</sBuySell>
                        <sBuyCurrency>GBP</sBuyCurrency>
                        <sCostCurrency>GBP</sCostCurrency>
                        <iTransactionType>0</iTransactionType>
                        <dfBuyAmount>30000</dfBuyAmount>
                        <dfCostAmount xsi:nil="true" />
                        <dtDealDate>2013-11-22T09:01:29.8396794Z</dtDealDate>
                        <dtStartDate xsi:nil="true" />
                        <dtEndDate xsi:nil="true" />
                        <sUser>msawyer</sUser>
                        <sReason>FICLIENT</sReason>
                        <iStatus>0</iStatus>
                        <sComments>Bulk upload by msawyer at 22/11/2013 09:01:29</sComments>
                        <bGenerateDescription>true</bGenerateDescription>
                        <sDealer>msawyer</sDealer>
                    </tF_Transactions>
mHelpMe
  • 6,336
  • 24
  • 75
  • 150

1 Answers1

0

When we change a user scope setting, the new value persists for the duration of the application session. You can persist changes to user settings between application sessions by calling the Settings.Save method after settings the value.

Ashish Charan
  • 2,347
  • 4
  • 21
  • 33
  • The value are persisting I can see them correctly. Its just when opening my application the next time it will not read in the value from the settings file for my custom type, it will for the other types such as datetime, bool etc – mHelpMe Nov 22 '13 at 11:02
  • Please check this link http://stackoverflow.com/questions/7681957/trouble-saving-a-collection-of-objects-in-application-settings It has exactly the same problem as yours. – Ashish Charan Nov 22 '13 at 11:12
  • But I cannot see any difference to the solution that was provided and what I currently have. I have edited my question to show the code in my designer class. – mHelpMe Nov 22 '13 at 11:21