As the topic suggests. I'm making a small program where you can browse and handle a bunch of data. The problem is that while inserting new rows into the SQL table works fine, using the same methods to update causes a problem with the smalldatetime variables.
I noticed using profiler that the exec command puts double quotes on the dates, but I can't seem to get rid of it. Plus the same double quote thing happens when inserting, and that works just fine. The command from the procedure looks like this:
exec spUpdateinfo @id=default, @job=N'Loadsoftext', @address=N'Loadsoftext', @startdate=''2009-02-01 00:00:00:000'', @enddate=''2009-05-15 00:00:00:000'', @other=N'Loadsoftext'
And what I get is this error:
Msg 102, Level 15, State 1, Line 5 Incorrect syntax near '2009'.
What I'm doing is using a date in the asp.net codebehind and passing that as a parameter as a normal date, assuming it's converted to smalldatetime in the SQL procedure. The methods and variables and strings etc used can be exactly the same as when inserting, but for some reason, UPDATING causes a problem. I've seen some PreserveSingleQuotes() function being used while googling around but that doesn't seem to exist in vb or SQL Server 2005.
Any idea what's causing this and how to get the update working normally?
Edit: Oh and the date is originally Finnish format being dd.MM.yyyy. But as said, all this works just fine and gets converted automatically while using INSERT.
Edit 2: Here's the whole code.
Asp.Net (vb)
Dim fdate As Date Dim ldate As Date fdate = CDate(BegindateTextBox.Text.Trim) ldate = CDate(EnddateTextBox.Text.Trim) Dim ID As New SqlParameter("@id", Request.QueryString("job_id")) Dim Job As New SqlParameter("@job", JobTextBox.Text) Dim Address As New SqlParameter("@address", AddressTextBox.Text) Dim Begindate As New SqlParameter("@startdate", fdate) Dim Enddate As New SqlParameter("@enddate", ldate) Dim Otherinfo As New SqlParameter("@other", OtherinfoTextBox.Text) Begindate.DbType = DbType.Date Enddate.DbType = DbType.Date myCommand = New SqlCommand("spUpdateinfo") myCommand.CommandType = CommandType.StoredProcedure myCommand.Connection = conn myCommand.Parameters.Add(ID) myCommand.Parameters.Add(Job) myCommand.Parameters.Add(Address) myCommand.Parameters.Add(Begindate) myCommand.Parameters.Add(Enddate) myCommand.Parameters.Add(Otherinfo) myCommand.ExecuteNonQuery()
SQL procedure
UPDATE jobInfo SET Job = @Job, Address= @Address, Begindate = CAST(@Begindate AS smalldatetime), Enddate = CAST(@Enddate AS smalldatetime), Otherinfo = @Otherinfo WHERE Job_id = @id
Took a while as I had to edit the sensitives out. Anyway, thx for help and any ideas what's going on? You can see the casts etc in there as I've tried to fiddle with whatever I could to fix it, but it still isn't working.
Edit 3: Thanks for the helps, off for the day so I'll continue looking at this tomorrow.