 |
| ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application . |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 23rd, 2004, 02:54 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Updating disconnected datasets
How can I reconnect a disconnected dataset to it's original datasource and use the dataset's Update method to update the datasource? I can't seem to update the datasource from an aspx page once the oledbConnection object used to get the original data has been closed and disposed of. I can update the dataset, but not the datasouce.
Jarrod
|
|

July 23rd, 2004, 03:31 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hey,
You need to use the SQLDataAdapter to do the update. You will also need to reestablish and open the connection to the database when you do SQLDataAdapter.Update.
Do you use stored procedures or SQL queries to do the update?
Brian
|
|

July 24th, 2004, 12:59 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
you told you can update your dataset but not your datasource it's because of that you don't send your sql queries(like Update,Insert,...) to your datasource when you just use Update method through your Adapter.
in this case you should use a CommandBuilder(SqlCommandBuilder,OlDbBuilder,....) to make it send your queries to your datasource for affecting it
ofcourse another way could be building yor commands within your Adapter and then use update method in your Adapter
but easiest way is using CommandBuilder(just in one table)....
(just create one CommandBuilder and then update your adapter)
Hope this helps you...
--------------------------------------------
Mehdi
I'm waiting for your better idea.
|
|

July 26th, 2004, 07:54 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I'm still not getting my data source to update. Here is my sample code. Is it neccessary to refill the DataSet?
protected void doUpdate(object o, DataGridCommandEventArgs e)
{
OleDbConnection conn = new OleDbConnection();
try
{
// Code to get updated values from DataGrid
// Fetch the cached DataSet from the Session object
DataSet ds = getDataSet();
// Code to update the appropriate DataRow
ds.AcceptChanges();
DataView dv = ds.Tables[0].DefaultView;
DataGrid1.EditItemIndex = -1;
// Reconnect to the database and Open
conn.ConnectionString = connString;
conn.Open();
// Make a new OleDbDataAdapter
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM GAI_FN_SPEC_CLAIMS", conn);
// Make a new OleDbCommandBuilder
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
// Do the update and rebind the DataSet to the DataGrid
da.Update(ds);
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
|
|

July 26th, 2004, 09:48 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hey,
You can't execute AcceptChanges before the update. AcceptChanges makes the RowState back to Unchanged (which the added rows should be Added RowState value and the updated rows should be Modified RowState value).
The Update statement should call AcceptChanges for you
Brian
|
|

July 26th, 2004, 09:49 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hey,
Also, just saw something, try getting the data view after you call the update statement, so you have the most accurate version. If you have an identity, the right value won't be sent back to the dataset until you call the update statement, and any other database changes such as from a trigger, default, etc.
Brian
|
|

July 26th, 2004, 10:37 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Many thanks. AcceptChanges was killing me.
Jarrod
|
|
 |