SqlCommand INSERT into smalldatetime column

1,583 views
Skip to first unread message

jtaylor

unread,
Jan 11, 2010, 3:56:13 PM1/11/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I'm trying to insert a row into a SQL Server table but I keep getting
a "Conversion failed when converting character string to smalldatetime
data type." error.

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

vinay kumar

unread,
Jan 12, 2010, 3:35:12 AM1/12/10
to dotnetde...@googlegroups.com
rather than mentioning date u can use getdate() function sqlserver

jtaylor

unread,
Jan 12, 2010, 9:30:34 AM1/12/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Won't the getdate() function return a full timestamp? I need the time
portion to always be midnight.

On Jan 12, 2:35 am, vinay kumar <namavinayku...@gmail.com> wrote:
> rather than mentioning date u can use getdate() function sqlserver
>

Jamie Fraser

unread,
Jan 13, 2010, 6:36:03 AM1/13/10
to dotnetde...@googlegroups.com
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.

vinay kumar

unread,
Jan 13, 2010, 6:52:50 AM1/13/10
to dotnetde...@googlegroups.com
and also u can use convert function of sqlserver
 it consists of 3 paramaters 

jtaylor

unread,
Jan 14, 2010, 10:46:47 AM1/14/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I tried this

sqlCmd.Parameters.AddWithValue("@Date", New DateTime(1,9,2010))
but it gave me a "Year, Month, and Day parameters describe an un-
representable DateTime". I changed it to be Year, Month, Day, and
then I was back to the conversion error on the .ExecuteNonQuery().

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.
>

Anachronistic

unread,
Jan 14, 2010, 12:44:02 PM1/14/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
jtaylor,

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

http://www.twitter.com/anachronistic

Arsalan Tamiz

unread,
Jan 16, 2010, 3:59:06 AM1/16/10
to dotnetde...@googlegroups.com
sqlCmd = New SqlCommand("INSERT INTO tableName" _
                                            + " (colDate, colCount)" _
                                            + " VALUES ('@Date', 1)" _
                                    , connSQL)

and this line you put @Date in single quotes is this right?

Hozefa Unwala

unread,
Jan 15, 2010, 3:22:37 PM1/15/10
to dotnetde...@googlegroups.com
Use convert.todatetime()
--
Hozefa Unwala

jtaylor

unread,
Jan 15, 2010, 12:55:09 PM1/15/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I'm afraid it still gave the conversion error.

akram mellice

unread,
Jan 17, 2010, 9:02:50 AM1/17/10
to dotnetde...@googlegroups.com
the line sqlCmd = New SqlCommand("INSERT INTO tableName" _

                                           + " (colDate, colCount)" _
                                           + " VALUES ('@Date', 1)" _
                                   , connSQL)
replace with sqlCmd = New SqlCommand("INSERT INTO tableName" _
                                           + " (colDate, colCount)" _
                                           + " VALUES (@Date, 1)" _
                                   , connSQL)
--
Akram Mellice

Jamie Fraser

unread,
Jan 18, 2010, 4:46:35 AM1/18/10
to dotnetde...@googlegroups.com
sqlCmd = New SqlCommand("INSERT INTO tableName (colDate,
colCount) VALUES (@Date)", connSQL)
sqlCmd.Parameters.Clear()
sqlCmd.Parameters.AddWithValue("@Date", New DateTime(2010, 1, 19))
sqlCmd.ExecuteNonQuery()

jtaylor

unread,
Jan 18, 2010, 3:43:24 PM1/18/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
The single quotes in SqlCommand were the problem. I dropped them and
it worked fine.

Thanks everyone for your help!

Reply all
Reply to author
Forward
0 new messages