Fill DataTable with DataReader usign ADO.NET

The follwoing Code explains you how to fill DataTable with DataReader and dispaly the data in DropDownList Control.

using System.Data;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
  OleDbConnection conn_MSACCESS=New OleDbConnection
(ConfigurationManager.ConnectionStrings["MS-db1ConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
conn_MSACCESS.Open();
DataTable dtable = new DataTable();
dtable = fillddl();
DropDownList1.DataSource = dtable;
DropDownList1.DataTextField
= "productname";
DropDownList1.DataValueField = "productid";
DropDownList1.DataBind();
conn_MSACCESS.Close();
 }
 public DataTable fillddl()
{
DataTable dt = new DataTable();
OleDbCommand cmd = new OleDbCommand("Select * from vendortbl", conn_MSACCESS);
OleDbDataReader reader = cmd.ExecuteReader();
dt.Load(reader);
return dt;
}
}

Ebook Download
View all
Learn
View all