471,353 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 software developers and data experts.

Question about DataTable

Hi,

If I want to programatically add rows to a DataTable, do I call
AcceptChanges per row? Or do I call AcceptChanges after all rows added?

TIA
Jan 21 '06 #1
6 1584
Hello Danny,

That really depends on the scenario. If you add a lot of rows and will use
the dataadapter to persist them to the databse, don't call AcceptChanges
until you done just that.

da.Update(dt)
dt.AcceptChanges();

AcceptChanges will effectivly reset the state flag for each row to indicate
that it hasn't changed.
--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net
Hi,

If I want to programatically add rows to a DataTable, do I call
AcceptChanges per row? Or do I call AcceptChanges after all rows
added?

TIA

Jan 21 '06 #2
Danny,

You don't have to call AcceptChanges.

If you call AcceptChanges, it will set the row state of the row to
unchanged. If you then pass this datatable to a data adapter, it will not
process the row when you try to update your back end data source.

If there is no back end data source, and you have no need for looking at
the row state, then that is fine.

When you are done, you can call AcceptChanges on the DataTable, and it
will make sure that AcceptChanges is called on all of the rows. If you have
a large number of rows in the table, you might want to call AcceptChanges on
each row as you add it (after creating it) through each iteration of your
loop, but that might be considered a premature optimization. You should
only do this if you find that making the call on the DataTable has proven to
be inefficient.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Danny Ni" <dn**@yahoo.com> wrote in message
news:ek**************@tk2msftngp13.phx.gbl...
Hi,

If I want to programatically add rows to a DataTable, do I call
AcceptChanges per row? Or do I call AcceptChanges after all rows added?

TIA

Jan 21 '06 #3
Nicholas,

Thank you for your response.

I am not clear about what you meant by : "pass this datatable to a data
adapter"

My understanding is:
DataAdapter adds a DataTable into a Dataset via da.Fill(ds,"mydt") method.
Then I can insert rows into the datatable and call da.Update method to save
data into backend server. In order for changes in a datatable to be saved to
backend, it must use the same DataAdapter that put it in Dataset.

Please elaborate and correct my confusion.

Thanks Again!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
Danny,

You don't have to call AcceptChanges.

If you call AcceptChanges, it will set the row state of the row to
unchanged. If you then pass this datatable to a data adapter, it will not
process the row when you try to update your back end data source.

If there is no back end data source, and you have no need for looking
at the row state, then that is fine.

When you are done, you can call AcceptChanges on the DataTable, and it
will make sure that AcceptChanges is called on all of the rows. If you
have a large number of rows in the table, you might want to call
AcceptChanges on each row as you add it (after creating it) through each
iteration of your loop, but that might be considered a premature
optimization. You should only do this if you find that making the call on
the DataTable has proven to be inefficient.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Danny Ni" <dn**@yahoo.com> wrote in message
news:ek**************@tk2msftngp13.phx.gbl...
Hi,

If I want to programatically add rows to a DataTable, do I call
AcceptChanges per row? Or do I call AcceptChanges after all rows added?

TIA


Jan 21 '06 #4
Danny,

Well, you don't necessarily have to use a data set that is filled with a
DataAdapter. As long as the structures match up with the various commands
in the adapter, it will work.

I was referring to the Update method. If you call AcceptChanges before
you pass your DataTable to the Update method, it will see the DataTable as
having no changes in it, and then it will do nothing.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Danny Ni" <dn**@yahoo.com> wrote in message
news:eR*************@TK2MSFTNGP11.phx.gbl...
Nicholas,

Thank you for your response.

I am not clear about what you meant by : "pass this datatable to a data
adapter"

My understanding is:
DataAdapter adds a DataTable into a Dataset via da.Fill(ds,"mydt") method.
Then I can insert rows into the datatable and call da.Update method to
save data into backend server. In order for changes in a datatable to be
saved to backend, it must use the same DataAdapter that put it in Dataset.

Please elaborate and correct my confusion.

Thanks Again!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@tk2msftngp13.phx.gbl...
Danny,

You don't have to call AcceptChanges.

If you call AcceptChanges, it will set the row state of the row to
unchanged. If you then pass this datatable to a data adapter, it will
not process the row when you try to update your back end data source.

If there is no back end data source, and you have no need for looking
at the row state, then that is fine.

When you are done, you can call AcceptChanges on the DataTable, and it
will make sure that AcceptChanges is called on all of the rows. If you
have a large number of rows in the table, you might want to call
AcceptChanges on each row as you add it (after creating it) through each
iteration of your loop, but that might be considered a premature
optimization. You should only do this if you find that making the call
on the DataTable has proven to be inefficient.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Danny Ni" <dn**@yahoo.com> wrote in message
news:ek**************@tk2msftngp13.phx.gbl...
Hi,

If I want to programatically add rows to a DataTable, do I call
AcceptChanges per row? Or do I call AcceptChanges after all rows added?

TIA



Jan 21 '06 #5
I think I am clear about the AcceptChanges now.

If you don't mind, I would like to ask another question:
If I have a DataTable not filled into a DataSet by a DataAdapter, how do I
persist changes of this DataTable to backend?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ou**************@TK2MSFTNGP15.phx.gbl...
Danny,

Well, you don't necessarily have to use a data set that is filled with
a DataAdapter. As long as the structures match up with the various
commands in the adapter, it will work.

I was referring to the Update method. If you call AcceptChanges before
you pass your DataTable to the Update method, it will see the DataTable as
having no changes in it, and then it will do nothing.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Danny Ni" <dn**@yahoo.com> wrote in message
news:eR*************@TK2MSFTNGP11.phx.gbl...
Nicholas,

Thank you for your response.

I am not clear about what you meant by : "pass this datatable to a data
adapter"

My understanding is:
DataAdapter adds a DataTable into a Dataset via da.Fill(ds,"mydt")
method. Then I can insert rows into the datatable and call da.Update
method to save data into backend server. In order for changes in a
datatable to be saved to backend, it must use the same DataAdapter that
put it in Dataset.

Please elaborate and correct my confusion.

Thanks Again!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@tk2msftngp13.phx.gbl...
Danny,

You don't have to call AcceptChanges.

If you call AcceptChanges, it will set the row state of the row to
unchanged. If you then pass this datatable to a data adapter, it will
not process the row when you try to update your back end data source.

If there is no back end data source, and you have no need for looking
at the row state, then that is fine.

When you are done, you can call AcceptChanges on the DataTable, and
it will make sure that AcceptChanges is called on all of the rows. If
you have a large number of rows in the table, you might want to call
AcceptChanges on each row as you add it (after creating it) through each
iteration of your loop, but that might be considered a premature
optimization. You should only do this if you find that making the call
on the DataTable has proven to be inefficient.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Danny Ni" <dn**@yahoo.com> wrote in message
news:ek**************@tk2msftngp13.phx.gbl...
Hi,

If I want to programatically add rows to a DataTable, do I call
AcceptChanges per row? Or do I call AcceptChanges after all rows added?

TIA



Jan 21 '06 #6
Danny,

You can add rows and set values on the data table yourself. For adds,
you just add the new row. For edits and deletes, you would have to create
the row in the data table, and then call AcceptChanges on the ROW. Once you
do that, you would have to delete the row or change a value in the row so
that the state is set to delete or edit.

Then you can pass that to your data adapter.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Danny Ni" <dn**@yahoo.com> wrote in message
news:eh**************@TK2MSFTNGP14.phx.gbl...
I think I am clear about the AcceptChanges now.

If you don't mind, I would like to ask another question:
If I have a DataTable not filled into a DataSet by a DataAdapter, how do I
persist changes of this DataTable to backend?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Ou**************@TK2MSFTNGP15.phx.gbl...
Danny,

Well, you don't necessarily have to use a data set that is filled with
a DataAdapter. As long as the structures match up with the various
commands in the adapter, it will work.

I was referring to the Update method. If you call AcceptChanges
before you pass your DataTable to the Update method, it will see the
DataTable as having no changes in it, and then it will do nothing.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Danny Ni" <dn**@yahoo.com> wrote in message
news:eR*************@TK2MSFTNGP11.phx.gbl...
Nicholas,

Thank you for your response.

I am not clear about what you meant by : "pass this datatable to a data
adapter"

My understanding is:
DataAdapter adds a DataTable into a Dataset via da.Fill(ds,"mydt")
method. Then I can insert rows into the datatable and call da.Update
method to save data into backend server. In order for changes in a
datatable to be saved to backend, it must use the same DataAdapter that
put it in Dataset.

Please elaborate and correct my confusion.

Thanks Again!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@tk2msftngp13.phx.gbl...
Danny,

You don't have to call AcceptChanges.

If you call AcceptChanges, it will set the row state of the row to
unchanged. If you then pass this datatable to a data adapter, it will
not process the row when you try to update your back end data source.

If there is no back end data source, and you have no need for
looking at the row state, then that is fine.

When you are done, you can call AcceptChanges on the DataTable, and
it will make sure that AcceptChanges is called on all of the rows. If
you have a large number of rows in the table, you might want to call
AcceptChanges on each row as you add it (after creating it) through
each iteration of your loop, but that might be considered a premature
optimization. You should only do this if you find that making the call
on the DataTable has proven to be inefficient.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Danny Ni" <dn**@yahoo.com> wrote in message
news:ek**************@tk2msftngp13.phx.gbl...
> Hi,
>
> If I want to programatically add rows to a DataTable, do I call
> AcceptChanges per row? Or do I call AcceptChanges after all rows
> added?
>
> TIA
>



Jan 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bruno Rodrigues | last post: by
10 posts views Thread by Shawn Meyer | last post: by
3 posts views Thread by simon | last post: by
4 posts views Thread by angus | last post: by
1 post views Thread by kempshall | last post: by
2 posts views Thread by Asfar | last post: by
5 posts views Thread by jehugaleahsa | last post: by
Sl1ver
reply views Thread by Sl1ver | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.