asp.net label标签中写html标签,ASP.NET C#在标签中显示数据-System.Web.UI.WebControls.Label...

I'm trying to pull a value from SQL into a label in ASP.Net using C# but it is just dispaying System.Web.UI.WebControls.Label.

I've run in debug and I can see the C# is pulling back the value I expect, how do I get this to display in the ASP?

C# Code:

string connectionString = ConfigurationManager.ConnectionStrings["SEV2"].ConnectionString;

string commandText = "SELECT COUNT (*) AS TotalApplicants FROM (SELECT tbl_Candidate.CandID, tbl_Candidate.FirstName, tbl_Candidate.LastName FROM tbl_Candidate INNER JOIN tbl_CandProfile ON tbl_Candidate.CandID = tbl_CandProfile.CandID) a";

DataTable dt = new DataTable();

SqlConnection con = new SqlConnection(connectionString);

con.Open();

SqlCommand command = new SqlCommand(commandText, con);

SqlDataReader reader = command.ExecuteReader();

while (reader.Read())

{

lblTotalApplicants = reader["TotalApplicants"].ToString();

}

this.DataBind();

con.Close();

ASP.Net Code:

Total Applicants

Solutions1

Your query returns an integer value, so you want to use ExecuteScalar.

Code Behind

protected void Page_Load(object sender, EventArgs e)

{

string connectionString =

ConfigurationManager.ConnectionStrings["SEV2"].ConnectionString;

string commandText = "YOUR QUERY";

using (var con = new SqlConnection(connectionString))

using (var command = new SqlCommand(commandText, con))

{

con.Open();

lblTotalApplicants.Text = command.ExecuteScalar().ToString();

}

}

ASPX

Remove

Total Applicants

Solutions2

You shouldn't bind the content inside the Label control. You should use the Text property. This should do the job:

Remember that totalApplicants should be a protected variable on your page.

You could also just set the Text property server-side:

while (reader.Read())

{

lblTotalApplicants.Text = reader["TotalApplicants"].ToString();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值