开发者

Displaying data from DataReader in Label control (ASP.NET)

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜