数据绑定 DropDownList 与 OleDbDataReader 从 Ms access 2013.
本文关键字:Ms access 2013 OleDbDataReader DropDownList 数据绑定 | 更新日期: 2023-09-27 18:35:56
我正在尝试将我的下拉列表与我的数据库 MS Access 2013(accdb 文件)进行数据绑定,这是我的代码
protected void Page_Load(object sender, EventArgs e)
{
string str = "Provider=Microsoft.ACE.OleDB.12.0; Data Source=C:''Users''Dima''Documents''Visual Studio 2013''Projects''networklab1''bin''weblabdb.accdb";
OleDbConnection db = new OleDbConnection(str);
db.Open();
string st = "select areaName from area;";
OleDbCommand dbc = new OleDbCommand(st, db);
OleDbDataReader read = dbc.ExecuteReader();
DropDownList1.DataSource = read;
DropDownList1.DataBind();
read.Close();
db.Close();
}
我得到的是一行"System.Data.Common.DataRecordInternal"我的错误是什么以及如何解决这个问题!!谢谢
您缺少检查Postback
以及您缺少DataTextField
和DataValueField
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
string str = "Provider=Microsoft.ACE.OleDB.12.0; Data Source=C:''Users''Dima''Documents''Visual Studio 2013''Projects''networklab1''bin''weblabdb.accdb";
OleDbConnection db = new OleDbConnection(str);
db.Open();
string st = "select areaName from area;";
OleDbCommand dbc = new OleDbCommand(st, db);
OleDbDataReader read = dbc.ExecuteReader();
DropDownList1.DataSource = read;
DropDownList1.DataTextField="areaName"; //missing this
DropDownList1.DataValueField="areaName"; //missing this
DropDownList1.DataBind();
read.Close();
db.Close();
}
}
您缺少 DataTextField 和 DataValueField。试试这个-
protected void Page_Load(object sender, EventArgs e)
{
string str = "Provider=Microsoft.ACE.OleDB.12.0; Data Source=C:''Users''Dima''Documents''Visual Studio 2013''Projects''networklab1''bin''weblabdb.accdb";
OleDbConnection db = new OleDbConnection(str);
db.Open();
string st = "select areaName from area;";
OleDbCommand dbc = new OleDbCommand(st, db);
OleDbDataReader read = dbc.ExecuteReader();
DropDownList1.DataSource = read;
DropDownList1.DataTextField="ShownTextFieldFromDatabaseResults";;
DropDownList1.DataValueField="ValueFieldFromDatabaseResults";
DropDownList1.DataBind();
read.Close();
db.Close();
}