Kalash
Kalash

Reputation: 15

implicit conversion from data type varchar to varbinary is not allowed

After I run the program, I add the data, and then i get this message:

implicit conversion from data type varchar to varbinary is not allowed

I watched this video on YouTube and I did exactly the same as youtuber. It works for him but not me.

I am trying to add data from Visual studio into an SQL database:

Here's the code:

Imports System.Data.SqlClient
SELECT CONVERT(varchar(100), CONVERT(varbinary(max),'0xFFD8FFE000'))
Public Class Form1

Dim Conn As SqlConnection
Dim CMD As SqlCommand

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Conn = New SqlConnection
    Conn.ConnectionString = "Data Source=BYG-A101-MOELKA;Initial Catalog=App;Integrated Security=True"
    Dim READER As SqlDataReader

    Try

        Conn.Open()
        Dim Query As String
        Query = "insert into person (ID,firstname,lastname,age) values ('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & TextBox3.Text & "', '" & TextBox4.Text & "')"

        CMD = New SqlCommand(Query, Conn)
        READER = CMD.ExecuteReader
        MessageBox.Show("Datasaved")

        Conn.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        Conn.Dispose()

    End Try


End Sub

Upvotes: 0

Views: 4173

Answers (1)

paparazzo
paparazzo

Reputation: 45096

A few problems

ExecuteReader should not be used for an insert
use ExecuteNonQuery

That is subject to injection attack. Use parameters. The parameters must match the data type of the table columns.

Upvotes: 1

Related Questions