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
2010-03-26 20:41:54
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
2010-03-26 20:42:48
He's just got one row, and he only wants the value from the first column.
John M Gant
2010-03-26 20:44:33
@jmgant - totally agree. Trying to put something for his next scenario :) when more rows may come.
ring bearer
2010-03-26 20:49:22
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
2010-03-26 20:43:36
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
2010-03-26 20:47:42