日期:2014-05-19  浏览次数:20578 次

一个绑定的radiobuttomlist 为什么显示的时候会比数据库少一项?
很奇怪,还有就是怎么让它第一项默认被选中?

------解决方案--------------------
string sql= "select * from vote order by id ";

string conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data source= " + Server.MapPath( "vote.mdb ");

OleDbConnection oleconn = new OleDbConnection(conn);

oleconn.Open();

OleDbCommand cmd = new OleDbCommand(sql, oleconn);
OleDbDataReader dr = cmd.ExecuteReader();

rdl.DataSource = dr;
rdl.DataTextField = "name ";
rdl.DataValueField = "name ";
rdl.DataBind();

oleconn.Close();

------解决方案--------------------
不能 while(dr.Read())后再绑定,这时候一条数据已经被Read掉了,你上面的代码中不需要用while来判断,直接用
rdl.DataSource = dr;
rdl.DataTextField = "name ";
rdl.DataValueField = "name ";
rdl.DataBind();
就可以了