Hi All,

Can't insert data into SQL database... Keep getting error i dont know how to solve...

Plz help

Grtz Tiello

CODE:
--------------------------------------------------------------------------------------

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim adapter As New SqlDataAdapter
        Dim sql As String
        connetionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\mydatabase.mdf;Integrated Security=True;User Instance=True"
        connection = New SqlConnection(connetionString)
        sql = "insert into tb_klanten (Klant_ID,Contactpersoon,geslacht,adres,postcode,woonplaats,land,telefoonnummer,faxnummer,emailadres,naam_administratief_contactpersoon,administratief_emailadres, administratief_telefoonnummer,bankgironummer,uname,upass) values(textbox1.text,@dropdownlist1.text,@textbox3.text,@textbox4.text,@textbox4.text,@textbox5.text,@textbox6.text,@textbox7.text,@textbox8.text,@textbox9.text,@textbox11.text,@textbox12.text,@textbox13.text,@textbox10.text,@textbox14.text,@textbox15.text)"
        Try


            connection.Open()
            adapter.InsertCommand = New SqlCommand(sql, connection)
            adapter.InsertCommand.ExecuteNonQuery()
            label1.Text = "Gebruiker aangemaakt!"
        Catch ex As Exception
            MsgBox(ex.ToString)

        End Try

    End Sub

--------------------------------------------------------------------------------------

ERROR:

System.Data.SqlClient.SqlException: Must declare the scalar variable "@dropdownlist1".
at System.Data.SqlClient.SqlConnectrion.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
etc. etc.

--------------------------------------------------------------------------------------

Solved

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim adapter As New SqlDataAdapter
        Dim sql As String
        connetionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\mydatabase.mdf;Integrated Security=True;User Instance=True"
        connection = New SqlConnection(connetionString)

        sql = "insert into tb_klanten (Contactpersoon,geslacht,adres,postcode,woonplaats,land,telefoonnummer,faxnummer,emailadres,naam_administratief_contactpersoon,administratief_emailadres, administratief_telefoonnummer,bankgironummer,uname,upass)"
        sql = sql & " VALUES "
        sql = sql & "('" & TextBox1.Text & "',"
        sql = sql & "'" & DropDownList1.Text & "',"
        sql = sql & "'" & TextBox3.Text & "',"
        sql = sql & "'" & TextBox4.Text & "',"
        sql = sql & "'" & TextBox5.Text & "',"
        sql = sql & "'" & TextBox6.Text & "',"
        sql = sql & "'" & TextBox7.Text & "',"
        sql = sql & "'" & TextBox8.Text & "',"
        sql = sql & "'" & TextBox9.Text & "',"
        sql = sql & "'" & TextBox13.Text & "',"
        sql = sql & "'" & TextBox10.Text & "',"
        sql = sql & "'" & TextBox11.Text & "',"
        sql = sql & "'" & TextBox12.Text & "',"
        sql = sql & "'" & TextBox14.Text & "',"
        sql = sql & "'" & TextBox15.Text & "')"

        MsgBox(sql)

        Try



            connection.Open()
            adapter.InsertCommand = New SqlCommand(sql, connection)
            adapter.InsertCommand.ExecuteNonQuery()
            label1.Text = "Gebruiker aangemaakt!"
        Catch ex As Exception
            MsgBox(ex.ToString)

        End Try



    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.