tmighty
tmighty

Reputation: 11399

Parameters.AddWithValue failing

I'm getting the "System.FormatException: The input has the wrong format." error on the second attempt while the first one works perfectly fine.

Does anybody see why this is so?

Attempt 1:

    Using nCmdIns1 As SQLite.SQLiteCommand = cnUser.CreateCommand
        With nCmdIns1
            .CommandText = "INSERT INTO images (oemimageguid,imagetitle,imagecategory,imagesize,imageblob256) VALUES (@1,@2,@3,@4,@5)"
            .Parameters.Add("@1", DbType.String).Value = uOEMImageGUID
            .Parameters.Add("@2", DbType.String).Value = uTitle
            .Parameters.Add("@3", DbType.Int32).Value = iCat
            .Parameters.Add("@4", DbType.Int32).Value = uImageSize
            .Parameters.Add("@5", DbType.Binary).Value = uBytes
            .ExecuteNonQuery()
        End With
    End Using

Attempt 2:

    Using nCmdIns2 As SQLite.SQLiteCommand = cnUser.CreateCommand
        With nCmdIns2
            .CommandText = "INSERT INTO images (oemimageguid,imagetitle,imagecategory,imagesize,imageblob256) VALUES (@1,@2,@3,@4,@5)"
            .Parameters.AddWithValue("@1", DbType.String).Value = uOEMImageGUID
            .Parameters.AddWithValue("@2", DbType.String).Value = uTitle
            .Parameters.AddWithValue("@3", DbType.Int32).Value = iCat
            .Parameters.AddWithValue("@4", DbType.Int32).Value = uImageSize
            .Parameters.AddWithValue("@5", DbType.Binary).Value = uBytes
            .ExecuteNonQuery()
        End With
    End Using

I've tried to isolate the problem by removing parameters and values one by one, but in the end, I got the same error even with this sparse line:

    Using nCmdIns3 As SQLite.SQLiteCommand = cnUser.CreateCommand
        With nCmdIns3
            .CommandText = "INSERT INTO images (oemimageguid) VALUES (@1)"
            .Parameters.AddWithValue("@1", DbType.String).Value = uOEMImageGUID
            .ExecuteNonQuery()
        End With
    End Using

Here is a screenshot of the exception for attempt 3:

enter image description here

Upvotes: 0

Views: 549

Answers (1)

Steve
Steve

Reputation: 216303

The second parameter of AddWithValue is the Value itself, not the type

See MSDN AddWithValue

In any case try to use always the first method because you have more control on the type of the parameter.

Can we stop using AddWithValue already?

Using nCmdIns2 As SQLite.SQLiteCommand = cnUser.CreateCommand
    With nCmdIns2
        .CommandText = "INSERT INTO images (oemimageguid,imagetitle,imagecategory,imagesize,imageblob256) VALUES (@1,@2,@3,@4,@5)"
        .Parameters.AddWithValue("@1", uOEMImageGUID)
        .Parameters.AddWithValue("@2", uTitle)
        .Parameters.AddWithValue("@3", iCat)
        .Parameters.AddWithValue("@4", uImageSize)
        .Parameters.AddWithValue("@5", uBytes)
        .ExecuteNonQuery()
    End With
End Using

Upvotes: 4

Related Questions