Soni Gunz
Soni Gunz

Reputation: 135

VB.NET Error Populating 2d Array with database field

i have a query that contains field "kode_brg" i want to fill them into array2d, i'm using mysqldatareader to fill the field into array2d, but it seems that the datareader only read once inside the while looping for more specific, i've added the ss of myproblem belom

i'm pretty sure that i've made a mistake in using the nested for inside the while loop

could somebody help me fix this problem thanks before regards, me enter image description here

EDITED: ADDED A CODE TO A QUESTIONS

    'FILL X

    Dim nil_x As String = "SELECT max( kode_faktur ) FROM detail"
    Dim x As Int32
    Dim CMD_X = New MySqlCommand(nil_x, conn.konek)
    x = Convert.ToInt32(CMD_X.ExecuteScalar())

    'FILL Y

    Dim nil_y As String = "select max(x.jumlah) from (select count(*) as jumlah from      detail group by kode_faktur)x"
    Dim y As Int32
    CMD_Y = New MySqlCommand(nil_y, conn.konek)
    y = Convert.ToInt32(CMD_Y.ExecuteScalar())

    'LOOPING ARRAY TRANS


    Dim msql As String = "select kode_brg from detail group by kode_faktur"
    Dim i, j As Integer
    Dim arayT(,) As String

    CMD = New MySqlCommand(msql, conn.konek)

    'Try
    Dim hasil As MySqlDataReader
    hasil = CMD.ExecuteReader()

    While hasil.Read()

        For i = 0 To x
            ReDim Preserve arayT(x, y)
            For j = 0 To y

                arayT(i, j) = hasil("kode_brg")
                'Continue For
            Next j
            j += 1
            'Continue For
        Next i
        i += 1
        Exit While
    End While

    conn.konek.Close()

    dgv2.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing
    dgv2.RowHeadersWidthSizeMode =    DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders
    dgv2.RowHeadersVisible = False

    With Me.dgv2
        .DataSource = New Mommo.Data.ArrayDataView(arayT)
        .SelectionMode = DataGridViewSelectionMode.FullRowSelect
        .AllowUserToAddRows = False
        .RowHeadersVisible = True
    End With

one big note from me DON'T SUGGEST ME TO USE ARRAYLIST/LIST OF/LINQ/DATASET/DATATABLE because what i really want to do is filling the 2d array with database field, comparing it with another 1d array, then create a new 2d array from both of them, finally i want to display the newly 2d array into the grid.. that's why i chose 2d array instead using dataset to fill my grid

i don't know how to compare list(of) or arraylist with 1d array, that's why i'm using 2d array (basic) thanks you before :)

the desired output

enter image description here

Upvotes: 1

Views: 581

Answers (2)

har07
har07

Reputation: 89315

Assuming you managed to get array size exactly the same as number of data returned from query, you can try to change the loop to following :

ReDim Preserve arayT(x, y)
For i = 0 To (x-1)
    For j = 0 To (y-1)
        hasil.Read()
        arayT(i, j) = hasil("kode_brg")
        'Continue For
    Next j
    'Continue For
Next i

Upvotes: 1

I have no idea of what you're trying to accomplish, but this is how you should do it:

Dim list As New List(Of String(,))

While hasil.Read()

    Dim item(,) As String = New String((x - 1), (y - 1)) {}
    Dim value As String = hasil("kode_brg")

    For i As Integer = 0 To (x - 1)
        For j As Integer = 0 To (y - 1)
            item(i, j) = value
        Next
    Next

    list.Add(item)

End While

Upvotes: 1

Related Questions