This code works:
sqlCmd = New SqlCommand("INSERT INTO tableName" _
+ " (colDate, colCount)" _
+ " VALUES ('1/9/2010
12:00:00 AM', 1)" _
, connSQL)
sqlCmd.ExecuteNonQuery()
While this does not:
sqlCmd = New SqlCommand("INSERT INTO tableName" _
+ " (colDate, colCount)" _
+ " VALUES ('@Date', 1)" _
, connSQL)
sqlCmd.Parameters.Clear()
sqlCmd.Parameters.AddWithValue("@Date", "1/9/2010 12:00:00
AM")
sqlCmd.ExecuteNonQuery()
What am I overlooking?
VB.NET & VS2005
On Jan 12, 2:35 am, vinay kumar <namavinayku...@gmail.com> wrote:
> rather than mentioning date u can use getdate() function sqlserver
>
sqlCmd.Parameters.AddWithValue("@Date", "1/9/2010 12:00:00
AM")
Adds your parameter as a type string (which gets converted to a varchar by SQL)
Try changing it to
sqlCmd.Parameters.AddWithValue("@Date", New DateTime(1,9,2010))
Or something similar.
On Jan 13, 5:36 am, Jamie Fraser <jamie.fra...@gmail.com> wrote:
> This line
>
> sqlCmd.Parameters.AddWithValue("@Date", "1/9/2010 12:00:00
> AM")
>
> Adds your parameter as a type string (which gets converted to a varchar by SQL)
>
> Try changing it to
>
> sqlCmd.Parameters.AddWithValue("@Date", New DateTime(1,9,2010))
>
> Or something similar.
>
Try using the .ToShortDateString() method provided via the DateTime
object. For example, if you want to add by value, use
DateTime.Now.ToShortDateString() and see if the current time is
inserted as you expected. If so and you need to insert other date
values, you'll need to establish new DateTime objects and set
everything accordingly before inserting the .ToShortDateString()
value.
I am not currently at a machine to test this, so please let us know if
you find it successful (or if you don't!)
Alan
Thanks everyone for your help!