Resolved save text box info into sql database?

Roofuss

Member
Joined
Apr 19, 2009
Messages
9
Programming Experience
Beginner
Hi everyone

i am trying to save the data from the text boxes on my form to my sql database but im not having any luck please can someone help me?

ive simply got 4 text boxes and a save button the code for my save button is as follows.

VB.NET:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim Connection As SqlConnection = MSDB.GetConnection
        Dim mySqlCommand As SqlCommand = New SqlCommand
        Connection.Open()

        mySqlCommand.Connection = Connection
        Try
            mySqlCommand.CommandText = "INSERT INTO customers (CustomerID, Customer, Contact, Email) VALUES ('@CustomerID', '@Customer', '@Contact', '@Email')"

            Dim InsertCommand As SqlCommand = New SqlCommand
            InsertCommand.Parameters.AddWithValue("@CustomerID", tbCustomerID.Text)
            InsertCommand.Parameters.AddWithValue("@Customer", tbCustomer.Text)
            InsertCommand.Parameters.AddWithValue("@Contact", tbContact.Text)
            InsertCommand.Parameters.AddWithValue("@Email", tbEmail.Text)

            mySqlCommand.ExecuteNonQuery()
            MessageBox.Show("New Customer Added To Your Database")
        Catch ex As Exception
            MessageBox.Show("Customer could not be added!" & ex.Message)
        Finally
            If Connection.State = ConnectionState.Open Then
                Connection.Close()
            End If
        End Try

    End Sub

It just bring up an error, i thought this would be simple but for me its proving not to be at all.

thanks Roo
 
Last edited:
A quick glance suggests your SQL is wrong. Remove the quotes so that ie becomes

VB.NET:
INSERT INTO customers (CustomerID, Customer, Contact, Email) VALUES (@CustomerID, @Customer, @Contact, @Email)

Separately, it is always useful to tell us the actual error you are receiving, rather than just say "it brings up an error".

Edit - 2nd glance suggests you are adding the parameters to the wrong SQLCommand as well :)
 
A quick glance suggests your SQL is wrong. Remove the quotes so that ie becomes

VB.NET:
INSERT INTO customers (CustomerID, Customer, Contact, Email) VALUES (@CustomerID, @Customer, @Contact, @Email)

Separately, it is always useful to tell us the actual error you are receiving, rather than just say "it brings up an error".

Edit - 2nd glance suggests you are adding the parameters to the wrong SQLCommand as well :)


Thanks you are my hero and it works great now plus ill remeber to give you more details in future thanks again
 
Back
Top