Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two table which are Route and Schedule, I want to display the ScheduleDepartDate in my drop down list. But it say "There is already an open DataReader associated with this Command which must be closed first."
C#
protected void iDD2_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(conStr);
            con.Open();
            SqlCommand cmdRouteID = new SqlCommand("SELECT RouteID From Route WHERE RouteDepartPlace = @DepartPlace",con);
            cmdRouteID.Parameters.AddWithValue("@DepartPlace", DropDownList1.Text);
            SqlDataReader rdr = cmdRouteID.ExecuteReader();
            SqlCommand cmdDate = new SqlCommand("SELECT ScheduleDepartDate From Schedule WHERE RouteID = @Des", con);
            cmdDate.Parameters.AddWithValue("@Des", rdr);
            SqlDataReader reader = cmdDate.ExecuteReader();

            DropDownList3.DataSource = reader;
            DropDownList3.DataValueField = "ScheduleDepartDate";
            DropDownList3.DataTextField = "ScheduleDepartDate";
            DropDownList3.DataBind();
        }
Posted
Updated 31-Jul-14 5:39am
v3
Comments
OriginalGriff 31-Jul-14 10:14am    
What's the error?
Any message?
Any particular line?

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:
C#
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:
C#
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();
    }
}
 
Share this answer
 
Comments
AlexanderYap 31-Jul-14 11:02am    
If now I want use two drop down list to get my primary key, is it this is the correct way to do it?

SqlConnection con = new SqlConnection(conStr);
con.Open();

object routeID = 0;
SqlCommand cmdRouteID = new SqlCommand("SELECT RouteID From Route WHERE RouteDepartPlace = @DepartPlace", con);
SqlCommand cmdRouteID2 = new SqlCommand("SELECT RouteID From Route WHERE RouteDestination = @Destination", con);
cmdRouteID.Parameters.AddWithValue("@DepartPlace", DropDownList1.Text);
cmdRouteID2.Parameters.AddWithValue("@Destination", iDD2.Text);

if (cmdRouteID.ExecuteScalar() == cmdRouteID2.ExecuteScalar())
{
routeID = cmdRouteID.ExecuteScalar();
}

SqlCommand cmdDate = new SqlCommand("SELECT ScheduleDepartDate From Schedule WHERE RouteID = @Des", con);
cmdDate.Parameters.AddWithValue("@Des", routeID);

SqlDataReader reader = cmdDate.ExecuteReader();
DropDownList3.DataSource = reader;
DropDownList3.DataValueField = "ScheduleDepartDate";
DropDownList3.DataTextField = "ScheduleDepartDate";
DropDownList3.DataBind();
Richard Deeming 31-Jul-14 11:05am    
Again, you're executing four queries when 1 will do the job:

SELECT S.ScheduleDepartDate FROM Schedule As S INNER JOIN Route As R ON R.RouteID = S.RouteID WHERE R.RouteDepartPlace = @DepartPlace And R.RouteDestination = @Destination
Richard Deeming 31-Jul-14 11:21am    
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 And R.RouteDestination = @Destination", con))
{
cmdDate.Parameters.AddWithValue("@DepartPlace", DropDownList1.Text);
cmdDate.Parameters.AddWithValue("@Destination", iDD2.Text);

con.Open();
using (SqlDataReader reader = cmdDate.ExecuteReader())
{
DropDownLIst3.DataSource = reader;
DropDownLIst3.DataValueField = "ScheduleDepartDate";
DropDownLIst3.DataTextField = "ScheduleDepartDate";
DropDownLIst3.DataBind();
}
}
AlexanderYap 31-Jul-14 11:22am    
Thank you, I get it
It's ok - I can see the problem.
The error you will be getting is along the lines of:
There is already an open DataReader associated with this Command which must be closed first

Which is pretty self explanatory.
You can't open two readers on the same connection object. Either finish the first reader and close it, or open a second connection for the second reader.

Or use SqlDataAdapters instead.
 
Share this answer
 
Comments
Rob Philpott 31-Jul-14 11:58am    
Yes, you can. Connection string option thingy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900