Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# - DataSet Not Saving
Message
From
15/04/2008 16:05:29
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01310720
Message ID:
01310899
Views:
13
IIRC, when you do DataAdapter.Update on a DataSet, it does AcceptChanges automatically. If you were manually spinning through each record and manually saving each record using a SqlCommand directly, you would want to do DataSet.AcceptChanges.

For your second question, you will need to configure a SqlCommand and use that.

>YES! Success!! Thanks Mike.
>
>So when would I ever use "oDataSet.AcceptChanges()"?
>
>--------
>
>There's one more part of this I'm still confused on. If I were pulling a customer record, then I use what I already
>have.
>
>
>When adding a new recod, I have:
>
>
>DSCustomer oDataSet = new DSCustomer();
>
>oDataSet.Tables[0].Rows.Add();
>
>DataTable oTable = oDataSet.Tables[0];
>
>int iRowCount = oTable.Rows.Count - 1;
>
>DataRow oRow = oTable.Rows[iRowCount];
>
>oRow["CustomerName"] = "Millard Filmore";
>oRow["CreditLimit"] = 2520;
>
>SqlCommandBuilder oBuilder = new SqlCommandBuilder(oDataAdapter);
>
>oDataAdapter.Update(oDataSet, oDataSet.Tables[0].TableName);
>
>
>This throws an error because there is no data adapter. If works if I do:
>
>string sSelectCommand = "select * from Customers where CustomerKey = 1";
>SqlDataAdapter oDataAdapter = new SqlDataAdapter(sSelectCommand, oConnection);
>
>
>before hand. But my question is that this seems to be making a call into the DB just to save a new record.
>
>Not sure if my question makes sense.
>
>
>
>
>
>
>
>>>I'm pretty sure I remember Bonnie posting an example in C# in an earlier thread to you (maybe 2-3 weeks ago). I would check that out. Basically, you will loop through all the records in your DataTable, check their version, and handle it based on that.
>>>
>>>Ok, looking back on Bonnies examples, I now have this. Again, it compiles and doesn't error, but the data doesn't
>>>make it to the back end. The new row is in the data set. What am I doing wrong here???
>>>
>>>
>>>
>>>/*****************************************************
>>> * Get the customer record
>>>******************************************************/
>>>
>>>string sSelectCommand = "select * from Customers where CustomerKey = 1";
>>>
>>>SqlDataAdapter oDataAdapter = new SqlDataAdapter(sSelectCommand, oConnection);
>>>DSCustomer oDataSet = new DSCustomer();
>>>oDataAdapter.Fill(oDataSet, "DSCustomer");
>>>
>>>/*****************************************************
>>> * Or Create a new blank data set record
>>>******************************************************/
>>>//DSCustomer oDataSet = new DSCustomer();
>>>
>>>// Add a blank row
>>>oDataSet.Tables[0].Rows.Add();
>>>
>>>
>>>// Get a reference to the table
>>>DataTable oTable = oDataSet.Tables[0];
>>>
>>>// Get the number of rows
>>>int iRowCount = oTable.Rows.Count - 1;
>>>
>>>// Get a reference to the new row
>>>DataRow oRow = oTable.Rows[iRowCount];
>>>
>>>// Edit the column values
>>>oRow["CustomerName"] = "Someone Else";
>>>oRow["CreditLimit"] = 15;
>>>
>>>// Save the changes to the dataset
>>>oDataSet.AcceptChanges();
>>>
>>>// Create a command builder.
>>>SqlCommandBuilder oBuilder = new SqlCommandBuilder(oDataAdapter);
>>>
>>>// Update the changes to the backend
>>>oDataAdapter.Update(oDataSet, oDataSet.Tables[0].TableName);
>>>
>>>
>>
>>Your DataSet.AcceptChanges line is marking your DataSet as current, so there are no records to update. Remove that line.
Very fitting: http://xkcd.com/386/
Previous
Reply
Map
View

Click here to load this message in the networking platform