Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
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;
        }
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 8-Nov-12 12:12pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
        }
  Permalink  
Comments
Sanjay K. Gupta at 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)

  Print Answers RSS
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 12 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100