Click here to Skip to main content
12,549,720 members (48,407 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# WPF
Hi all, having bit of a problem with reading data from a db into a listview the code I have below doesn't work and I have no idea why any ideas would be appreciate!
I don't get an error on compile just when I change the date.

SqlCommand command = new SqlCommand("SELECT FoodName,Calories,Quantity,fat,Protein,Carbs FROM [User_Food] WHERE Date = @Date",conn);
command.Parameters.Add(new SqlParameter("@Date, datePicker1.SelectedDate));
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
FoodCollection.Add(new FoodData
FoodName = dr.GetString(0),
Calories = dr.GetInt16(1),
Quantity = dr.GetInt16(2),
FatContent = dr.GetDouble(3),
ProteinContent = dr.GetDouble(4),
CarbsContent = dr.GetDouble(5)

Any help would be appreciated very much!
Posted 26-Aug-11 3:35am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

initially you do not have a selectedDate. When loading the page set the SelectedDate to DateTime.Now;
DanHodgson88 26-Aug-11 10:29am
hey mate sorry slow reply, Just tried it I did need it but throws a dif error now:

"Exception had been thrown by the target of an invocation"

and now this error comes up on build so I know theres a problem somewhere in the laoding of the db data
digimanus 26-Aug-11 10:38am
did you debug to see which line ave the error?
digimanus 26-Aug-11 10:40am
did you rename any namespace?
digimanus 26-Aug-11 10:40am
some solutions here:
DanHodgson88 27-Aug-11 11:21am
thanks mate I will have a read through - Appreciate your time again mate seem to be my saviour atm!
digimanus 27-Aug-11 15:22pm
has to do with teh fact you do a lot with databases and .NET, just like me.

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.161021.1 | Last Updated 26 Aug 2011
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