asp.net(c#)编程问题

来源:百度知道 编辑:UC知道 时间:2024/05/05 02:24:11
我要在主页(WebForm1.aspx)中动态添加用户控件(hot.ascx)显示热门的商品,就和 http://www.joyo.com/?source=wisdo_011001 卓越上热销商品下的固定格式显示商品信息的类似。
用以下代码执行后显示错误:"指定的转换无效".源错误是:"hpc.ID=sdr.GetString(0);"
谁知道是什么原因帮我找出来,或者提供实现同样功能的代码也可以!
WebForm1.aspx.cs中的代码:
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection con=new SqlConnection("server=.;database=ASPNET;uid=sa;pwd=;");
SqlCommand cmd=new SqlCommand("select id from product",con);
con.Open();
int i=0;
SqlDataReader sdr=cmd.ExecuteReader();
System.Web.UI.WebControls.TableRow tr=new TableRow();
while(sdr.Read())
{
i=i+1;
System.Web.UI.WebControls.TableCell tc=new TableCell();
hot hpc=(hot)this.LoadControl("hot.ascx");
hpc.ID=sdr.GetString(0);//(出错行出错行出错行)
tc.Controls.Add(

ID在C#语言中是一个控件关键字,它的值是不充许更改的,建议你将hot.ascx.cs中的代码定义ID这个变量改个名字再试试看

SqlConnection con=new SqlConnection("server=.;database=ASPNET;uid=sa;pwd=;");

server=.有错误