Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a method called GetSkillsProject
This method returns a list of skills
This method has the following code:

C#
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;
        }

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?
Posted

1 solution

Try this:

C#
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;
        }
 
Share this answer
 
Comments
Sanjay K. Gupta 12-Nov-12 5:13am    
+5 , I think it will work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900