Gridview仅显示第一条记录

时间:2015-02-05 20:01:29

标签: c# asp.net gridview drop-down-menu datagridview

在我的网页中,我能够成功检索所有记录。我正在使用下拉列表根据不同的“状态”过滤记录。但是,它只显示第一个检索到的记录。没有例外或错误。当我在我的数据库中运行查询(以测试我的sql语句)时,有3个检索到的记录。我仍然无法确定我的代码中出了什么问题。

我正在开发一个三层Web应用程序。

以下是我的表示层代码,

 protected void ddl_status_SelectedIndexChanged(object sender, EventArgs e)
    {
        string status = ddl_status.SelectedItem.ToString();

            //Default
            if (status == "Show All Submissions")
            {
                gv_combinedEntries.Visible = true;
                SubmissionBLL sBLL = new SubmissionBLL();

                //Call getAllStudent() method in StudentBLL
                List<Submission> subAll = new List<Submission>();

                subAll = sBLL.getAllSub();

                gv_combinedEntries.DataSource = subAll;
                gv_combinedEntries.DataBind();
            }

            else 
            {
                SubmissionBLL subStatus = new SubmissionBLL();
                List<Submission> subList = new List<Submission>();
                subList = subStatus.getStatusList(status);
                gv_combinedEntries.DataSource = subList;
                gv_combinedEntries.DataBind();
            }
    }

业务逻辑层:

public List<Submission> getStatusList(string statusPick)
{
        List<Submission> specificStatus = new List<Submission>();
        Submission sub2 = new Submission();
        specificStatus = sub2.getStatusNewList(statusPick);
        return specificStatus;
 }

数据访问层:

 public List<Submission> getStatusNewList(string statusSelected)
    {
        List<Submission> subStatus = new List<Submission>();
        string Id, School, TeamName, submissionDate, status;

        string queryStr = "SELECT s.Id,c.School,c.TeamName,s.submissionDate,
        c.submissionStatus FROM TeamSubmission s INNER JOIN Competition c
        ON c.teamUsername = s.username WHERE submissionStatus = @enterStatus";

        SqlConnection conn = new SqlConnection(_connStr);
        SqlCommand cmd = new SqlCommand(queryStr, conn);

        cmd.Parameters.AddWithValue("@enterStatus", statusSelected);

        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            Id = dr["Id"].ToString();
            School = dr["School"].ToString();
            TeamName = dr["TeamName"].ToString();
            submissionDate = dr["submissionDate"].ToString();
            status = dr["submissionStatus"].ToString();

            subStatus.Add(new Submission(Id, School, TeamName, submissionDate, status));
        }

        conn.Close();
        dr.Close();
        dr.Dispose();

        return subStatus;
    }

1 个答案:

答案 0 :(得分:1)

您需要将 if(dr.Read())语句更改为while(dr.Read())

 while (dr.Read())
        {
            Id = dr["Id"].ToString();
            School = dr["School"].ToString();
            TeamName = dr["TeamName"].ToString();
            submissionDate = dr["submissionDate"].ToString();
            status = dr["submissionStatus"].ToString();

            subStatus.Add(new Submission(Id, School, TeamName, submissionDate, status));
        }