1

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 ?

0

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

It's not working for me because I'm using it with Visual Basic
2

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

Comments

0

I know this is a bit old thread, but the above did not work for me. But this did:

 If reader.HasRows Then
       label = reader("columnName")
       labelName.Text = label
 End If

smc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.