D-Boy110
D-Boy110

Reputation: 13

Need to understanding error message

hi guys am getting an error in ASP.NET( code behind in VB)and i don't know what it means. The error comes as i execute the code below to update a table in my database

Dim v_command1 As New SqlCommand
Dim v_sqlcode1 As String
v_sqlcode1 = "Update Session SET Endtime = DATEADD(Hour,'" & DRPL_DURATION.SelectedValue & "','" & TXTBX_START.Text & "') WHERE  SessionID = ('" & TXTBOX_SESSIONID.Text & "')"
        v_sqlconn.Open()

this code should allow me to be able to update the end time of a meeting when i decide to change how long it lasts for. the error am getting is this:

Additional information: Argument data type varchar is invalid for argument 2 of dateadd function.

does anyone have an idea what this error message might means.

Thanks

Upvotes: 1

Views: 60

Answers (3)

e2a
e2a

Reputation: 982

This error message is associated with the second argument of DATEADD(..). You need to use an integer (number) value.

See the documentation for more information DATEDD.

Upvotes: 0

David
David

Reputation: 219047

Just as the error states, you can't use a varchar data type for the second argument to DATEADD(). It's looking for an integer.

If the value you're passing is that integer, simply remove the single-quotes:

"[...] Hour, " & DRPL_DURATION.SelectedValue & ", [...]"

If it's not an integer and you're actually trying to pass a string of text, then you'll need to re-think what you're doing because you can't add text to a date.

Note: I also highly recommend using parameterized queries instead of directly concatenating values like this. This introduces a SQL injection vulnerability into your code which is eliminated by query parameters. Additionally, you'll find that errors related to column data types are far more rare when you simply pass the value to the query engine and let the engine decide how to manage the data type.

Upvotes: 3

Justin
Justin

Reputation: 175

https://msdn.microsoft.com/en-CA/library/ms186819.aspx

Dateadd is looking for a number as a 2nd arguement and you are passing it a string(varchar).

Upvotes: 1

Related Questions