Search code examples
c#asp.netdatareader

asp.net DataReader never close


if manage_score return true ,the function Update is called. At this line in Update(System.Data.SqlClient.SqlDataReader oReader3 = cmd2.ExecuteReader(). I get this error ( There is already an open DataReader associated with this Command which must be closed first.) I have removed some lines to make the code less long to read. Manage_score: Insert a new entry if not already there. Update: Is called if the entry is already there, and It do some calculations before Updating.

Thank you

public static bool manage_score(String City, String Street, String No, int Clean, int Fun, int Study, bool recommend)
{
    System.Data.SqlClient.SqlConnection conn=null;
    System.Data.SqlClient.SqlCommand cmd = null; ;
    System.Data.SqlClient.SqlDataReader rdr=null;
    bool needUpdate=false;

    try
    {
        using( conn = new System.Data.SqlClient.SqlConnection(dbConn))
        { 

             using(cmd = new System.Data.SqlClient.SqlCommand(
             "Insert Into c_Show (Street,City,No,Count,Clean,Fun,Study,CleanScore,FunScore,StudyScore,Like_,Dislike) " +
             "VALUES (@Street,@City,@No,'1',@Clean,@Fun,@Study,@Clean,@Fun,@Study,@Like,@DisLike)", conn))
             { 
                cmd.Parameters.Add("@No", System.Data.SqlDbType.Int).Value = No;
                cmd.Parameters.Add("@City", System.Data.SqlDbType.Char).Value = City;

                cmd.Connection.Open();
                using (rdr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)) { }
            }
        }
    }
    catch (System.Data.SqlClient.SqlException ex)
    {
        if (ex.Number == 2627)// duplicate key
        {
            needUpdate=true;
        }
    }
    catch (System.Exception ex) { }

    return needUpdate;
}

static public void update(String City, String Street, String No, int Clean, int Fun, int Study, bool recommend)
{
    using (System.Data.SqlClient.SqlConnection conn2 = new System.Data.SqlClient.SqlConnection(dbConn))
    {
        String resCount = null;
        String resClean = null;
        String resFun = null;
        String resStudy = null;
        String resLike = null;
        String resDislike = null;

        using (System.Data.SqlClient.SqlCommand cmd2 = new System.Data.SqlClient.SqlCommand(
        "SELECT Street,City,No,Count,CleanScore,FunScore,StudyScore,Like_,Dislike FROM c_Show " +
        "Where City=@City and Street=@Street and No=@No ", conn2))
        {
            cmd2.Parameters.Add("@No", System.Data.SqlDbType.Int).Value = No;
            cmd2.Parameters.Add("@City", System.Data.SqlDbType.Char).Value = City;
            cmd2.Parameters.Add("@Street", System.Data.SqlDbType.NVarChar).Value = Street;
            cmd2.Connection.Open();
            cmd2.ExecuteReader();
            using (System.Data.SqlClient.SqlDataReader oReader3 = cmd2.ExecuteReader())
            {}
        }
    }
}

Solution

  • According to your code, you have mistakenly called cmd2.ExecuteReader() twice.

            cmd2.ExecuteReader(); // once.  You probably want to remove this one?
            using (System.Data.SqlClient.SqlDataReader oReader3 = cmd2.ExecuteReader()) // twice
            {
    

    I'm thinking that first call is a mistake. Remove it and you should be fine.