Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

i am using datepicker in my windows program in C#. I inserted date from datetimepicker to sqlserver but unfortunately I can not retrieve date from database.it is throwing error.
please any one help me.how to select date in datepicker from database.


here is my piece of code..

C#
dtRecDate.Value = Convert.ToDateTime(dtDocHeaderDetails.Rows[0].ItemArray[13]);
Posted
Updated 1-May-14 2:54am
v2
Comments
[no name] 1-May-14 7:32am    
Why would you show us a snippet of code that has nothing to do with your problem? If your problem is getting data from a database, don't you think that you should be showing us that code? And tell us what "throwing error" means?
Thanks7872 1-May-14 7:49am    
Calm down. OP has posted the relevant code only. He is trying to set date which he got from database into date picker.
[no name] 1-May-14 7:57am    
Calm down? In what way am I not calm? No is he not showing the relevant code. There is nothing at all in this posting about any database or a description of the error.
Bitto kumar 1-May-14 8:56am    
what you said right rohan leuva sir, my requirement is that only..
Sanket Saxena 1-May-14 7:39am    
what error your are getting and where??

We would need to know the error to know for sure what is happening but you should be able to solve this by debugging it. Put a breakpoint on that line of code and see what the values are.

Also, I would suggest you not reference your columns using an index because it depends on your sql not changing. Instead do something like this:
C#
dtDocHeaderDetails.Rows[0]["fieldName"]
 
Share this answer
 
Comments
Bitto kumar 1-May-14 8:56am    
String was not recognized as a valid DateTime.
ZurdoDev 1-May-14 9:05am    
Right. So put a breakpoint and see what the value is. Chances are it is null or blank. So you may want to use Decimal.TryParse instead.
What is the datatype of your database column where you are storing your values? While storing or reading the date you can convert it in a format that you require refer following link.

http://msdn.microsoft.com/en-us/library/ms187928%28SQL.90%29.aspx[^]


Syntax for CAST:
CAST ( expression AS data_type [ (length ) ])

Syntax for CONVERT:
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

103 is used for British/French format like dd/mm/yy.

There are lot many other formats. Please refer above link for better understanding on using date time in sql.
 
Share this answer
 
I'd suggest to check if field is equal to DBNull.Value[^].
For example:
C#
DateTime myDate = DateTime.Today();
if (DBNull.Value.Equals(dtDocHeaderDetails.Rows[0].ItemArray[13]))
    dtRecDate.Value = myDate;
else
    dtRecDate.Value = dtDocHeaderDetails.Rows[0].ItemArray[13];
 
Share this answer
 

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