I have dataset connected with db. Dataset and TableAdapterManager were auto-created by VS based on connection.
I can do like this.tableAdapterManager.UpdateAll(testcheckerDataSet);
It's working fine.
Next I (using 3d party database editor) have copied .db file to "blank_testing.db" (2nd db) and then deleted all data from it. So It has just scheme without data.
Now I (in the code) filled dataset with data from "testchecker.db" (1st db), changed data using from elements and want to save data to 2nd db and then to 1st db. I try to make it chaging connectionString for each adapter.
string originalPath = tableAdapterManager.Connection.ConnectionString;
string ns = tableAdapterManager.Connection.ConnectionString.Replace("testchecker.db", "blank_testing.db");
tableAdapterManager.Connection.ConnectionString = ns;
group_testingTableAdapter.Connection.ConnectionString = ns;
groupsTableAdapter.Connection.ConnectionString = ns;
testingTableAdapter.Connection.ConnectionString = ns;
this.tableAdapterManager.UpdateAll((testcheckerDataSet) testcheckerDataSet.Copy() ); // here error
tableAdapterManager.Connection.ConnectionString = originalPath;
group_testingTableAdapter.Connection.ConnectionString = originalPath;
groupsTableAdapter.Connection.ConnectionString = originalPath;
testingTableAdapter.Connection.ConnectionString = originalPath;
this.tableAdapterManager.UpdateAll( testcheckerDataSet );
But I get error "concurrency violation the updatecommand affected 0 from 1". I think It happens because 2nd db has not data at all.
So could somebody advise me how can I save current dataset to another db file and then again to first (original) db?
Thanks.
Problem here is when you call UpdateAll method it try to find previous record from database and update. but here in second db there is no record. what you can try is iterate through all the DataRows in your DataTable and use the DataRow.SetAdded() method to change the row's DataRowState, then it will consider records as newly added.
One more thing:
SetAdded can only be invoked on a DataRow instance where the RowState is Unchanged. you can call AcceptChanges on the DataSet. Then RowState property of each DataRow also changes; Added and Modified rows become Unchanged, and Deleted rows are removed. Now you can call DataRow.SetAdded().
Related
I have a dataset called "titulos" and have 1 table there called "tb"
with the columns with the name "titulo","titulo 2" and "titulo3".
I'm trying to do an insertion of rows in the event onclick of a button
but for some reason my code doesn't work!
My dataset is on a xsd file and I am using visual studio 2013 with c#.
I already tried this code but I don't know how to apply in my situation:
NorthwindDataSet.CustomersRow newCustomersRow =
northwindDataSet1.Customers.NewCustomersRow();
newCustomersRow.CustomerID = "ALFKI";
newCustomersRow.CompanyName = "Alfreds Futterkiste";
northwindDataSet1.Customers.Rows.Add(newCustomersRow);
The problem is that shows an error saying it does not recognize the dataset...
The erros is : "The name " Ds_Admissibilidade" does not exist in the current context
A DataSet is a disconnected copy of the data. It forgets if the data originated from database, an xml file or anything else. When you add rows to the DataSet, you only change the in-memory copy, not the original source.
You need some mechanism to update the source. For databases, a table adapter or dataadapter will do this for you. For a file source, you need to serialize the DataSet to the file, much the reverse of the way you read in in first place.
Hope this helps :)
DataRow newRow = titulos.Tables["tb"].NewRow();
newRow["titulo1"] = "titulo1";
newRow["titulo2"] = "titulo2";
newRow["titulo3"] = "titulo3";
titulos.Tables["tb"].Rows.Add(newRow);
Make sure you're setting all the values of the non nullable parameters. If you're using another instance of the dataset "titulos" use ImportRow instead of Add function.
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.
According to this article : http://msdn.microsoft.com/en-us/library/xzb1zw3x.aspx , after I insert/change data in the dataset, I need to call the UPDATE to synchronize my database data with the dataset data.
My problem is this:
I have created a dataset called dataset1.xsd and then I create a new TableAdapter to do my INSERT query with, and THEN I need to somehow let my database know that the stuff in dataset has changed.
DataSet1TableAdapters.reservationsTableAdapter ta = new DataSet1TableAdapters.reservationsTableAdapter();
ta.Insert(LastName,Arrival,Departure); // this is where I do the INSERT query
Now I should update the dataset, right? How do I do this? The article I posted above suggests doing something like this:
ta.Update(DataSet1.reservationsDataTAble);
However, I can't do this because:
Error - 'myproject.DataSet1.reservationsDataTable' is a 'type', which is not valid in the given context.
I tried declaring a new Dataset1 DataTable, and then updating that, but it still won't show any changes in my database.
I KNOW, however, that the changes are saved in the DataSet, because when I fill a new datatable later, the record is there.
EDIT: Thanks to the comments, below, I tried doing this for a change:
DataSet1.reservationsDataTable NDT = new DataSet1.reservationsDataTable();
DataSet1TableAdapters.reservationsTableAdapter ta = new DataSet1TableAdapters.reservationsTableAdapter();
ta.Insert(LastName,Arrival,Departure);
ta.Fill(NDT);
ta.Update(NDT);
... and I could see (in the debugger) that the NDT datatable DID in fact contain the data that was "INSERTED" and then filled into the data table.
However, the ta.Update(NDT); still did not update my database...
I assume that However, the ta.Update(NDT); still did not update my database means that no sql-insert is executed and that yo get no exception.
does this work for you?
var myDataSet1 = new DataSet1();
var newReservation = myDataSet1.reservations.NewRow();
newReservation.LastName=...
newReservation.Arrival=...
newReservation.Departure=...
myDataSet1.reservations.AddreservationsRow(newReservation);
var ta = new DataSet1TableAdapters.reservationsTableAdapter();
ta.Update(myDataSet1.reservations);
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.
I'm having some trouble getting my DataSet to work.
I have a MDB-Database in the background and created a DataSet out of it. Now I created a new method that lets me create a new user in the table.
But when I call it, nothing happens. No exceptions, no errors and I even get "1" returned as number of affected rows. But when I look in the database, no user was added.
I have the feeling that I miss to somehow tell the DataSet that I want to operate the Database itself rather than just the internal DataSet... How can I achieve this?
// DataSet1 uses the connection to the Database.mdb
// Created by the Designer
// The Users table has 3 columns, id, name and password
DataSet1 set = new DataSet1();
UsersTableAdapter adap = new UsersTableAdapter();
DataSet1.UsersRow row = set.Users.AddUsersRow("asd", "asd");
int count = adap.Insert("das", "das");
MessageBox.Show(row.RowState.ToString() + ": " + count.ToString());
count = adap.Update(set.Users);
set.AcceptChanges();
MessageBox.Show(row.RowState.ToString() + ": " + count.ToString());
// The Messagebox shows: "Added: 1"
// The second one: "Unchanged: 1"
MessageBox.Show(set.Users.Rows.Count.ToString());
// Returns "2"...
Not knowing too much about your code, try reading this how to from MSDN:
http://msdn.microsoft.com/en-us/library/ms233812(v=VS.80).aspx
Update: with data sets, what would normally happen (assuming the data set has been populated) is rows would be added, edited, or deleted in code. These rows would have a corresponding RowState signifying them as added, edited, deleted, or unmodified. During the .Update the associated table adapter, complete with associated insert/update/delete commands, will iterate the rows and execute the command on a given row depending on the row's state. When the rows have been iterated, .AcceptChanges is called, reverting the row state's to default.
If you call AcceptChanges before updating, then nothing will happen. The RowState of each row will be lost so the .Update will not have the required information it needs to perform the update to the database. If you have custom code for wrapping it all in a transaction, then you need to make sure you commit the transaction.
In your example, you seem to imply you are using the table adapter and not the data set itself. I would advise against this - the table adapters usually place methods on the tables in a data set that you should use.
Update 2: the code looks OK to me, though you don't need to call Insert on the adapter, or AcceptChanges after the update (the latter is done automatically). This leads me to believe there is a bug in your insert command SQL - try extracting the SQL used by the command and run it manually against the database.
Update 3: the following code works fine for me:
static void Main(string[] args)
{
db1DataSet set = new db1DataSet();
set.Users.AddUsersRow("asd", "asd");
foreach (DataRow row in set.Users.Rows)
{
object foo = row.RowState; // Confirm row state in debugger.
}
UsersTableAdapter adap = new UsersTableAdapter();
adap.Update(set.Users);
Console.Read();
}