473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

add new rows of data to a datatable of a dataset

Hi all,

I want to keep adding a row of data each time to the datatable of a dataset.
I see on msdn that I could do this:
ie.
workRow = workTable.NewRow();
workTable.Rows.Add(new Object[] {1, "Smith"});
eg. I have:
DataRow row = ds.Tables["AA"].NewRow();
ds.Tables["AA"].Rows.Add(new Object[] {xxxxxxxxxxx});

I was wondering if I could add a row each time by running a sql statement.
How do I do this instead of using an Object[] array ?? My existing code is as
below:

int intPID=1;
for (int i = 1; i<= 20; i++)
{
strCommandA = "select * from Product where productID="+ intPID;
SqlDataAdapter myCommandA = new SqlDataAdapter(strCommandA, myConnection);
myConnection.Open();
xxxx
myConnection.Close();

intPID++;
}

TIA.

regards,
andrew

Mar 22 '06 #1
4 1654
Hi Andrew,
Why don't you use BETWEEN in your SELECT statement? Instead of doing 20
connections to the DB, now you do it in only 1 shot, then use
DataAdapter.Fill() method to fill the dataset or datatable.

Hope this helps.
VHD50.

"Andrew" wrote:
Hi all,

I want to keep adding a row of data each time to the datatable of a dataset.
I see on msdn that I could do this:
ie.
workRow = workTable.NewRow();
workTable.Rows.Add(new Object[] {1, "Smith"});
eg. I have:
DataRow row = ds.Tables["AA"].NewRow();
ds.Tables["AA"].Rows.Add(new Object[] {xxxxxxxxxxx});

I was wondering if I could add a row each time by running a sql statement.
How do I do this instead of using an Object[] array ?? My existing code is as
below:

int intPID=1;
for (int i = 1; i<= 20; i++)
{
strCommandA = "select * from Product where productID="+ intPID;
SqlDataAdapter myCommandA = new SqlDataAdapter(strCommandA, myConnection);
myConnection.Open();
xxxx
myConnection.Close();

intPID++;
}

TIA.

regards,
andrew

Mar 22 '06 #2
Hi,

Thanks for the reply. Actually my table structure doesn't allow me to use
the BETWEEN statement. The sqlstatement that I have given is just an example
of me requiring a loop to add a new row to the datatable.

Is there anyone else here who can help me out ?
Thanks.

regards,
andrew

Mar 23 '06 #3
Suppose you know the data (how many columns there are & which data goes into
which column...), you can manually build a datarow, then add it to the
datatable. Some thing like this:
DataRow row = ds.Tables["AA"].NewRow();
row.Item(0) = x
row.Item(1) = y
row.Item(2) = z
.....
ds.Tables["AA"].Rows.Add(row);

Hope this helps.
VHD50.


"Andrew" wrote:
Hi,

Thanks for the reply. Actually my table structure doesn't allow me to use
the BETWEEN statement. The sqlstatement that I have given is just an example
of me requiring a loop to add a new row to the datatable.

Is there anyone else here who can help me out ?
Thanks.

regards,
andrew

Mar 23 '06 #4
Hi,

Thanks for replying. What I did eventually was to create an object called row:

DataRow row = ds.Tables["AA"].NewRow();
row.aa = row.retrieveStudentResults2(intX, intY, "aa");
row.bb = row.retrieveStudentResults2(intX, intY, "bb");
ds.Tables["AA"].Rows.Add(new Object[] {row.aa, row.bb,row. cc, ..... });

The issue I am worried about is that this means that I have to make multiple
connections to the db just to populate my ds. I wonder if there is a more
efficient way of doing this.

This seems to work though.

regards,
andrew

"VHD50" wrote:
Suppose you know the data (how many columns there are & which data goes into
which column...), you can manually build a datarow, then add it to the
datatable. Some thing like this:
DataRow row = ds.Tables["AA"].NewRow();
row.Item(0) = x
row.Item(1) = y
row.Item(2) = z
....
ds.Tables["AA"].Rows.Add(row);

Hope this helps.
VHD50.


"Andrew" wrote:
Hi,

Thanks for the reply. Actually my table structure doesn't allow me to use
the BETWEEN statement. The sqlstatement that I have given is just an example
of me requiring a loop to add a new row to the datatable.

Is there anyone else here who can help me out ?
Thanks.

regards,
andrew

Mar 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave Elliott | last post by:
After inserting a new data row to a DataTable that is bound to a datagrid, I am unable to change data in a row that is after the newly added row without getting bizarre results. I have added the...
0
by: Ireneus Broncel | last post by:
I have a class which reads Groups and Users from ActiveDirectory. The Problem is, that i have about 10000 rows as product. When I am trying to read the "memberOf" Objects out of this field i get...
0
by: Dave | last post by:
Tried posting in the Winform Forum without much luck, so posting here... After inserting a new data row to a DataTable that is bound to a datagrid, I am unable to change data in a row that is...
1
by: Andrew | last post by:
Hey all, I am very new to ASP.Net (and .Net in general), but that isn't stopping the boss from wanting to begin new projects in it. This latest project has me kinda stumped and after a couple...
12
by: Graham Blandford | last post by:
Hi all, Would someone be able to tell me the most 'graceful' way of removing unwanted rows from a dataset based on a condition prior to update? OR, resetting the rows all to unchanged after they...
2
by: muntyanu | last post by:
Hi all, I have problem when merging existing DataTable into new dataset. DataSet ds = new DataSet(); while ( done ) { // fill myCustomDataSet.MyTable with data ds.Merge(...
1
by: Lars E | last post by:
Hi all I have a small problem. I have a datatable with 8 columns. But it is only data in 5 of the columns. Data for the remaing 3 columns is in another dataset. I Want to run trough the...
4
by: Andre | last post by:
I am ruinning this in the global.asa VIA Visual Studio VB .NET (framework 1.1) I have a access database with a table called tblCounters with a feild called Counters with on row of data in it...
7
by: Susan Mackay | last post by:
I have a data table that is connected to a database table with a data adapter in the 'standard' manner. However I want to be able to remove selected rows from the data table (i.e. no longer...
4
by: Bobby Edward | last post by:
I have an xsd dataset. I created a simple query called GetDataByUserId. I can preview the data fine! I created a very simple BLL function that calls it and returns a datatable. When I run...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.