积极答复者
在Web中给dropdownlist控件绑定数据,用sqlcommand对象为什么不能绑定数据库中的第一条数据?求解…

问题
-
SqlCommand cmd = new SqlCommand("select * from categories", con);
con.Open();
SqlDataReader read = cmd.ExecuteReader();
if (read.Read())
{
DropDownList1.DataSource = read;
DropDownList1.DataTextField = "categoryname";
DropDownList1.DataValueField = "categoryid";
DropDownList1.DataBind();
}
read.Dispose();
con.Close();在categories表中有8条数据,但这段代码为什么不能绑定到第一条数据?…
答案
-
你好:)
删除“if……”部分,建议这样做:SqlCommand cmd = new SqlCommand("select * from categories", con);
con.Open();
SqlDataReader read = cmd.ExecuteReader();
DropDownList1.DataSource = read;
DropDownList1.DataTextField = "categoryname";
DropDownList1.DataValueField = "categoryid";
DropDownList1.DataBind();
con.Close();如果依旧不行,请直接使用SqlDataAdapter+DataTable方式填充并且绑定:
using (SqlDataAdapter adapter = new SqlDataAdapter("select * from xxx"), new SqlConnection("……"))
{
DataTable dt = new DataTable();DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "categoryname";
DropDownList1.DataValueField = "categoryid";
DropDownList1.DataBind();}
如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已建议为答案 ChiYauModerator 2011年6月10日 5:00
- 已标记为答案 Lie YouModerator 2011年6月17日 9:58
-
Hi,
Datareader.read() 方法如果存在多个行,则为 true;否则为 false。
如果按照您的写法,if(read.Read()),当读到最后一条数据之后,由于没有下一条了,此时就会返回false,当然最后一条数据也是不能进入到 if 代码块的。
关于Datareader.read() 的MSDN 详细解释:
谢谢您的支持和理解。
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Lie YouModerator 2011年6月17日 9:58
-
- 已标记为答案 Lie YouModerator 2011年6月17日 9:58
全部回复
-
我想你是指绑定后的数据 没有选择第一条么?
你可以使用DropDownList1的selectindex属性来指定选择的记录。
family as water- 已建议为答案 ChiYauModerator 2011年6月10日 5:00
-
你好:)
删除“if……”部分,建议这样做:SqlCommand cmd = new SqlCommand("select * from categories", con);
con.Open();
SqlDataReader read = cmd.ExecuteReader();
DropDownList1.DataSource = read;
DropDownList1.DataTextField = "categoryname";
DropDownList1.DataValueField = "categoryid";
DropDownList1.DataBind();
con.Close();如果依旧不行,请直接使用SqlDataAdapter+DataTable方式填充并且绑定:
using (SqlDataAdapter adapter = new SqlDataAdapter("select * from xxx"), new SqlConnection("……"))
{
DataTable dt = new DataTable();DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "categoryname";
DropDownList1.DataValueField = "categoryid";
DropDownList1.DataBind();}
如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已建议为答案 ChiYauModerator 2011年6月10日 5:00
- 已标记为答案 Lie YouModerator 2011年6月17日 9:58
-
Hi,
Datareader.read() 方法如果存在多个行,则为 true;否则为 false。
如果按照您的写法,if(read.Read()),当读到最后一条数据之后,由于没有下一条了,此时就会返回false,当然最后一条数据也是不能进入到 if 代码块的。
关于Datareader.read() 的MSDN 详细解释:
谢谢您的支持和理解。
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Lie YouModerator 2011年6月17日 9:58
-
- 已标记为答案 Lie YouModerator 2011年6月17日 9:58