Using Multiple Data Readers

You can have two active datareaders in Sql Server 2000 by simply creating two connections.

To demonstrate this, I must first berate you for using two very poor practices: dynamic sql and arraylists. Neither have any place in your code. You should also read up on the using construct, though you have my apologies and condolences on "using" and "arraylists" if you're still using .net 1.1.

That said, here's how the code should look:

string sql_Phone = "SELECT Phone_Number FROM Contact_Details WHERE Emp_ID = @EmpID";
using (SqlConnection cn2 = new Sqlconnection(databaseConnectionString))
using (SqlCommand cmd_Phone = new SqlCommand(sql_Phone, cn2))
{
    cmd_Phone.Parameters.Add("@EmpID", SqlDbType.Int);
    cn2.Open();

    while (dr_SignUp.Read())
    {
        List<string> arrPhone = new List<string>();
        cmd_Phone.Parameters[0].Value = dr_SignUp["Emp_ID"];

        using (SqlDataReader dr_Phone = cmd_Phone.ExecuteReader())
        {
            while (dr_Phone.Read())
            {
                arrPhone.Add(dr_Phone["Phone_Number"].ToString());
            }
        }

Also, looking at your code I suspect what you really need to do is re-write your sql. You can combine all those into a single query that you just bind directly to the grid.


Sure:

public void SignUpControllerDay()
{
    using (var conn = new SqlConnection(ConnectionString))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = "SELECT ...";
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                var phone = reader["Phone_Number"].ToString();
                Bar(phone);
            }
        }
    }
}

public void Bar(string phone)
{
    using (var conn = new SqlConnection(ConnectionString))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = "SELECT ..."; // use phone to prepare statement
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                // Fill the grid
            }
        }
    }
}

Tags:

C#

Sql Server