Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get value from DataTable

I want to get all column value from DataTable and store it to the ListBox. Here is my code

            If myTableData.Rows.Count > 0 Then
                For i As Integer = 0 To myTableData.Rows.Count
                    Dim DataType() As String = myTableData.Rows(i).Item(1)
                    ListBox2.Items.AddRange(DataType)
                Next
            End If

but when I compile that code, I got error message like this :

Unable to cast object of type 'System.String' to type 'System.String[]'

so, how to resolve this problem?? Please help me....

like image 678
Flashidkz Avatar asked Jan 05 '12 17:01

Flashidkz


People also ask

How to get string value from DataTable in c#?

string myname = dt. Rows[i][0]; - Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) object myname = dt.


1 Answers

It looks like you have accidentally declared DataType as an array rather than as a string.

Change line 3 to:

Dim DataType As String = myTableData.Rows(i).Item(1)

That should work.

like image 189
Richard Avatar answered Oct 31 '22 05:10

Richard