每当我通过ObjectDataSource连接到Datalist时,只会重复数据库的最后一条记录,而不会显示其他记录. [英] whenever I connect to Datalist through ObjectDataSource only the last record of the database is repeated and the other record does not show up.
本文介绍了每当我通过ObjectDataSource连接到Datalist时,只会重复数据库的最后一条记录,而不会显示其他记录.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个名为GetSkillsProject的方法
此方法返回技能列表
此方法具有以下代码:
I have created a method called GetSkillsProject
This method returns a list of skills
This method has the following code:
public List<Model.Skill> GetSkillsOfProject(int ProjectId)
{
List<Model.Skill> skllist = new List<Model.Skill>();
Model.Skill skl = new Model.Skill();
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("GetSkillsOfProject", con);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
sda.SelectCommand.Parameters.AddWithValue("@ProjectId", ProjectId);
sda.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
skl.Name = dt.Rows[i]["Name"].ToString();
skllist.Add(skl);
}
return skllist;
}
每当我通过 ObjectDataSource 连接到 Datalist 时,只会重复数据库的最后一条记录,而其他记录则不会显示.
* Sql代码写得很好
有人可以帮忙吗?
whenever I connect to Datalist through ObjectDataSource only the last record of the database is repeated and the other record does not show up.
* Sql codes are well-written
can anyone help?
推荐答案
尝试一下:
Try this:
public List<Model.Skill> GetSkillsOfProject(int ProjectId)
{
List<Model.Skill> skllist = new List<Model.Skill>();
// Move the following line to inside the for loop: (see for loop)
//Model.Skill skl = new Model.Skill();
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("GetSkillsOfProject", con);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
sda.SelectCommand.Parameters.AddWithValue("@ProjectId", ProjectId);
sda.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
Model.Skill skl = new Model.Skill(); //<-- Instantiate Model object here.
skl.Name = dt.Rows[i]["Name"].ToString();
skllist.Add(skl);
}
return skllist;
}
这篇关于每当我通过ObjectDataSource连接到Datalist时,只会重复数据库的最后一条记录,而不会显示其他记录.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文