Good evening. I want to insert data to a table in SQL DB.I have a combo box with the values "5" and "6". When you click one or the other in the combo box i want these values to be inserted in a database( MS SQL Server 2008).I also have some textboxes which are bind correnctly and have no problem. The problem is with the bindings(i guess) of the combo box. I get sql exception.
Here is a snippet. Any help would be appreciated.Thanks
Private Sub BindFields()
txtSurname.DataBindings.Add("Text", ObjDataView, "surname")
txtName.DataBindings.Add("Text", ObjDataView, "name")
cboColor.DataBindings.Add(cboColor.SelectedValue.ToString, ObjDataView, "color")
End Sub
Private Sub Customers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cboColor.Items.Add("5")
cboColor.Items.Add("6")
FillDataSetAndView()
BindFields()
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim objCommand As SqlCommand = New SqlCommand()
Dim intPosition As Integer
intPosition = objCurrencyManager.Position
ObjConnection.Open()
objCommand.Connection = ObjConnection
objCommand.CommandText = "INSERT INTO tblCustomers" & "(name, surname, color)" & "VALUES(@name,@surname,@color);"
objCommand.Parameters.AddWithValue("@name", txtName.Text)
objCommand.Parameters.AddWithValue("@surname", txtSurname.Text)
objCommand.Parameters.AddWithValue("@color", cboColor.SelectedValue.ToString)
Try
objCommand.ExecuteNonQuery()
Catch SqlExceptionErr As SqlException
MessageBox.Show(SqlExceptionErr.Message)
End Try
ObjConnection.Close()
FillDataSetAndView()
BindFields()
objCurrencyManager.Position = intPosition
ShowPosition()
End Sub
vba
tag got to do with this? – Remou Feb 7 '12 at 17:15