acadia
acadia

Reputation: 2621

first column value in a datatable

I have a dataset with just 1 datatable and 1 row but with 2 columns. I want to get the value of the 1st column. How can I get it in vb.net

Upvotes: 2

Views: 22141

Answers (3)

John M Gant
John M Gant

Reputation: 19308

All you need is

ds.Tables(0).Rows(0)(0)

where ds is the name of your DataSet object. This will return the first column from the first row of the first table as an Object.

Upvotes: 9

systempuntoout
systempuntoout

Reputation: 74104

Try with:

public class MainClass
   Shared Sub Main()
      Dim thisConnection As New SqlConnection("yourconnection")
      Dim thisCommand As New SqlCommand _
         ("SELECT FirstField FROM YourTable",thisConnection)
      Try
         thisConnection.Open()
         Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()   
         While (thisReader.Read())
               MessageBox.Show(thisReader.GetValue(0))
         End While
      Finally
         thisConnection.Close()
      End Try
   End Sub
End Class

Upvotes: 0

ring bearer
ring bearer

Reputation: 20783

Private Sub PrintValues(ByVal myTable As DataTable)
    Dim myRow As DataRow
    Dim myColumn As DataColumn
    For Each myRow in myTable.Rows
       For Each myColumn In myTable.Columns
          Console.WriteLine(myRow(myColumn))
          Exit For
       Next
    Next
 End Sub

Upvotes: 0

Related Questions