简体   繁体   中英

Repeated values in list in while(reader.Read()) {}

[WebMethod]
public List<reports> getMyReports( int user_id )
{
    string cs = ConfigurationManager.ConnectionStrings["ReportDB"].ConnectionString;
    using (SqlConnection con = new SqlConnection(cs))
    {
        SqlCommand cmd = new SqlCommand("getAllReportsByUserID", con);
        cmd.CommandType = CommandType.StoredProcedure;
        List<reports> repers = new List<reports>();
        //users[][] liser = new users[][];
        SqlParameter user_id_parameter = new SqlParameter("@user_id", user_id);
        cmd.Parameters.Add(user_id_parameter);
        reports report = new reports();
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            report.id = Convert.ToInt32(reader["id"]);
            report.title = reader["title"].ToString();
            report.description = reader["description"].ToString();
            report.anonymous = (bool)reader["anonymous"];
            report.location = reader["location"].ToString();
            report.status = reader["status"].ToString();
            report.category = reader["category"].ToString();
            report.date = (DateTime)reader["date"];
            report.picture_url = reader["picture_url"].ToString();
            report.admin_id = Convert.ToInt32(reader["admin_id"]);
            repers.Add(report);
        }
        return repers;
    }
}

I have the top function that calls the following stored procedure:

CREATE Proc [dbo].[getAllReportsByUserID]
@user_id int
as
Begin
  Select 
    id, 
    title, 
    description, 
    anonymous, 
    location, 
    status, 
    category, 
    date, 
    picture_url, 
    admin_id 
  from reports 
  where user_id = @user_id
End

I have tested the procedure individually and it works fine. Yet, when I test the WebService created above I get a list with the last value duplicated along the whole list.

Can someone please help me figure out why do I get the same (last)value repeated over and over again?

By creating the report object before the loop and reusing it repeatedly, you insert a reference to that same object multiple times in your list.

You should create the report object inside your loop:

        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            reports report = new reports();
            report.id = Convert.ToInt32(reader["id"]);
            report.title = reader["title"].ToString();
            report.description = reader["description"].ToString();
            report.anonymous = (bool)reader["anonymous"];
            report.location = reader["location"].ToString();
            report.status = reader["status"].ToString();
            report.category = reader["category"].ToString();
            report.date = (DateTime)reader["date"];
            report.picture_url = reader["picture_url"].ToString();
            report.admin_id = Convert.ToInt32(reader["admin_id"]);
            repers.Add(report);
        }
        return repers;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM