473,387 Members | 1,365 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,387 software developers and data experts.

Writing Data from DataTable

I am attempting to write some information contained in a datatable to a
textbox on my form.

I do it like this:

// get the number of columns
int intCols = myDataSet.Tables[0].Columns.Count;
// get the number of rows
int intRows = myDataSet.Tables[0].Rows.Count;
foreach (DataRow row in myDataSet.Tables[0].Rows) {
for(int i = 0; i < intCols ; i++) {
textBox2.Text += (string) row[i] + " , ";
}
textBox2.Text += Environment.NewLine;
}

It is taking a VERY long time, over 10 mins for less than 2000 rows.
Does anyone have any siggestions on making this faster?
Oct 28 '07 #1
2 4285
"Mick Walker" <ma**********@privacy.netwrote in message
news:5o************@mid.individual.net...
// get the number of columns
int intCols = myDataSet.Tables[0].Columns.Count;
// get the number of rows
int intRows = myDataSet.Tables[0].Rows.Count;
foreach (DataRow row in myDataSet.Tables[0].Rows) {
for(int i = 0; i < intCols ; i++) {
textBox2.Text += (string) row[i] + " , ";
}
textBox2.Text += Environment.NewLine;
}

It is taking a VERY long time, over 10 mins for less than 2000 rows.
Does anyone have any siggestions on making this faster?
I suspect that the problem may be in the string concatenations. Strings
in .Net are immutable, so every time that you change the string it has to be
discarded and a new string allocated instead. These operations are slow. It
should be faster if you use a StringBuilder:

using System.Text;
....
int intCols = myDataSet.Tables[0].Columns.Count;
int intRows = myDataSet.Tables[0].Rows.Count;
StringBuilder sb = new StringBuilder();
foreach (DataRow row in myDataSet.Tables[0].Rows) {
for(int i = 0; i < intCols ; i++) {
sb.Append((string) row[i] + " , ");
}
sb.Append(Environment.NewLine);
}
textBox2.Text = sb.ToString();
Oct 28 '07 #2
Alberto Poblacion wrote:
"Mick Walker" <ma**********@privacy.netwrote in message
news:5o************@mid.individual.net...
>// get the number of columns
int intCols = myDataSet.Tables[0].Columns.Count;
// get the number of rows
int intRows = myDataSet.Tables[0].Rows.Count;
foreach (DataRow row in myDataSet.Tables[0].Rows) {
for(int i = 0; i < intCols ; i++) {
textBox2.Text += (string) row[i] + " , ";
}
textBox2.Text += Environment.NewLine;
}

It is taking a VERY long time, over 10 mins for less than 2000 rows.
Does anyone have any siggestions on making this faster?

I suspect that the problem may be in the string concatenations.
Strings in .Net are immutable, so every time that you change the string
it has to be discarded and a new string allocated instead. These
operations are slow. It should be faster if you use a StringBuilder:

using System.Text;
...
int intCols = myDataSet.Tables[0].Columns.Count;
int intRows = myDataSet.Tables[0].Rows.Count;
StringBuilder sb = new StringBuilder();
foreach (DataRow row in myDataSet.Tables[0].Rows) {
for(int i = 0; i < intCols ; i++) {
sb.Append((string) row[i] + " , ");
}
sb.Append(Environment.NewLine);
}
textBox2.Text = sb.ToString();

WOW!

Thank you. I knew of the StringBuilder object, but I had never had a
casue to use it. In this case it cut execution time down to around 2.1
seconds, and for 40,000 rows, it was around 5 seconds. That is some
performance gain.

Thanks very much Alberto
Oct 28 '07 #3

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

Similar topics

0
by: elcc1958 | last post by:
I need to support a VB6 application that will be receiving disconnected ADODB.Recordset from out DotNet solution. Our dotnet solution deals with System.Data.DataTable. I need to populate a...
5
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
4
by: Simon | last post by:
Hi all, I have a process, where I take a dataset from an SQL call, and need to write an XML file from that dataset. The data set can contain 10's of tables, each with 100's of rows, and I have...
1
by: VMI | last post by:
Is it possible to store the data in a datatable in the hard disk instead of the memory? By default, when a datatable's being filled, the table (and data) will remain in memory. Would it be possible...
9
by: VMI | last post by:
We have this huge application that's based on storing tons of data on a dataTable. The only problem we're having is that storing LOTS of data (1 million records) into a datatable will slow down the...
13
by: Leszek Taratuta | last post by:
Hello, I have several drop-down lists on my ASP.NET page. I need to keep data sources of these lists in Session State. What would be the most effective method to serialize this kind of data...
6
by: Tejpal Garhwal | last post by:
I have datagrid filled with some data rows. At the run time i want know how many total rows are there in the data grid ? Any idea ? Any Suggestions ? Thanks in advance Tej
19
by: Noozer | last post by:
I need to keep my application settings in a file that users can copy/backup/etc. Before I start using the old INI file standard, is there any easy way to use XML files to hold application...
9
by: Anil Gupte | last post by:
After reading a tutorial and fiddling, I finally got this to work. I can now put two tables created with a DataTable class into a DataRelation. Phew! And it works! Dim tblSliceInfo As New...
6
by: rcoco | last post by:
Hi, I have a datagrid that is ment to insert data. But when I run the form only the header appears. I would like some advise from you all and solve this problem I'm using visual studio 2003. My...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.