A
Assimalyst
Hi,
I'm attempting to use a data reader to load data from a single table,
tblCountry, with two columns, countryNo (the Key) and countryName, into
a DropDownList box. Here's the code:
protected System.Data.SqlClient.SqlConnection conn;
protected System.Data.SqlClient.SqlDataReader drCountry;
private void Page_Load(object sender, System.EventArgs e)
{
string SQL = "SELECT * FROM tblCountry";
SqlConnection conn = new
SqlConnection("Server=(local);Database=YLCdbSQL;Integrated
Security=SSPI");
SqlCommand countryLoad = new SqlCommand (SQL, conn);
SqlDataReader drCountry = null;
try
{
conn.Open();
drCountry = countryLoad.ExecuteReader();
countryDropLst.DataSource = drCountry;
countryDropLst.DataMember = "countryName";
countryDropLst.DataTextField = "countryName";
countryDropLst.DataValueField = "countryNo";
countryDropLst.DataBind();
}
finally
{
drCountry.Close();
conn.Close();
}
}
But when I run it I get the error "The IListSource does not contain any
data sources", and the DataBind line is highlighted.
I have checked the table using Enterprise Manager, it exists and has
numerous entries in both columns.
Any idea what the problem might be?
Thanks.
I'm attempting to use a data reader to load data from a single table,
tblCountry, with two columns, countryNo (the Key) and countryName, into
a DropDownList box. Here's the code:
protected System.Data.SqlClient.SqlConnection conn;
protected System.Data.SqlClient.SqlDataReader drCountry;
private void Page_Load(object sender, System.EventArgs e)
{
string SQL = "SELECT * FROM tblCountry";
SqlConnection conn = new
SqlConnection("Server=(local);Database=YLCdbSQL;Integrated
Security=SSPI");
SqlCommand countryLoad = new SqlCommand (SQL, conn);
SqlDataReader drCountry = null;
try
{
conn.Open();
drCountry = countryLoad.ExecuteReader();
countryDropLst.DataSource = drCountry;
countryDropLst.DataMember = "countryName";
countryDropLst.DataTextField = "countryName";
countryDropLst.DataValueField = "countryNo";
countryDropLst.DataBind();
}
finally
{
drCountry.Close();
conn.Close();
}
}
But when I run it I get the error "The IListSource does not contain any
data sources", and the DataBind line is highlighted.
I have checked the table using Enterprise Manager, it exists and has
numerous entries in both columns.
Any idea what the problem might be?
Thanks.