Gridview 只显示第一条记录
Gridview displays first record only
在我的网页中,我能够成功检索所有记录。我正在使用下拉列表根据不同的 "status" 过滤记录。但是,它只显示第一个检索到的 record.There 没有异常或错误。当我 运行 我的数据库中的查询(测试我的 sql 语句)时,有 3 条检索记录。我仍然无法确定我的代码出了什么问题。
我正在开发一个三层网络应用程序。
这是我的表示层代码,
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;
}
您需要将 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));
}
在我的网页中,我能够成功检索所有记录。我正在使用下拉列表根据不同的 "status" 过滤记录。但是,它只显示第一个检索到的 record.There 没有异常或错误。当我 运行 我的数据库中的查询(测试我的 sql 语句)时,有 3 条检索记录。我仍然无法确定我的代码出了什么问题。
我正在开发一个三层网络应用程序。
这是我的表示层代码,
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;
}
您需要将 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));
}