Home > Blockchain >  Why "conversion failed when converting the varchar value 'passed' to data type int&qu
Why "conversion failed when converting the varchar value 'passed' to data type int&qu

Time:08-16

I am using following code, but getting error.

conversion failed when converting the varchar value 'passed' to data type int

Try
  con.open()
    Dim query = "update NTable SET Semester=Semester 1 WHERE Semester BETWEEN 1 AND 8 "
    Dim cmd As SqlCommand
    cmd = New SqlCommand(query, con)
    cmd.ExecuteNonQuery()
    con.close()
    con.open()
    Dim query2 = "update NTable SET Semester='Passed' WHERE Semester=9"
    Dim cmd2 As SqlCommand
    cmd2 = New SqlCommand
    cmd2 = New SqlCommand(query2, con)
    cmd2.ExecuteNonQuery()
    con.close()
    display()
Catch ex As Exception
    MsgBox(ex.Message)
End Try

Is there anything I am missing?

CodePudding user response:

  Dim query2 = "update NTable SET Semester='Passed' WHERE Semester=9"

what datatype for Semester column as you update string value, but in first query you update integer value.

In this Semester variable what you stored ? string value or integer ? Show variable declaration line.

 Dim query = "update NTable SET Semester=Semester 1 WHERE Semester BETWEEN 1 AND 8 "

CodePudding user response:

According to your first UPDATE that is successfuly processed, the Semester column in the database is a Numeric data type.

In your second update you are trying to assign a character typed value 'Passed' to the same column. That is the issue that the error message is specifically referring to, not the WHERE clause as you have assumed.

This looks like a common typo, you can cut'n'pasted the query without updating the target field correctly. The solution is to change your query to SET the actual field that will accept the value 'Passed'.

Assuming that the column is called Result, the following statement would be expected:

Dim query2 = "update NTable SET Result='Passed' WHERE Semester=9"
  • Related