- 爱易网页
-
ASP.NET教程
- label绑定有关问题 ~
日期:2014-05-18 浏览次数:20593 次
label绑定问题 ~~~~~~~~~
我想从数据库中读出数据然后绑定到label中
cs文件里是这么写的:
sqlDBHelper db = new sqlDBHelper();//链接数据库类
public SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
string str= "select FileName from News where id=1 ";
try
{
db.Open();
dr = db.CreateSQLdr(str);
dr.Read();
dr[ "FileName "].ToString();
}
catch (Exception error)
{
Response.Write(error.Message.ToString());
}
finally
{
dr.Close();
db.Dispose();
db.Close();
}
}
html代码里写:
<asp:Label ID= "Label1 " runat= "server " Text= ' <%#dr[ "FileName "].ToString()%
> '> </asp:Label>
就是读取不了里面的数据。
请大家帮忙呀 ~~~~~~~~~~~~~~急~~~~~~~~~~~~~~~~~
------解决方案--------------------
public xx = System.String.Empty;
................
xx = dr[ "FileName "].ToString();
<asp:Label ID= "Label1 " runat= "server " Text= ' <%#xx '> </asp:Label>
Label1.DataBind();
------解决方案--------------------
finally
{
dr.Close();
db.Dispose();
db.Close();
}
dr已经关闭了,所以你取不到了,改一下就好;
//定义一个公共变量
public string str= " ";
sqlDBHelper db = new sqlDBHelper();//链接数据库类
public SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
string str= "select FileName from News where id=1 ";
try
{
db.Open();
dr = db.CreateSQLdr(str);
dr.Read();
str=dr[ "FileName "].ToString();
}
catch (Exception error)
{
Response.Write(error.Message.ToString());
}
finally
{
dr.Close();