在开发当中遇到这样一个古怪问题:
下面是代码:
public SqlDataReader GetSong()
{
SqlConnection mycon = new SqlConnection(myconnetion);
string select = "";
if (Session["Singer_ID"] != null)
{
select = @"select * from YSong
where YSingerID=" + Session["Singer_ID"].ToString() + " order by Song_date desc";
}
else
{
Response.Redirect("../NotLogin.aspx");
} SqlCommand cmd = new SqlCommand(select, mycon);
mycon.Open(); SqlDataReader dr = cmd.ExecuteReader(); return dr;
}
protected void BindDropDownList()
{
SqlDataReader dr = GetSong();
if(dr.Read())
{
this.DropDownList1.DataSource = dr;
this.DropDownList1.DataTextField = "YSongname";
this.DropDownList1.DataValueField = "YSongID";
this.DropDownList1.DataBind();
}
dr.Close(); }
问题是,当调用了BindDropDownList()方法绑定后,无论如何,DropDownList下拉窗口都没有第一个值。而当数据表中仅有一个值时,则是什么都没有。
比如:
表中有
YSongname YSongID
121 131
1232 1231
则只能看到第二个
若只有第一行的数据时,则什么都不显示
希望各位高手指点一二...
下面是代码:
public SqlDataReader GetSong()
{
SqlConnection mycon = new SqlConnection(myconnetion);
string select = "";
if (Session["Singer_ID"] != null)
{
select = @"select * from YSong
where YSingerID=" + Session["Singer_ID"].ToString() + " order by Song_date desc";
}
else
{
Response.Redirect("../NotLogin.aspx");
} SqlCommand cmd = new SqlCommand(select, mycon);
mycon.Open(); SqlDataReader dr = cmd.ExecuteReader(); return dr;
}
protected void BindDropDownList()
{
SqlDataReader dr = GetSong();
if(dr.Read())
{
this.DropDownList1.DataSource = dr;
this.DropDownList1.DataTextField = "YSongname";
this.DropDownList1.DataValueField = "YSongID";
this.DropDownList1.DataBind();
}
dr.Close(); }
问题是,当调用了BindDropDownList()方法绑定后,无论如何,DropDownList下拉窗口都没有第一个值。而当数据表中仅有一个值时,则是什么都没有。
比如:
表中有
YSongname YSongID
121 131
1232 1231
则只能看到第二个
若只有第一行的数据时,则什么都不显示
希望各位高手指点一二...
解决方案 »
- 动态加载用户控件,却无法调用里面的事件
- 附件上传后可以直接修改,如何解决,回答必给分
- (求助!!) 偶的项目在VS2003下开发了一大半了,现在BOSS要求换成VS2005的,如何进行转换啊?
- 接收不了FORM的值,如何解决,在线等!!!!
- 如何将数据库中图片放入剪贴板中?
- 100分求助概念性问题~~~~~~~~高手请指点~~~~~~~~~~~
- 比较简单的问题,想要求救!
- 问一个问题.高分.关于cookie 和idasm.exe工具
- 绑定了HyperlinkColumn的DataGrid怎么取值
- 水晶报表只能支持5个用户
- 对于泛型IList提供的数据怎么实现分页和排序?
- 求一正则,写了好久没有成功
this.DropDownList1.DataSource = dr;
this.DropDownList1.DataTextField = "YSongname";
this.DropDownList1.DataValueField = "YSongID";
this.DropDownList1.DataBind();
直接这样绑定就ok了,不要加那句!!!
SqlDataReader dr = GetSong(); this.DropDownList1.DataSource = dr;
this.DropDownList1.DataTextField = "YSongname";
this.DropDownList1.DataValueField = "YSongID";
this.DropDownList1.DataBind();
dr.Close();
好像不习惯使用datareader做数据源的,因为要把datareader绑定到数据源,datareader不能关,而且connection也不能关,绑定过后才能关,根据使用连接的习惯吧,使用过后都及时关闭,这样再绑定,可能忘记关闭吧,我的想法。一般使用dataset,datatable,arraylist等容器做数据源。