Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following classes.
C#
namespace SmartAdminMvc.Models
{
    [Table("vwFeedback")]
    public class Feedback
    {   [Key]
        public int ProjectID { get; set; }
        public int FeedbackNo { get; set; }
        public int CustomerPersonNo { get; set; }
        public int QuestionSeq { get; set; }
        public int QuestionID { get; set; }
        public int Applicable { get; set; }
        public string Answer { get; set; }
        public string AnswerText { get; set; }
        public string Question { get; set; }
        public string QuestionAlt1 { get; set; }
        public string QuestionAlt2 { get; set; }
        public string AnsType { get; set; }
        public string Option1 { get; set; }
        public string Option2 { get; set; }
        public string Option3 { get; set; }
        public string Option4 { get; set; }
        public string Option5 { get; set; }

    }
}


XML
namespace SmartAdminMvc.Models
{
    public class FeedbackContext : DbContext
    {
        public DbSet<Feedback> Feedbacks { get; set; }
    }
}


Following is my controller class
XML
namespace SmartAdminMvc.Controllers
{
    public class FeedbackController : Controller
    {
        // GET: Feedback
        public ActionResult Index()
        {
            FeedbackContext feedbckContxt = new FeedbackContext();
            List<Feedback> allFeedbcks = feedbckContxt.Feedbacks.ToList();
            
            try{
                foreach (var item in allFeedbcks)
                {
                  
                    System.Diagnostics.Debug.WriteLine("Answer   " + item.Answer + "   Question Id.    " + item.QuestionID + "   CustomerPersonNo   " + item.CustomerPersonNo);
                }
            } catch(Exception exp){

                Response.Write("Error:" + exp.ToString());
            }

            return View(allFeedbcks);
        }
    }
}


Following is shown as the output

Answer 4 Question Id. 1009 CustomerPersonNo 10002
Answer 4 Question Id. 1009 CustomerPersonNo 10002
Answer 4 Question Id. 1009 CustomerPersonNo 10002
Answer 4 Question Id. 1009 CustomerPersonNo 10002
Answer 4 Question Id. 1009 CustomerPersonNo 10002
Answer 4 Question Id. 1009 CustomerPersonNo 10002
Answer 4 Question Id. 1009 CustomerPersonNo 10002
Answer 4 Question Id. 1009 CustomerPersonNo 10002
Answer 4 Question Id. 1009 CustomerPersonNo 10002
Answer 4 Question Id. 1009 CustomerPersonNo 10002

On the Output it prints only the first row values (repeatedly) from the table. But the number of rows fetched is correct.
I have been struggling with this issue for very long . It will be a great help if someone could help me out.

Thanks
Posted
Updated 25-Aug-15 19:57pm
v3
Comments
manognya kota 25-Aug-15 6:26am    
Hi,

did you check the value in allFeedbcks variable by debugging?
TrishaC 25-Aug-15 6:39am    
Yes, i did debug it. 'allFeedbcks' returns 10 rows of repeated data. My table has 10 rows of unique data. I am facing this problem with just this table. The same logic works fine for another table that i am trying to fetch.
xszaboj 25-Aug-15 12:02pm    
Are you sure you specified your table structure correctly? primary key etc?
Sinisa Hajnal 25-Aug-15 8:08am    
If your allFeedbacks already contains wrong data, dig into the fetching method and find the source of the problem. Maybe delete the model and re-create it?
TrishaC 25-Aug-15 12:25pm    
I found my mistake. I was defining just 1 key in the class whereas the DB table had a composite key.

[Key]
[Column(Order = 0)]
public int ProjectID { get; set; }
[Key]
[Column(Order = 1)]
public int FeedbackNo { get; set; }
[Key]
[Column(Order = 2)]
public int CustomerPersonNo { get; set; }
[Key]
[Column(Order = 3)]
public int QuestionSeq { get; set; }
public int QuestionID { get; set; }
public int Applicable { get; set; }

1 solution

Answered only to remove from unanswered queue: solved by the OP
 
Share this answer
 

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