Update intsert delete to a sql database through a sql connection

johmolan

Well-known member
Joined
Oct 11, 2008
Messages
129
Programming Experience
Beginner
Hi!

I connect to må sql express server through this connection:

VB.NET:
       DS = "Data Source=" + TextBox1.Text + ";Initial Catalog=" + TextBox4.Text + ";User ID=" + TextBox2.Text + ";Password=" + TextBox3.Text + ""
connetionString = DS
cnn = New SqlConnection(connetionString)
cnn.Open()

Then I make another form, connects to the DB and retrieves a tabel and show it in datagridview. But when I now wants to change the data in the table I can't seem to find the right way to update of insert into the table. my code looks like this at the moment:

VB.NET:
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.IO


Public Class Form2

    Dim myConnection As SqlConnection
    Dim myCommand As SqlDataAdapter
    Dim ds As DataSet
    Dim Insupdel As String
    Dim Insupdel2 As String
    Dim Insupdel3 As String
    Dim Sqlcom As SqlCommand

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        Test()


    End Sub

    Sub Test()

        myConnection = Form1.cnn

        myCommand = New SqlDataAdapter("SELECT * FROM Kundenavn", myConnection)

        ' Create and fill the DataSet.
        ds = New DataSet()
        myCommand.Fill(ds, "Kundenavn")
        DataGridView1.DataSource = ds.Tables("Kundenavn")



      


        Insupdel = "INSERT INTO Kundenavn  (Kundenavn, KundeID, Kunde_ABC, Kontaktperson) VALUES ('@Kundenavn', '@KundeID', '@Kunde_ABC', '@Kontaktperson')"

        Insupdel2 = "UPDATE Kundenavn  (Kundenavn, KundeID, Kunde_ABC, Kontaktperson) VALUES ('@Kundenavn', '@KundeID', '@Kunde_ABC', '@Kontaktperson')"

        Insupdel3 = "DELETE Kundenavn  (Kundenavn, KundeID, Kunde_ABC, Kontaktperson) VALUES ('@Kundenavn', '@KundeID', '@Kunde_ABC', '@Kontaktperson')"


        Sqlcom = New SqlCommand(Insupdel, myConnection)





    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form1.cnn.Open()
        Sqlcom.ExecuteNonQuery()
        Form1.cnn.Close()
        Test()
    End Sub
End Class

As you see I have declared three separate strings to do the insert odate etc but when I try it I get an error which says :

Conversion failed when converting the varchar value '@KundeID' to data type int.

But I can't seem to find where to "convert it" to an int. the value I put in is integer but I get the same error if I leave the table empty...

Anyone here who can point me in the right direction??
 
Back
Top