469,913 Members | 2,107 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

parameter date conversion

maheshwag
my code is:

Expand|Select|Wrap|Line Numbers
  1.  
  2. string sql="insert into dummy(date)values(@date)
  3.  
  4. sqlcommand cmd=new sqlcommand(sql,con)
  5. cmd.parameters.Add("@date", SqlDbtype.datetime).values=maskedtextbox1.text;
  6. cmd.ExecuteNonquery();
  7.  
it's throw varchar to date conversion error how to solve it.

my date format is "DD/MM/YYYY"
Sep 2 '10 #1

✓ answered by hype261

I am not big on localization for SQL Server, but I believe there is a way you can change your SQL Server to natively handle dates the way you want to.

You may want to look at this page.

http://msdn.microsoft.com/en-us/libr...81(VS.85).aspx

Or this one

http://msdn.microsoft.com/en-us/libr...(SQL.100).aspx

4 1903
hype261
207 100+
I believe you are getting an error because you are .text from the maskedtextbox1 is returning a string instead of a date. To convert it you should be able to cast it to a date using the following code example.

Expand|Select|Wrap|Line Numbers
  1. DateTime convertedDate = DateTime.Parse(dateString);
  2.  
Sep 2 '10 #2
that will convert to the date but sir what about format like DD/MM/YYYY. yeah! here user should type date in dd/mm/yyyy format and sql store date in mm/dd/yyyy format that's the problem how to overcome with it. in paramerized query as per my first post.
Sep 3 '10 #3
hype261
207 100+
I am not big on localization for SQL Server, but I believe there is a way you can change your SQL Server to natively handle dates the way you want to.

You may want to look at this page.

http://msdn.microsoft.com/en-us/libr...81(VS.85).aspx

Or this one

http://msdn.microsoft.com/en-us/libr...(SQL.100).aspx
Sep 3 '10 #4
Plater
7,872 Expert 4TB
DateTime fields are NOT stored as strings in SqlServer. How you want to view them, does not matter to it.
You can use DateTime.TryParseExact() to supply the input format of "dd/MM/yyyy" and populate a DateTime object that you can supply to the sql parameter.
Sep 3 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by nimish | last post: by
5 posts views Thread by SalimShahzad | last post: by
4 posts views Thread by Vishal | last post: by
4 posts views Thread by dhnriverside | last post: by
1 post views Thread by Franck | last post: by
2 posts views Thread by p.smachylo | last post: by
2 posts views Thread by RN1 | last post: by
2 posts views Thread by anderst | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.