Click here to Skip to main content
16,012,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How to convert textbox value to date datatype.anbody help me.in below there is my code
cm.Parameters.Add("@fromdate", SqlDbType.Date) = Convert.ToDateTime(txtfromdate.Text);
it shows me error that" You can't forcefully change the date to datetime".How how will i convert it..

Thnaks
Posted
Updated 24-Jul-11 23:17pm
v3
Comments
shefeekcm 25-Jul-11 5:13am    
did you get the answer?
Starlene 25-Jul-11 5:26am    
no
[no name] 25-Jul-11 5:21am    
AFAIK, .NET does not give this specific error message: "u cant forcefully change the date to datetime". If you want people to help you, give us the precise error message that you got.

It seems you missed to add Value on your code. Try this.

cm.Parameters.Add("@fromdate", SqlDbType.Date).Value = Convert.ToDateTime(txtfromdate.Text);
 
Share this answer
 
Comments
Abhinav S 25-Jul-11 5:27am    
The OP seems to have marked this as answered.
However, I felt this would not be the cause of the error.

The Value property is taken by default right?
walterhevedeich 25-Jul-11 5:33am    
I agree this was not the real cause of the error. I just pointed out what he needs to add but it seems will still get the error that he gave a while ago, since the type I gave is still SqlDbType.Date. I was assuming he was getting multiple errors. Perhaps a combination of our answers might solve his problem? :)
Abhinav S 25-Jul-11 8:11am    
Yes. Helping the OP solve errors is the final motive so hope these solutions helped him in the end.
first fix datetime format of your textbox.
Then use this
System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
dateInfo.ShortDatePattern = "dd/MM/yyyy";//This may depend on your format
cm.Parameters.Add("@fromdate", SqlDbType.DateTime).Value = Convert.ToDateTime(txtfromdate.Text, dateInfo);
 
Share this answer
 
v4
Comments
Starlene 25-Jul-11 5:21am    
no yaar .it is not working.thanks for help..
walterhevedeich 25-Jul-11 5:34am    
Can you give the exact error message instead of just telling its not working. The code makes sense from our point of view.
walterhevedeich 25-Jul-11 5:22am    
Makes sense as well. Countered.
You need to use SqlDbType.DateTime as your parameter if you are going to pass a DateTime value.
cm.Parameters.Add("@fromdate", SqlDbType.DateTime)

The second way to fix this could be to try Convert.ToDateTime(txtfromdate.Text).Date.
Just pass in the Date value instead of DateTime.
 
Share this answer
 
v2
Comments
walterhevedeich 25-Jul-11 5:21am    
Makes sense. Countered.
Abhinav S 25-Jul-11 5:23am    
Thanks. A heavy weight voter downvoted me. I wish they had at least left a valid reason. Thanks for the counter.
walterhevedeich 25-Jul-11 5:35am    
I can see that. Too bad my vote wasn't able to compensate.

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