system.outofmemory exception

C

cloud

Hi all,

I have a datagridview that has 200000 rows and 5 columns. I need to
extract each row with all the column data and collect in string.

My code is as below:

Dim myList As New List(Of Long)
Dim fieldArray as string
Dim resultArray as string = ""

For Each dr1 As DataGridViewRow In Me.DataGridView1.SelectedRows
myList.Add(dr1.Index)
Next

myList.Sort()

For Each nItem As Integer In myList

Dim dr2 As DataGridViewRow = Me.DataGridView1.Rows(nItem)
ReDim dataGridViewArray(0 To dr2.Cells.Count - 1)

For Each s3 As DataGridViewCell In dr2.Cells
dataGridViewArray(s3.ColumnIndex) =
s3.Value.ToString
Next

fieldArray = ""

'Getting all the column fields from the datagridview
For j = 0 To dataGridViewArray.Length - 1
fieldArray = fieldArray + dataGridViewArray(j)
Next

'Add all the content to the resultArray
resultArray = resultArray + fieldArray ---> exception
next

I have to get all the data in a string form or other
It works when i have less amount of data but if i have massive amounts
of data i get Systen.outofmemoryexception
at resultArray statement.
Thank you for any suggestion...
 
J

James Hahn

What are you doing with that string when you have created it? It is
unlikely that you need to have that whole string all in memory at once.

It seems that what you need to do is start processing the string so that you
can get rid of the part that is already processed and not continually add to
it.

For instance, if you are writing it to a file, then write out the text every
time you get a new line.

If you did get all that data into one string, it would be unmanageable.
 
P

Patrice

Sometimes you can also use the stringbuilder class. When you concat two
strings you create a new string so you have to have twice the size. A
stringbuilder is more efficient...

In this particular case I would second James advice. Double check what you
do with the string and see if you really need all at once...

The size for each field would be ? You'll see with a simple calculation that
is will use up a fair amount of memory ... You may want to explain what you
are doing then with this string...

--
Patrice


"cloud" <[email protected]> a écrit dans le message de groupe de
discussion :
(e-mail address removed)...
 

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

Top