Click here to Skip to main content
Rate this: bad
good
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.
 
using(connString)
{
conn.Open();
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));
command.ExecuteNonQuery();
 
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
good
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;
  Permalink  
Comments
DanHodgson88 at 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 at 26-Aug-11 10:38am
   
did you debug to see which line ave the error?
digimanus at 26-Aug-11 10:40am
   
did you rename any namespace?
digimanus at 26-Aug-11 10:40am
   
some solutions here: http://social.msdn.microsoft.com/Forums/pl-PL/vstsprofiler/thread/8bd8da75-76d2-4257-aeee-d040e6167bf9
DanHodgson88 at 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 at 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
0 Sergey Alexandrovich Kryukov 535
1 CPallini 450
2 OriginalGriff 270
3 George Jonsson 256
4 Suvabrata Roy 229
0 OriginalGriff 4,873
1 CPallini 3,900
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,646
4 Gihan Liyanage 2,236


Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 26 Aug 2011
Copyright © CodeProject, 1999-2014
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