Save datatable as user setting

后端 未结 3 1041
你的背包
你的背包 2021-01-15 00:37

I currently try to save a datatable as a user setting using die old \"properties.settings.default.save()\" method.

It does not work. My Settings don\'t get saved. ho

相关标签:
3条回答
  • 2021-01-15 01:12

    sadly the solution above doesn't work for me (got an exceptation that the root element is missing). I had to make this adjustments.

    Create XML String:

     StringWriter writer = new StringWriter();
     table.WriteXml(writer, XmlWriteMode.WriteSchema); //WriteSchema
     Settings.Default.TableXml = writer.ToString();
    

    Create DataTable from Xml String

     StringReader reader = new StringReader(Settings.Default.TableXml);
     table.ReadXml(reader);
    

    Hope this helps al further users having the same problem ;)

    0 讨论(0)
  • 2021-01-15 01:20

    You can save the DataTable as an XML string to an ordinary String setting, like this:

     StringWriter writer = new StringWriter();
     table.WriteXml(writer);
     Settings.Default.TableXml = writer.ToString();
    

    You can then load it from the setting like this:

     StringReader reader = new StringReader(Settings.Default.TableXml);
     table.ReadXml(reader);
    
    0 讨论(0)
  • 2021-01-15 01:21

    I believe I encountered this same problem while performing a LINQ query on a DataTable from an ERP database. After using the CopyToDataTable() method on the query result and assigning it to the settingTable setting, I verified that neither the resultTable nor the settingTable were null and I ran the Settings.Default.Save() method. However, I received a null error when I subsequently attempted to assign the settingTable to a dataTable variable.

    I resolved this problem by assigning the settingTable.TableName property to something other than its default value (the null string) before running the Settings.Default.Save() method.

    Note: The ERP system is really slow, I would just query the data directly using the ERP's business objects.

    0 讨论(0)
提交回复
热议问题