Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

1 solution

initially you do not have a selectedDate. When loading the page set the SelectedDate to DateTime.Now;
 
Share this answer
 
Comments
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
Herman<T>.Instance 26-Aug-11 10:38am    
did you debug to see which line ave the error?
Herman<T>.Instance 26-Aug-11 10:40am    
did you rename any namespace?
Herman<T>.Instance 26-Aug-11 10:40am    
some solutions here: http://social.msdn.microsoft.com/Forums/pl-PL/vstsprofiler/thread/8bd8da75-76d2-4257-aeee-d040e6167bf9
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!

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