tags:

views:

36

answers:

3

Hello, 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

+1  A: 

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.

John M Gant
A: 
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
ring bearer
He's just got one row, and he only wants the value from the first column.
John M Gant
@jmgant - totally agree. Trying to put something for his next scenario :) when more rows may come.
ring bearer
A: 

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
systempuntoout
thisReader.ExecuteScalar would be a better option here. No need to open a reader just to read a single value.But if I'm reading the question right he doesn't want to connect to a database.
John M Gant
You are right in both points :).
systempuntoout