473,503 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Web App: How to go through for loop adding string to datarow??

55 New Member
Hi,

I need some help in creating a dataset that I can bind to a datalist after it has added a set of strings to it:

Expand|Select|Wrap|Line Numbers
  1. DataSet ds = new DataSet();
  2. DataTable dt = new DataTable("table");
  3. ds.Tables.Add(dt);
  4. dt.Columns.Add("id");
  5. dt.Columns.Add("description");
  6. dt.Columns.Add("price");
  7.  
  8. for (int i = 0; i < Data.Count; i++)
  9. {
  10. string id = sourceofdata[i].id  //Here is the main problem ** needs to be a cell item
  11.  
  12. string description = sourceofdata[i].description
  13. }
  14.  
  15. DataList1.Datasource = ds;
  16. DataList1.DataBind();
Sep 1 '08 #1
3 1999
vekipeki
229 Recognized Expert New Member
Use the DataTable.NewRow() method to get a new row instance for that table, then set the individual columns for that row, something like this:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < sourceOfData.Length; i++)
  2. {
  3.     DataRow dRow = dt.NewRow();
  4.     dRow["id"] = sourceOfData[i].Id;
  5.     dRow["description"] = sourceOfData[i].Desc;
  6.     dRow["price"] = sourceOfData[i].Price;
  7.     dt.Rows.Add(dRow);
  8. }
  9.  
  10. dataList.DataSource = dt; // bind to a table
  11.  
Sep 1 '08 #2
DonBytes
25 New Member
If sourceofdata is another table that looks the same you can use:

Expand|Select|Wrap|Line Numbers
  1. foreach (DataRow dr in sourceofdata.Rows)
  2. {
  3. ...
  4. }
if it's another DataSet:
Expand|Select|Wrap|Line Numbers
  1. foreach (DataRow dr in sourceofdata.Tables[0].Rows)
  2. {
  3. ...
  4. }
Change 0 to the index you want or the name of the table as string.
Sep 1 '08 #3
jcatubay
12 New Member
Hi did anyone have any problem using this code?

for (int i = 0; i < sourceOfData.Length; i++)
{
DataRow dRow = dt.NewRow();
dRow["id"] = sourceOfData[i].Id;
dRow["description"] = sourceOfData[i].Desc;
dRow["price"] = sourceOfData[i].Price;
dt.Rows.Add(dRow);
}

Everytime I use this, the first dRow does not have any data, it's all null. But then the next time this executes "dt.Rows.Add(dRow); ", the one being inserted is the last dRow which is in i-1. So when I have 5 dRows, the first one is empty then then I'm not getting the last dRow.

I followed pretty much the exact code just the naming is different. What am I doing wrong?
Jun 30 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
5350
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
3
4855
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
2
2022
by: dSchwartz | last post by:
I need help adding a column to a dataset, but its a little bit more complicated then just that. Here's the situation: I have many xml files in one directory, each which represent a newsletter. I...
10
2316
by: Trevor | last post by:
Hey, I am trying to do this tutorial on the microsoft site : http://msdn.microsoft.com/library/default.asp? url=/library/en-us/dndotnet/html/usingadonet.asp I can get everything to work up to...
6
4400
by: Rudy | last post by:
Hi all, I know this is easy, just can't seem to get it. I have a windows form, and a text box, with a value already in it. I need to add that value to a table. It's just one value, so the entire...
8
2448
by: matt | last post by:
hello, can anyone speak to some of the common or preferred methods for building Excel .XLS files, programmatically thru the .NET framework? i have an intranet app that needs to generate &...
4
4673
by: igotyourdotnet | last post by:
How can i loop through my dataset if if colum 1 is there more then once group them together example: dataset looks like this BMW BMW BMW LEXUS LEXUS
6
5637
by: Lord Zoltar | last post by:
I built a small app with VS C# Express 2008, and published it as a click-once app. I tried installing it on my local machine, and when I click on its icon from the Start menu, nothing happens....
1
3342
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
0
7202
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
7086
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
7330
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
7460
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
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
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...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.