views:

117

answers:

2

Hey,

I have a query that returns one row so I want to display it in the Label, but I can't find the property DataSource on it.

How can I do this ?

+1  A: 

If you're using a SqlDataReader in C# then you want something like this

string label;
if (reader.Read())
{
  label = reader.IsDBNull(reader.GetOrdinal("Column"))
    ? String.Empty
    : reader.GetString(reader.GetOrdinal("Column"));
}
reader.Close();
MyLabel.Text = label;

In VisualBasic.Net it will be something like

Dim label as String
If reader.HasRows Then
  Label = reader.GetString(reader.GetOrdinal("ColumnName"))
End If
reader.Close
MyLabel.Text = label
Russ C
It's not working for me because I'm using it with Visual Basic
dotNET
Thank you very much.
dotNET
You're welcome, glad it helped!
Russ C
+1  A: 

If you are only returning one row with one column you might want to use command.ExecuteScalar() instead of a data reader. Then you can just set your label like this:

lblAnswer.Text = myCommand.ExecuteScalar().ToString()
Jon