add new rows of data to a datatable of a dataset

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.
 
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
 
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.
 
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 said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top