You're trying to execute two commands on the same connection at the same time.
Looking at the queries, I suspect that the first query is only returning a single value. (If not, your second query isn't going to work.) Try replacing the first
ExecuteReader
with
ExecuteScalar
:
using (SqlConnection con = new SqlConnection(conStr))
{
con.Open();
object routeID;
using (SqlCommand cmdRouteID = new SqlCommand("SELECT RouteID From Route WHERE RouteDepartPlace = @DepartPlace", con))
{
cmdRouteID.Parameters.AddWithValue("@DepartPlace", DropDownList1.Text);
routeID = cmdRouteID.ExecuteScalar();
}
using (SqlCommand cmdDate = new SqlCommand("SELECT ScheduleDepartDate From Schedule WHERE RouteID = @Des", con))
{
cmdDate.Parameters.AddWithValue("@Des", routeID);
using (SqlDataReader reader = cmdDate.ExecuteReader())
{
DropDownList3.DataSource = reader;
DropDownList3.DataValueField = "ScheduleDepartDate";
DropDownList3.DataTextField = "ScheduleDepartDate";
DropDownList3.DataBind();
}
}
}
Alternatively, you could merge the two queries:
using (SqlConnection con = new SqlConnection(conStr))
using (SqlCommand cmdDate = new SqlCommand("SELECT S.ScheduleDepartDate From Schedule As S INNER JOIN Route As R ON R.RouteID = S.RouteID WHERE R.RouteDepartPlace = @DepartPlace", con))
{
cmdDate.Parameters.AddWithValue("@DepartPlace", DropDownList1.Text);
con.Open();
using (SqlDataReader reader = cmdDate.ExecuteReader())
{
DropDownList3.DataSource = reader;
DropDownList3.DataValueField = "ScheduleDepartDate";
DropDownList3.DataTextField = "ScheduleDepartDate";
DropDownList3.DataBind();
}
}