Question : close datareader connection

I am using some data readers on my website and for some reason when i log in there are 2 sql threads that stay open and i think its becuase of these data readers that i have.  can someone tell me if i am handling these right?

    public static SqlDataReader GetUserInformation(string userName)
    {
        SqlConnection con = new SqlConnection(GetConnectionString());
        SqlCommand cmd = new SqlCommand("sp", con);

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@UserName", userName);

        con.Open();

        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        return dr;
 
       
    }



    public static void LoadUserInformation(string userName)
    {
        SqlDataReader dr = GetUserInformation(userName);

        if (dr.Read())
        {
            UserID = Convert.ToInt32(dr["UserID"].ToString());
            FacilityID = Convert.ToInt32(dr["FacilityID"].ToString());
            Admin = Convert.ToBoolean(dr["Admin"].ToString());
            Email = dr["Email"].ToString();
       
        }

        dr.Close();


    }

Answer : close datareader connection

You cannot close the connection from the datareader perspective. The connection needs to close in the same place scope where it was opened.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
    public static void LoadUserInformation(string userName)
    {
        SqlConnection con = new SqlConnection(GetConnectionString());
        SqlCommand cmd = new SqlCommand("sp", con);

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@UserName", userName);

        con.Open();

        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        if (dr.Read())
        {
            UserID = Convert.ToInt32(dr["UserID"].ToString());
            FacilityID = Convert.ToInt32(dr["FacilityID"].ToString());
            Admin = Convert.ToBoolean(dr["Admin"].ToString());
            Email = dr["Email"].ToString();
        
        }

        dr.Close();

        con.Close();
    }
Random Solutions  
 
programming4us programming4us