I am trying to data bind my drop-down list with my database MS Access 2013 (accdb file) this is my code
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();
}
and what I get is one line "System.Data.Common.DataRecordInternal" what is my mistake and how to fix this!!! thank you
You are missing to Check for Postback
as well as Your are missing DataTextField
and 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();
}
}
Your are missing DataTextField and DataValueField.Try this-
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.