update query in DataTable??

  • Thread starter Thread starter Guest
  • Start date Start date
ZeroVisio,

If you mean to the back end data source that the data came from, no,
there is not. You will have to create a data adapter that will update the
table appropriately, and make sure that the table only has the change on
that one row. Once you have that, you can run it through the data adapter,
and the data on the back end should be updated.

Hope this helps.
 
Nicholas,

thanks for the reply. I want to just update the dataTable. in my case I'm
not using datatable for getting data from any database. it is just a
dataholder (i found it easy to use datatable than two dimensional dynamic
array for searching certain elements in it).
So how do you it. I saw there is something like acceptChanges but I couldn't
figure how to make changes! Help!!

Nicholas Paldino said:
ZeroVisio,

If you mean to the back end data source that the data came from, no,
there is not. You will have to create a data adapter that will update the
table appropriately, and make sure that the table only has the change on
that one row. Once you have that, you can run it through the data adapter,
and the data on the back end should be updated.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ZeroVisio said:
Hi,

I want to know if there is an easy way to do update a column of a row in
DataTable.
 
ZeroVisio,

But where do you want to make the changes to? Do you just want to
commit them to the DataTable in memory? If that is the case, calling
AcceptChanges will just make it so that the state of each row is unchanged.
Basically, adds, edits, and deletes will be committed (there will not be an
original row version).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ZeroVisio said:
Nicholas,

thanks for the reply. I want to just update the dataTable. in my case I'm
not using datatable for getting data from any database. it is just a
dataholder (i found it easy to use datatable than two dimensional dynamic
array for searching certain elements in it).
So how do you it. I saw there is something like acceptChanges but I
couldn't
figure how to make changes! Help!!

Nicholas Paldino said:
ZeroVisio,

If you mean to the back end data source that the data came from, no,
there is not. You will have to create a data adapter that will update
the
table appropriately, and make sure that the table only has the change on
that one row. Once you have that, you can run it through the data
adapter,
and the data on the back end should be updated.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ZeroVisio said:
Hi,

I want to know if there is an easy way to do update a column of a row
in
DataTable.
 
yaah i just want to commit changes to datatable in memory. righnow i dont
intend to write this table to any database (but maybe in future if it is
needed). here is exactly what im trying to do.

i have seven columns and some rows in my datatable: here is my snapshot of
datatable

id, type,text, start, end, st_flg, end_flg
3, Strategy , pmts, 1, 2, N, N
5, Strategy , print, 1, 4, N, N
7, Strategy, Mail, 1, 6, N, N

now after checking a condiition i want to change N to Y in st_flg and/or
end_flg.

so I retrieve rows that match my criteria and want to update the st_flg
and/or end_flg. However all this in memory. no final commitment to database.

i thought of using datarow.itemarray.setvalue but then dont know how this
will reflect back to my table.

hope you can help!






Nicholas Paldino said:
ZeroVisio,

But where do you want to make the changes to? Do you just want to
commit them to the DataTable in memory? If that is the case, calling
AcceptChanges will just make it so that the state of each row is unchanged.
Basically, adds, edits, and deletes will be committed (there will not be an
original row version).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ZeroVisio said:
Nicholas,

thanks for the reply. I want to just update the dataTable. in my case I'm
not using datatable for getting data from any database. it is just a
dataholder (i found it easy to use datatable than two dimensional dynamic
array for searching certain elements in it).
So how do you it. I saw there is something like acceptChanges but I
couldn't
figure how to make changes! Help!!

Nicholas Paldino said:
ZeroVisio,

If you mean to the back end data source that the data came from, no,
there is not. You will have to create a data adapter that will update
the
table appropriately, and make sure that the table only has the change on
that one row. Once you have that, you can run it through the data
adapter,
and the data on the back end should be updated.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,

I want to know if there is an easy way to do update a column of a row
in
DataTable.
 
ZeroVisio,
The "easiest" way is to use a for each loop. Something like:

' Create a DataTable
DataTable table = new DataTable("ZeroVisio");
table.Columns.Add("id", typeof(int));
table.Columns.Add("type", typeof(string));
table.Columns.Add("text", typeof(string));
table.Columns.Add("start", typeof(string));
table.Columns.Add("end", typeof(string));
table.Columns.Add("st_flg", typeof(char));
table.Columns.Add("end_flg", typeof(char));

' Fill the DataTable
table.Rows.Add(new object[] {3, "Strategy", "pmts", 1, 2, 'N', 'N'}) ;
table.Rows.Add(new object[] {5, "Strategy", "print", 1, 4, 'N', 'N'}) ;
table.Rows.Add(new object[] {7, "Strategy", "Mail", 1, 6, 'N', 'N'}) ;

' Update table Set st_flg = 'Y'
foreach(DataRow row in table.Rows)
{
row["st_flg"] = 'Y';
}

You can use table.Select if you want to restrict to certain rows.

' Update table Set st_flg = 'Y' Where text = 'pmts'
foreach(DataRow row in table.Select("text = 'pmts'"))
{
row["end_flg"] = 'Y';
}

I don't have a sample handy, one could even generalize the above into a
callable routine.

Hope this helps
Jay
 
Hi Jay,

That was really helpful. I had datatow separately and thus wasnt able to
figure out how to reflect the changes back to the original DAtatable. It
works like a charm, thanks a zillion!!!



Jay B. Harlow said:
ZeroVisio,
The "easiest" way is to use a for each loop. Something like:

' Create a DataTable
DataTable table = new DataTable("ZeroVisio");
table.Columns.Add("id", typeof(int));
table.Columns.Add("type", typeof(string));
table.Columns.Add("text", typeof(string));
table.Columns.Add("start", typeof(string));
table.Columns.Add("end", typeof(string));
table.Columns.Add("st_flg", typeof(char));
table.Columns.Add("end_flg", typeof(char));

' Fill the DataTable
table.Rows.Add(new object[] {3, "Strategy", "pmts", 1, 2, 'N', 'N'}) ;
table.Rows.Add(new object[] {5, "Strategy", "print", 1, 4, 'N', 'N'}) ;
table.Rows.Add(new object[] {7, "Strategy", "Mail", 1, 6, 'N', 'N'}) ;

' Update table Set st_flg = 'Y'
foreach(DataRow row in table.Rows)
{
row["st_flg"] = 'Y';
}

You can use table.Select if you want to restrict to certain rows.

' Update table Set st_flg = 'Y' Where text = 'pmts'
foreach(DataRow row in table.Select("text = 'pmts'"))
{
row["end_flg"] = 'Y';
}

I don't have a sample handy, one could even generalize the above into a
callable routine.

Hope this helps
Jay

ZeroVisio said:
Hi,

I want to know if there is an easy way to do update a column of a row in
DataTable.
 
Back
Top