问题描述
今天看书做习题,习题要求在asp.net中(C#语言)在页面加载的时候添加checkboxlist控件内容。我是这个作的,先在App_Code中添加了一个类。usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Data.SqlClient;///<summary>///Class1的摘要说明///</summary>///publicclassDB{publicDB(){////TODO:在此处添加构造函数逻辑//}publicstaticSqlConnectioncreatConnection(){System.Data.SqlClient.SqlConnectioncon=newSqlConnection("server=.;database=login;uid=sa;pwd=;");returncon;}}然后在主页中的代码:usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Data.SqlClient;publicpartialclass_Default:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){SqlConnectioncon=DB.creatConnection();con.Open();SqlCommandcmd=newSqlCommand("select*frompersonLike",conSystem.Data.SqlClient.SqlDataReadersdr=newSqlDataReader();this.CheckBoxList1.DataTextField="likeContent";this.CheckBoxList1.DataValueField="id";this.CheckBoxList1.Text=sdr;this.CheckBoxList1.DataBind();sdr.Close();con.Close();}}运行结果显示:错误1类型“System.Data.SqlClient.SqlDataReader”未定义构造函数D:WebSite6Default.aspx.cs1951D:WebSite6错误2无法将类型“System.Data.SqlClient.SqlDataReader”隐式转换为“string”D:WebSite6Default.aspx.cs2235D:WebSite6这是怎么回事儿?应该如何修改代码?在线等待&……
解决方案
解决方案二:
使用下面的語句來創建SQLDataReader的對象來試試.SqlDataReadersdr=command.ExecuteReader();
解决方案三:
Soory!!command--->cmd
解决方案四:
SqlConnectionMyconn=newSqlConnection(Myconnection);Myconn.Open();SqlCommandMycomm=newSqlCommand(MySql,Myconn);CheckBoxList1.DataSource=Mycomm.ExecuteReader();CheckBoxList1.DataTextField="字段";CheckBoxList1.DataValueField="字段";CheckBoxList1.DataBind();Myconn.Close();