DropDownList with DataReader
According to Wrox book, the authour has mentioned that using data readers is the most efficient way to fetch data directly. So I try to bind the data to my DropDownList using data readers instead of DataSet, but I could't get the desire result.
My coding is as below:
void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DDList.DataSource = GetEmployeeID();
DDList.DataBind();
}
}
System.Data.SqlClient.SqlDataReader GetEmployeeID()
{
string connectToSqlServer = "server=(local);database=Northwind;" +
"integrated security=true;";
System.Data.SqlClient.SqlConnection DatabaseConnection =
new System.Data.SqlClient.SqlConnection(connectToSqlSe rver);
string queryCommand = "select EmployeeID from Employees order by EmployeeID ASC";
System.Data.SqlClient.SqlCommand dbCommand =
new System.Data.SqlClient.SqlCommand();
dbCommand.CommandText = queryCommand;
dbCommand.Connection = DatabaseConnection;
DatabaseConnection.Open();
System.Data.SqlClient.SqlDataReader myDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavio r.CloseConnection);
return myDataReader;
}
So, what is the mistake at the coding? One thing is that in Wrox book, it never mention the use of System.Data.SqlClient.SqlDataReader, I add it as a test (I don't know whether this is the syntax that actually exist or otherwise).
|