简体   繁体   中英

vb.net SQL date to time conversion issue

The error is: Conversion failed when converting date and/or time from character string.

Basically I'm trying to make it so that when someone clicks a location in a list box it increments the correct field in a Stats table by 1 to say that they clicked the location. I've been trying to solve this for a while now and can't get anywhere. My date is in date format, and so is the Date field in my Stats table. My code can be seen below.

    Dim Current As String
    Dim Check As String
    Dim objCmd2 As Object
    Dim iCount As Integer
    Dim tDate As Date

    Current = LocBox.SelectedItem

    Check = LocList.FindString(Current)

    If Check > -1 Then
        MsgBox("You have already selected this place to visit")
    Else
        LocList.Items.Add(Current)
        ObjDataReader.Close()
        tDate = Date.Today
        MessageBox.Show(tDate)
        tDate = tDate.ToString()
        objCmd2 = New SqlCommand("SELECT " & Replace(Current, " ", "_") & " FROM Stats WHERE Date = '" & tDate & "'", objConn)
        ObjDataReader = objCmd2.ExecuteReader
        If ObjDataReader.HasRows Then
            ObjDataReader.Read()
            If ObjDataReader(0) Is DBNull.Value Then
                iCount = 0
            Else
                iCount = ObjDataReader(0)
            End If
        Else
            iCount = 0
        End If
        objCmd = New SqlCommand("INSERT INTO Stats(Date," & Replace(Current, " ", "_") & ") Values('" & tDate.Date & "'," & iCount & " )", objConn)
        ObjDataReader.Close()
        objCmd.ExecuteNonQuery()
        objConn.Close()

Thanks in advance.

The issue comes in with the second SQL statement I think. I don't understand why adding a date into a date field would be an issue, I've tried adding it as a string which didn't work. Thanks for all the answers so far.

As Martin points out this is probably the best choice:

int colnum = ConvertColumnNameToNumber(Current)
objCmd2 = New SqlCommand("SELECT "+colnum.ToString()+" FROM Stats WHERE [Date] = @inDate", objConn)
objCmd2.Parameters.Add("@inDate",SqlDbType.DateTime).value = tDate

Not tested or compiled might have typos.

Note the use of colnum above. Because you can't use parameters for column names you have to use the column number. If you have code that returns an integer as a return value you can protect against injection (it can only a number).

I'm going to guess the issue is here: MessageBox.Show(tDate)

You need to convert the date to a string using tDate.ToString or whatever other method you want.

Are you getting compile errors? I always code with Option Strict On, and your code would definitely bring up errors where you are not converting variables.

我可能不满意,但是您将tDate作为Date变量类型,然后尝试向其发送字符串,则可以在SqlCommand行中使用tDate.ToString()而不先进行转换。

Try Removing the apostrophes from around tdate so that it is...

objCmd2 = New SqlCommand("SELECT " & Replace(Current, " ", "_") & " FROM Stats WHERE Date = " & tDate, objConn) 

and

objCmd = New SqlCommand("INSERT INTO Stats(Date," & Replace(Current, " ", "_") & ") Values(" & tDate.Date & "," & iCount & " )", objConn) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM