Click here to Skip to main content
12,243,182 members (22,117 online)
Rate this:
 
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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 12 Nov 2012
Copyright © CodeProject, 1999-2016
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