Click here to Skip to main content
15,906,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am trying to commit listview data to a database now I thought we had found a solution in this:

try
           {
               SqlConnection conn = new SqlConnection(Calorie_Counter.Properties.Settings.Default.CalorieCounterUKConnectionString);
               conn.Open();

               foreach (var item in listView1.Items)
               {
                   var FoodName = item.SubItems[0].ToString(); /*PROBLEM LINE*/

                   using (SqlCommand command = new SqlCommand("INSERT INTO [User_Food] (FoodName,Calories,Quantity,Fat,Protein,Carbs,Date) VALUES (@FoodName,@Calories,@Quantity,@Fat,@Protein,@Carbs,@Date)", conn))
                   {
                       command.Parameters.AddWithValue("@FoodName", FoodName);
                       command.Parameters.Add(new SqlParameter("@Calories", Calories));
                       command.Parameters.Add(new SqlParameter("@Quantity", Quantity));
                       command.Parameters.Add(new SqlParameter("@Fat", FatContent));
                       command.Parameters.Add(new SqlParameter("@Protein", ProteinContent));
                       command.Parameters.Add(new SqlParameter("@Carbs", CarbsContent));
                       command.Parameters.Add(new SqlParameter("@Date", datePicker1.SelectedDate));
                       command.ExecuteNonQuery();
                   }
                   conn.Close();
               }
           }
           catch(Exception ex)
           {
               string message = ex.Message;
               MessageBox.Show(message);
           }
       }


For some reason item.SubItems[0] - doesn't want to know it says there is no definition for subitems and I knwo there Is!

If anyone could suggest asolution to thios it would be imensley appreciated!

Dan

  ObservableCollection<fooddata> FoodCollection = new ObservableCollection<fooddata>();
        ObservableCollection<fooddata> FoodCollection1 = new ObservableCollection<fooddata>();
        ObservableCollection<fooddata> FoodCollection2 = new ObservableCollection<fooddata>();
        ObservableCollection<fooddata> FoodCollection3 = new ObservableCollection<fooddata>();

        public MainWindow()
        {

            InitializeComponent();
        }

        public ObservableCollection<fooddata> _FoodCollection
        { get { return FoodCollection; } }
        public ObservableCollection<fooddata> _FoodCollection1
        { get { return FoodCollection1; } }
        public ObservableCollection<fooddata> _FoodCollection2
        { get { return FoodCollection2; } }
        public ObservableCollection<fooddata> _FoodCollection3
        { get { return FoodCollection3; } }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (lunchRdo.IsChecked == true)
            {
                FoodCollection.Add(new FoodData
                {
                    FoodName = foodNameTxt.Text,
                    Calories = Convert.ToInt16(caloriesNum.Value),
                    Quantity = Convert.ToInt16(quantityNum.Value),
                    FatContent = fatNum.Value,
                    ProteinContent = proteinNum.Value,
                    CarbsContent = carbsNum.Value
                });
            }
            else if (breakfastRdo.IsChecked == true)
            {
                FoodCollection1.Add(new FoodData
                {
                    FoodName = foodNameTxt.Text,
                    Calories = Convert.ToInt16(caloriesNum.Value),
                    Quantity = Convert.ToInt16(quantityNum.Value),
                    FatContent = fatNum.Value,
                    ProteinContent = proteinNum.Value,
                    CarbsContent = carbsNum.Value
                });
            }
            else if (dinnerRdo.IsChecked == true)
            {
                FoodCollection2.Add(new FoodData
                {
                    FoodName = foodNameTxt.Text,
                    Calories = Convert.ToInt16(caloriesNum.Value),
                    Quantity = Convert.ToInt16(quantityNum.Value),
                    FatContent = fatNum.Value,
                    ProteinContent = proteinNum.Value,
                    CarbsContent = carbsNum.Value
                });
            }
            else if (snackRdo.IsChecked == true)
            {
                FoodCollection3.Add(new FoodData
                {
                    FoodName = foodNameTxt.Text,
                    Calories = Convert.ToInt16(caloriesNum.Value),
                    Quantity = Convert.ToInt16(quantityNum.Value),
                    FatContent = fatNum.Value,
                    ProteinContent = proteinNum.Value,
                    CarbsContent = carbsNum.Value
                });
            }
        }

        public class FoodData
        {
            public string FoodName { get; set; }
            public int Calories { get; set; }
            public int Quantity { get; set; }
            public double FatContent { get; set; }
            public double ProteinContent { get; set; }
            public double CarbsContent { get; set; }
        }

</fooddata></fooddata></fooddata></fooddata></fooddata></fooddata></fooddata></fooddata></fooddata></fooddata></fooddata></fooddata>


Ignore any MVVM mistakes at the minute I just want to get it working then I will worry about that! The XAML has each column binded to the specific method i.e. FoodName etc.. and I have no idea what that <fooddata> crap is I can't get rid of it!
Posted
Updated 25-Aug-11 1:00am
v4
Comments
Prerak Patel 25-Aug-11 7:14am    
Check the updated answer. Posting a XAML would be more helpful, but it seems like it is bound to FoodData.

1 solution

Check this line, it has nothing after listView1.
command.Parameters.AddWithValue("@FoodName", listView1.);

Moreover, I would say, WPF doesn't have SubItems, only Items.
http://msdn.microsoft.com/en-us/library/system.windows.controls.listview.aspx[^]
http://stackoverflow.com/questions/4788925/why-does-it-incist-subitems-is-undefined-by-controls-listview[^]

[Edit]
http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items.aspx[^]
Items can be of any type. And digimanus rightly said that, if your item is not of type ListViewItem, there will be an error.

[Edit]
Try this
C#
var Food = (FoodData)item;

and pass parameters as Food.FoodName, Food.Calories etc.
 
Share this answer
 
v4
Comments
DanHodgson88 25-Aug-11 6:33am    
sorry apologies that was when I was messing around with the code and forgot to chnage it back!
Prerak Patel 25-Aug-11 6:39am    
There is nothing as SubItems in WPF. It has only Items. Updated the answer.
DanHodgson88 25-Aug-11 6:42am    
Right ok, so how do you get the value of a 'subitem' so I can place it into the database? This is becoming a pain!
Prerak Patel 25-Aug-11 6:53am    
How the items are added to listview?
DanHodgson88 25-Aug-11 6:55am    
I wil edit question to include the code

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