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.
Related
basic question - is it possible to populate a combobox with datasource and then cut the link to the datasource, but keep the data in the combobox? this will enable you to reuse your ds.Tables[0] below without affecting the first populated combobox.
comboBox1.DataSource = ds.Tables[0];
(The ds is populated from MS SQL Server with SqlDataAdapter)
No, it is not possible without copying the source data in some way. The DataSource property is a reference to your dataset, datatable or whatever object not a copy of it. Setting DataSource to null will remove any possibilities for the combo to see the data referenced.
Instead you could easily create a copy of your original table using the appropriate method available in the DataTable class
comboBox1.DataSource = ds.Tables[0].Copy();
This create a new copy of the table with the actual structure and content, but it is a copy in another memory area of the informations stored in the first object. At this point you have two distinct objects in memory and you can change the first one without affecting the second.
Let me say also that this is not very smart with big tables. Don't use this approach if your table contains a lot of records for obvious reasons.
I have a winforms app that uses a strongly typed custom DataSet for holding data for processing. It gets populated with data from a database.
I have a user control that takes any custom dataset and displays the contents in a data grid. This is used for testing and debugging. To make the control reusable, I treat the custom dataset as a normal System.Data.DataSet.
I have extended the control to allow the dataset to be saved to an XML file and also load previously saved XML files.
What I'm now trying to do is take the loaded data file, which is treated as a standard DataSet, and cast it back to the Custom Dataset. This shouldn't be difficult but I am getting the following System.InvalidCastException message:
Unable to cast object of type 'System.Data.DataSet' to type
'CostingDataSet'.
Here is an example of the problem code (It's the last line of the 3 that generates the exception):
DataSet selected = debugDisplay.SelectedDataSet;
CostingDataSet tempDS = new CostingDataSet();
tempDS = (CostingDataSet)selected.Copy();
Can anyone give me a steer on how to fix this?
Edit:
Following the comments from nEM I implemented this and all was good.
foreach (System.Data.DataTable basicDT in selected.Tables)
{
DataTable dt = tempDS.Tables[basicDT.TableName];
dt = basicDT.Copy();
}
In addition, the code suggested by SSarma also works.
From what I have gathered from this website, you can't cast a regular dataset into a typed one which makes sense as its strongly typed and has certain specifications. If you have saved it as a regular dataset, when you deserialise it, the XML has no recollection of it ever being created as a typed dataset. For the xml file, you only ever saved a regular dataset so it is equivalent to trying to convert a standard dataset into a typed one by explicit casting which isn't allowed.
You could create a populate method that takes in a regular dataset as an argument which copies all the data into your typed dataset.
This is assuming that you are serialising it as a standard dataset.
How about using Streams (sorry following code is not tested) but you get the idea
DataSet selected = debugDisplay.SelectedDataSet;
string ds1 = selected.GetXml();
CostingDataSet tempDS = new CostingDataSet();
System.IO.MemoryStream ms = new System.IO.MemoryStream(ds1.Length);
selected.WriteXml(ms);
ms.Position = 0;
tempDS.ReadXml(ms);
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
I'm consuming a 3rd-party web service that outputs a Dataset (from XML). I'd like to create a new table in my local MS Access database that contains the DataSet data.
Is there a simple way to hand-off the DataSet to a .net object and ask it to "create a table from this"?
I know that we can use different parts of ADO to extract schema, build commands, insert rows, etc. I figured there has to be a simpler way.
The only two ways I know of are to
Walk through the DataSet field by
field and generate a DDL instruction
(which is valid for MS-Access)
Add a reference to ADOX, create a new table (with columns) and append the new table to the ADOX catalog. More info here. But again you are walking throught the dataset table field by field.
I haven't provided much detail on either of these approaches since I don't think they match what you've specified.
It seems you are looking for a quicker way than either of those so I guess the answer to your question is no.
I am using vs2008 with CrystalReports and I'm wandering how can i dynamically add rows of data to a *.rpt file?(using c#).
In more detail i want to create a a function that populates a *.rpt file with data that might contain lists(for example "FirstName", "LastName", List<"Friend"> ;..Friend beeing an object that contains multiple fields like "FriendNr", "Address",....).
the code that i used so far is:
ReportDocument rpt = new ReportDocument();
MemoryStream stream = new MemoryStream();
string filename = filepath + "/myRpt.rpt";
rpt.Load(filename);
rpt.SetParameterValue(0, myObject.FirstName);
rpt.SetParameterValue(1, myObject.LastName);
Inside the rpt file i have placed FieldObjects(Parameter Fields), and i populate the file with data by assigning the desired values to these objects (" rpt.SetParameterValue(0, myObject.FirstName);" )
Please help me find a way to populate the report with the rows of data contained in the List also.
Thanks a lot for your time.
I don't think it is possible to add data rows to a report this way. I suggest using a Typed DataSet as your report data source. The report can then display as many Friend objects as you require.
Dynamic 'rows' approaches:
1). You could add more items to the parameter's CurrentValues collection. Not sure how you are using this in the report, but it may work for your purposes. Look at the ParameterFieldDefinition class for more information.
2). Create a DataSet, modify as necessary, then assign it to the report. Use the ReportDocument.SetDataSource() method to bind a report to data programmatically.
3). Another approach is to build a report that uses XML data, then programmatically modify the XML, then refresh the report.