
Originally Posted by
Campion
You know what I just realized?
Why are you using TWO Command objects? Why are you putting the SQL into 1 object, then the parameters in to the other?
You need to use one or the other.
Lol thanks for pointing that out i was tired when i started this but ive fixed that now the error im getting is
SqlCommand.Prepare method requires all parameters to have an explicitly set type.
and my code know looks like this?
Code:
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')"
mySqlCommand.Parameters.AddWithValue("@CustomerID", tbCustomerID.Text)
mySqlCommand.Parameters.AddWithValue("@Customer", tbCustomer.Text)
mySqlCommand.Parameters.AddWithValue("@Contact", tbContact.Text)
mySqlCommand.Parameters.AddWithValue("@Email", tbEmail.Text)
mySqlCommand.Prepare()
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