0

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
6
  • But what kind of data suits to be stored both as binary and character? Commented Jul 8, 2015 at 13:12
  • Schema of the person table? And why are you using CMD.ExecuteReader()? And please don't use string concatenation. See stackoverflow.com/questions/17729200/executenonquery-for-insert Commented Jul 8, 2015 at 13:15
  • i only did what the youtuber did, in his case it works fine!! I've created the table in such: ID int primarykey FirstName Varchar (50) LastName Varchar (50) Age int Commented Jul 8, 2015 at 13:26
  • possible duplicate of Inserting data into SQL Server database using VB.Net Commented Jul 8, 2015 at 13:41
  • Blam: I appreciate that you are trying to find a solution for me. However, i'm still learning and i haven't gotten into parameters yet. Therefore, it's hard for me to reflect from the link you've send me. As I said before, the programmer on youtube made it work and i cannot understand why it's not working for me. it keeps saying that i should use the CONVERT function!! so what i'm i missing? Should i change the whole code or should i insert/delete 1 or 2 to make it work? Commented Jul 8, 2015 at 13:57

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I've tried to read a lot about parameters... but i can't seem to understand how to make one with my code. Also, as i said, i'm trying to learn how from a youtuber (I've posted the link) In his case, it works fine. Can you please demonstrate it for me. i'm stuck
Start with ExecuteReader should not be used for an insert
Ok, that i did, i removed ( READER = CMD.ExecuteReader) and replaced it with (CMD.ExecuteNonQuery) Same problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.