Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi,
I am getting an annoying error while inserting into database. below is the query string. I get error on ExcecuteNOnQUery line. error is "Operand type clash: int is incompatible with date" below is the insert string.
 
Here User table has 4 columns. Date has date datatype and remaining are varchar(50)
 
sqlstring = "Insert Into User(Date,Phone_no,NameF,New_User) Values (" + Today.ToString("yy-MM-dd") + ",'" + txtPhoneno.Text + "','" + NameF.Text + "','" + N_User + "') "
 

Could anybody tell, what is happening wrong here.
Posted 11-Nov-12 10:07am
Edited 11-Nov-12 10:08am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

What is today ? Is it a local variable defined? If you are trying to use DateTime then I believe it should be DateTime.Today.
 
Besides, this please note few points below.
 
Please notice that DateTime.Today will give only date part. If you need to store time as well then you should should DateTime.Now
 
Also, while inserting be aware of the datetime format of the sql.
 
And last point and the most important point: Rather than using direct inputs from the textbox, it is better to use parameterized query. As using direct textbox input in the query makes your system vulnerable to SQL Injection attack.
 
Hope that helps
Milind
  Permalink  
Comments
Xperment2008 at 12-Nov-12 10:49am
   
Hi,
I tried DateTime.Today but still I get same error. I am trying to input current date into table.
Milind Thakkar at 12-Nov-12 12:56pm
   
What is the data type of "Date column?

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

  Print Answers RSS
0 mhegazy94 460
1 Sergey Alexandrovich Kryukov 405
2 Kornfeld Eliyahu Peter 275
3 Gihan Liyanage 163
4 Sibeesh KV 156
0 Sergey Alexandrovich Kryukov 6,775
1 OriginalGriff 6,696
2 CPallini 5,345
3 George Jonsson 3,599
4 Gihan Liyanage 2,751


Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 12 Nov 2012
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