开发者

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 = ObjDataRea开发者_Go百科der(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.


I may be off base but you have tDate as a Date variable type and then your trying to send it a string, you can just use tDate.ToString() in your SqlCommand lines and not convert it first.


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) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜