Using cm As New OleDb.OleDbCommand("", con)
cm.CommandText = "insert into tb(tname,bd,temail,homephone,mobile,country,sex,favcar,hob) values(@tname,@bd,@temail,@homephone,@mobile,@country,@sex,@favcar,@hob)"
For Each h As Control In mycontrols
If TypeOf h Is DateTimePicker Then
cm.Parameters.AddWithValue("@" & CType(h, DateTimePicker).Name & "", CType(h, DateTimePicker).Value.ToShortDateString)
ElseIf TypeOf h Is TextBox Then
cm.Parameters.AddWithValue("@" & CType(h, TextBox).Name & "", IIf(IsNumeric(CType(h, TextBox).Text), Val(CType(h, TextBox).Text), CType(h, TextBox).Text))
ElseIf TypeOf h Is ComboBox Then
cm.Parameters.AddWithValue("@" & CType(h, ComboBox).Name & "", CType(h, ComboBox).Text)
End If
Next
Try
If con.State = ConnectionState.Closed Then con.Open()
cm.ExecuteNonQuery()
gd(dg.Rows.Count - IIf(dg.AllowUserToAddRows, 2, 1))
If con.State = ConnectionState.Open Then con.Close()
Catch ex As Exception
MsgBox(Err.Description, MsgBoxStyle.Critical, "error")
End Try
End Using