How to save a dataset table using a button? - c#

I have a group of data sets and they are all editable, what I need is a function that copies all of the information in the data tables and saves it back into the data set or as a seperate xml.

Ultimately, a DataTable is updatable. I can write
ds.Tables[0].Rows[0][0] = "Test"
(where ds is a DataSet) and that will update row 1, column 1 of the first DataTable within the DataSet. What I do from there with it is my choice, but the change is there in the in-memory copy of the DataSet.
If you're populating from a database via a DataAdapter, you can then call Update() to commit the changes back to the database, but if you're not in that scenario, then your change will remain in memory until you either dispose of the DataSet/s or go back off to the source to fetch them again.
I think the bottom line here is that you don't need a function to update DataTables, as they are inherently capable of being updated.

Related

Handling a large ADODB record set in data table?

I am loading a large result set of about 3 million rows (ADODB record set) into a data table. Its taking too long to even load the result set into a data table. I want to find out a way of extracting only part of a result set and then loading it into a DataTable. Alternatively, is there a way to directly read the recordset directly instead of loading it into a data table and then reading it ?
This is the code I use to fill my DataTable -
OleDbDataAdapter oleDA = new OleDbDataAdapter();
DataTable dt = new DataTable();
oleDA.Fill(dt, myADODBRecordset);
Here are some options to consider:
Get only the rows and columns you really need to work with.
Get some data and let the user ask for the next set of rows when they want to.
Write optimized SQL queries
Don't use a DataTable unless you have to because it contains more metadata information that other list-type objects.
Consider using a managed .NET provider.
Why load so large data into memory? A large data transaction must comsume large resource, so optimize your code, or use EF.

Database not updating when using DataSet

I am doing a small project to learn how to use DataSet but i have a small problem. Consider following code:
foreach (DatabaseDataSet.ApplicationRow rowApplication in database.Application)
{
if (rowApplication.AID.ToString() == lblIDApplication.Text)
{
rowApplication.Date= tbApplicationDatum.Text;
rowApplication.Status = tbApplicationStatus.Text;
applicationAdapter.Update(rowApplication);
break;
}
}
I don't know why but the database doesn't get updated. The DataRow is being updated as when I call the data again I see the new value. But when I re-run my application it's back to it's old value again. Any help?
EDIT: I'm working with strongly typed DataSet
You need to call the Update method of your adapter to propogate the changes
AcceptChanges only updates the changes in memory for the row and does not migrate those to the database
MSDN
AcceptChanges and RejectChanges only apply to DataRow related changes
(that is, Add, Remove, Delete, and Modify). They are not applicable to
schema or structural changes.
Calling AcceptChanges will not replicate these changes back to the
data source if the DataSet was filled using a DataAdapter. In that
situation, call Update instead
See Updating Data Sources with DataAdapters for more information
Its important to remember that the DataSet is a 'local copy' of the data not a 'live link' to the DB. If your DataSet is populated by a IDataAdaptor (say a TableAdaptor) for example you need to call the DataAdaptors Update method passing in the Updated dataset to sync the results back to the underlying DB.
Also I would suspect you DONT want to be doing 'new ApplicationTableAdapter()' because typically you would want to update with the TableAdaptor you populated with, at the least you would need to ensure you had the correct connection, query etc set up.
SOLUTION: It happens that nothing was wrong with the code. I had two ConnectionString defined in App.config. I forgot to remove the first one after I removed a previous database that had errors in it. Upon removing the first ConnectionString, everything worked.

Database changes are not pushing to server

I have a winforms application, that is using the datasource and dataset controls from the IDE
This is the code block i am using
dsParcelBatch.BC_cpo_PARCELRow pr = dsParcelBatch.BC_cpo_PARCEL.FindByISN(int.Parse(activeParcelID));
pr.BeginEdit();
pr.NODE_ISN = 6;
pr.EndEdit();
pr.AcceptChanges();
dsParcelBatch.AcceptChanges();
I can read the correct row in line 1, and it is populating the PR row with the correct values,
i call the beginedit, and the dsParcelBatch has not updated.
I change the value of the NODE_ISN to the new value, and it sticks
I close the edit, and accept the changes in the row.
I can look into the datasource (dsParcelBatch) and the changes are in there - YEA!
I call the dsParcelBatch, and the changed value is changed....
but when i view the database, the value is back to the original value (5)
What am i missing something?
Before calling AcceptChanges you need to use a DataAdapter to Update the database.
AcceptChanges only changes the state of the rows in the DataTable, not in the database.
MSDN:
When AcceptChanges is called, any DataRow object still in edit mode
successfully ends its edits. The DataRowState also changes: all Added
and Modified rows become Unchanged, and Deleted rows are removed.

.Net Simple Data File usage

I've created a new project in .Net (2010 4.0) and added an SDF data file. I've generated a dataset and created a table in it (and I believe generated the Fill and other methods).
In code, I'm trying to add a row to the database.
eBureauScrubber.App_Data.matchingtempDataSet ds = new App_Data.matchingtempDataSet();
eBureauScrubber.App_Data.matchingtempDataSet.ctfFileRow row = ds.ctfFile.NewctfFileRow();
row.Address = "123 Main St.";
row.City = "Overland Park";
row.FirstName = "Matt";
row.LastName = "Dawdy";
row.rownum = 1;
EDIT: Added the next bit of code.
ds.ctfFile.Rows.Add(row);
ds.ctfFile.AcceptChanges();
ds.AcceptChanges();
eBureauScrubber.App_Data.matchingtempDataSetTableAdapters.ctfFileTableAdapter ctfa = new App_Data.matchingtempDataSetTableAdapters.ctfFileTableAdapter();
ctfa.Update(ds.ctfFile);
This runs fine. However, after the program completes, the data is not persisted in the database. What am I missing?
EDIT: I've tried all different combinations of AcceptChanges() on the datatable, the dataset, running update() before, after, etc. I'm missing something huge here. I'm not even sure it is connecting to the "right" database. Maybe that's my problem.
EDIT 2: Here's what I did to get this to work (it's still funky, though).
Change the properties of my DB file in App_Data to "Do Not Copy"
Manually copy that db file to bin\debug\app_data
Use the data adapter's fill method to fill the ds.ctfFile data table.
Create a row (.NewctfFileRow())
Set values on that row.
ds.ctfFile.Rows.Add(row)
ds.ctfFile.AcceptChanges();
ds.AcceptChanges();
Call the adapater's update method.
Now, the data is in my database file (in bin\debug\app_data), but I can't see it because the Data Sources connection. I'm still trying to find out how to do that.
It should have generated a TableAdapter class with a .Update() method that you have to call to save data in your database. See MSDN for some examples.

C# Reordering the Tables in a Dataset

I create a custom dataset that I pass off to a black boxed component. The dataset consists of usually 5-6 tables (with unique names assigned by me). The component takes the dataset and builds a drop down combo box based off the table names. What I am needing to do though is to change the ordering of the tables within the dataset. I need to do this so I first offer up to the user the appropriate selection in the drop down (based off what section in the application they are in). So for instance...if they are in "Section A" then that is the first table name shown in the drop down list...if the user goes to "Section F" then that is what is shown in the list first...so on and so forth.
The more code intensive way is of course to just change the ordering in which I add the tables to the dataset. This would work, but I thought there had to be some way to do this more elegantly and with less code.
I am working in C# with the 3.5 framework.
Remember that DataSets and their contents are stored on the heap, so you can have a DataTable object in more than one place in the DataSet.
Simply create your DataSet with a dummy DataTable in position zero. Then, based on whatever section they'e in, you put the corresponding table in position zero. Your table name will appear twice in your DropDownBox, once as the 'default' and again below in its proper context and order with the other tables.
public class ThisThing
{
private DataSet myDS = new DataSet();
//Populate your DataSet as normal
public DataSet ChangeLocation(int CurrentSectionNumber)
{
myDS.Table[0] = myDS.Table[CurrentSectionNumber]
}
}
I'm not sure trying to force your ordering information into the DataSet's data structure is the most intuitive approach. You might consider passing an ordered list of DataTable instead of (or in addition to) the DataSet.

Categories