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();
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!