Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF DateTime Sqlite
I want to reading a datetime value from a Sqlite database and assign it to datepicker control. Here is my code that i am trying:
 
try
{
sqlitecon.Open();
 
string Query = "Select * from Customer_New where Cust_Id='" + val + "' ";
 
SQLiteCommand createCommand = new SQLiteCommand(Query, sqlitecon);
// createCommand.ExecuteNonQuery();
SQLiteDataReader dr = createCommand.ExecuteReader();
while(dr.Read()){
if(!dr.IsDBNull(1))
date_open.DisplayDate = dr.GetDateTime(1);
if(!dr.IsDBNull(2))
Date_Joining.DisplayDate = dr.GetDateTime(2);
if(!dr.IsDBNull(3))
txt_Title.Text = dr.GetString(3);
if(!dr.IsDBNull(4))
txt_cardnum.Text =dr.GetString(4);
}
sqlitecon.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
 
The above code is executed on form load event .so when form loads it should load the date value from db table into date picker .but form is opening with Message box that shows exception "specified cast is not valid". Then it fails to load any value from db and if i remove date fields from assigning in while loop then rest of text fields are assigned correctly to form . My db foramt of date is :MM/DD/YYYY and is of type datetime . On debugging by putting break point at "date_open.DisplayDate = dr.GetDateTime(1);" it shows system date and time .My db tables are following sequence: Cust_id | date_open | Date_Joining |..... .Please Help
Posted 9-Oct-13 21:28pm
Comments
Pheonyx at 10-Oct-13 3:43am
   
if you step through your code and before either of the dr.GetDateTime lines execute, view the dr.GetDateTime(1) or dr.GetDateTime(2) in the watch window, what values do you get?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 310
1 Sergey Alexandrovich Kryukov 166
2 PhilLenoir 164
3 Richard MacCutchan 160
4 Sharmanuj 146
0 Sergey Alexandrovich Kryukov 6,081
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 10 Oct 2013
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