DataSet XML export is empty - c#

I've got in-memory dataset with couple of tables that is populated in code. Data-bound grids on the gui show table contents without a problem.
Then I try to export the dataset into XML:
ds.WriteXml(fdSave.FileName, XmlWriteMode.WriteSchema);
and get empty XML (with couple of lines regarding dataset names but without any tables)
If I export table directly I've got all the data but dataset name is obviously wrong:
ds.Fields.WriteXml(fdSave.FileName, XmlWriteMode.WriteSchema);
What am I missing? Is there any reasonable way to write the whole dataset into file?

ok, silly me. was clearing dataset tables collection before populating it...

Have a look at the documentation on DataSet.WriteXml:
http://msdn.microsoft.com/en-us/library/ms135425.aspx
and on XmlWriteMode:
http://msdn.microsoft.com/en-us/library/system.data.xmlwritemode.aspx
Try using XmlWriteMode.IgnoreSchema. The docs say for XmlWriteMode.WriteSchema "If the DataSet does not have a current schema, nothing is written."

you must add the data tables to your dataset like,
ds.Tables.Add(datatable1);
then write export it into the schema

Related

Write back error to datatable bulkcopy C#

I want to make import data from .xlsx to database
the flow as:
xlsx->datatable->database
I copy data from excel to temp table and insert, update to official table later
Everything above work OK but now i'm stucking at writing back error if have(data already had, constraint...) to specific field of corresponding row in datatable and view to DataGrid
If you went through this problems, please advise me the best way to do it
Thank you with highly appreciate.
try to use this library https://www.nuget.org/packages/ExcelDataReader/ to read .xlsx and load in DataSet / DataTable. Then run the validations in your datatable.
Its better to have a seperate datatable which you will be binding to DataGrid.

Better way to use data set in C#

We have created a custom dataset and populating it with some data.
Before adding data, we are adding columns in the data set as follows
DataSet archiveDataset = new DataSet("Archive");
DataTable dsTable = archiveDataset.Tables.Add("Data");
dsTable.Columns.Add("Id", typeof(int));
dsTable.Columns.Add("Name", typeof(string));
dsTable.Columns.Add("LastOperationBy", typeof(int));
dsTable.Columns.Add("Time", typeof(DateTime))
Once the Dataset is create, we are filling values as follows
DataRow dataRow = dsTable.NewRow();
dataRow["Id"] = source.Id;
dataRow["Name"] = source.Name;
dataRow["LastOperationBy"] = source.LastOperationBy;
dataRow["Time"] = source.LaunchTime;
Is there any better and managed way of doing this. can I make the code more easy to write using enum or anything else to reduce the efforts?
You could try using a Typed Dataset.
This should get rid of the ["<column_name>"] ugliness.
If the dataset has a structure similar to tables in a database, then Visual Studio makes it really easy to create one: just click Add -> New Item somewhere in the solution and choose DataSet. VS will show a designer where you can drag tables from your server explorer.
Update (after response to Simon's comment):
A typed dataset is in fact an XSD (XML Schema Definition).
What I did in a similar case was:
created an empty DataSet (using Add -> New Item -> DataSet)
opened the newly created file with a text editor (by dafault, in VS it shows the XSD designer)
paste the XSD that I had created manually
You could also choose to use the designer to create the schema.
Considering your comment "I am using Dataset to export data to a XML file" I recommend using a different technology such as
Linq to XML http://msdn.microsoft.com/en-us/library/bb387061.aspx or
Xml Serialzation http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
Or better yet of is doesnt have to be XML (and you only want hierarchical readable text consider JSON instead http://james.newtonking.com/pages/json-net.aspx
You can bind dataset in two way first one is using database second one is add manually.
After create column for dataset you can add using Loops you can add it if you have 10000 of entries.
You can use Reflection. Another option is to use EntityFramework or NHibernate to map the columnnames and datastructure columns and then avoid these code to fill each field manually. But they will add more complexity. Also Performance wise the your code is better.

How to erase all records from a database when using LINQ and tableadapter?

I am trying to load data from Excel file to DataGridView in my Form. First I load all data from excel sheet into dataset then put all that data in database using C# LINQ and then I just set the DataGridView.DataSource to name.BindingSource and that is it. I get all the data in DataGridView but when I try to load the data again (I have closed my program and changed some cells in Excel) the new data is just appendend to the previous data but I want only the new data...
So my question is: How to delete all records in database or in LINQ or TableAdapter just to delete RECORDS.... :) getting frustrated by this.
I am trying to clear a database dataset by using nameDBDataset.table.Clear() but nothing, trying to use TableAdapter to delete but again nothing.
help please...
Thank you all!
I found the answer, its really quite simple... duh :)
nameDataContext dc = new nameDataContext();
dc.ExecuteCommand("TRUNCATE TABLE name-of-the-table");
Thats it...

DataSet.Copy vs Dataset.Clone

Can someone explain me DataSet.Copy() vs Dataset.Clone()
Also let me know some scenario's where we can use these
Clone will create a new, empty DataSet with the same schema (tables and columns) as the old one. The new DataSet will not have any data.
Copy does the same thing, but also copies the rows in the tables.
Clone copies the only the structure of the dataset where as Copy copies the data as well.
http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=52327
The Clone() method creates a new DataSet with the same structure. The Copy() method does too, and copies the data.
DataSet.Copy copies both the structure (tables, relations etc) and the data.
DataSet.Clone copies only the structure of the DataSet.
.Clone returns a data set with exactly the same schema but without any rows, .Copy returns both the schema and the rows.

Getting xml from a dataset

I have a DataSet which has one table loaded with data.
When i write out the dataset to xml using the GetXml method of the dataset, i get all the columns in that data table as elements in the xml file.
How do i get the resulting xml with the column values as attributes instead of elements?
The article here trails off without a proper answer
I am using .NET Framework 2.0
Before writing the XML, do something like
foreach (DataColumn column in aDataSet.Tables[0].Columns)
{
column.ColumnMapping = MappingType.Attribute;
}
Although I'll admit I didn't test this. You still may get a DiffGram structured file.
foreach(DataColumn dc in dsHR.Tables[0].Columns)
dc.ColumnMapping = MappingType.Attribute;
Quite simple :)

Categories

Resources