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 ?
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 ?
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
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()