Click here to Skip to main content
15,907,000 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Evening all,

I have been having a bit of an issue with binding data to a listview. I have spent a fair amount of time trying to bind data from a database to a listview and I have little success.

This is the code I have so far:

C#
private void SetListView()
   {
       this.ListView1.Items.Clear();

       using (SqlCommand cmd = new SqlCommand("SELECT MealType.MealType, Food.FoodName, Food.Calories FROM Day INNER JOIN Diary ON Day.DiaryID = Diary.DiaryID INNER JOIN Meal ON Day.DayID = Meal.DayID INNER JOIN MealFood ON Meal.MealID = MealFood.MealID INNER JOIN Food ON MealFood.FoodID = Food.FoodID INNER JOIN MealType ON Meal.TypeID = MealType.TypeID INNER JOIN Users ON Diary.UserID = Users.UserID WHERE (Users.UserName = @username) AND MealType.MealType = @MealType", conn))
       {
           cmd.Parameters.Add("@username", SqlDbType.VarChar);
           cmd.Parameters["@username"].Value= HttpContext.Current.Session["Username"].ToString();
           cmd.Parameters.Add("@MealType", SqlDbType.VarChar);
           cmd.Parameters["@MealType"].Value = "Breakfast";

           DataSet ds = new DataSet();
           SqlDataAdapter da = new SqlDataAdapter();

           da.SelectCommand = cmd;
           da.Fill(ds);

           if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
           {

               foreach (DataRow dr in ds.Tables[0].Rows)
               {
                   string FoodName = dr["FoodName"].ToString();
                   string MealType = dr["MealType"].ToString();
                   int Calories = Convert.ToInt32(dr["Calories"].ToString());

                   ListViewItem FoodItems = new ListViewItem();

                   FoodItems.SubItems.Add(FoodName);
                   FoodItems.SubItems.Add(MealType);
                   FoodItems.SubItems.Add(Calories);
                   this.ListView1.Items.Add(FoodItems);
               }
           }
       }
   }


To my eyes this should work but I have obviously gone wrong somewhere. The main error I am getting at the minute is that ListViewItem() is not an acceptable constructor, I checked MSDN and according to the website the default constructor should be fine.

I am just a bit confused where I have gone wrong. If anyone could give me a hand and point out my shortfall I would greatly appreciate it.

Thank you.

Dan
Posted

1 solution

 
Share this answer
 
Comments
DanHodgson88 28-Oct-12 6:54am    
Thank you for the reply Kuthuparakkal, I have already used the top link and that doesn't seem to have the answers I am looking for. I will read through the second and see if I can gauge where I have gone wrong.

Thank you again for the reply.
DanHodgson88 31-Oct-12 17:05pm    
Thank you for the two links the second helped me out in a big way and I have no completed it :)
Kuthuparakkal 31-Oct-12 23:07pm    
you're welcome!

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