Private Sub bttSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttSave.Click
If Me.txtName.Text = "" Then
MessageBox.Show("กรุณากรอกชื่อน้ำยา")
Me.txtName.Focus()
Exit Sub
End If
Dim a As String
If flagAction = "add" Then
a = "Insert into product( productID, productName,Item,heavy,price)"
a &= " Values(@productId,@productname,@Item,@Heavy,@Price)"
ElseIf flagAction = "edit" Then
a = "update product set productname ='" & Trim(txtName.Text) & "',item='" & Trim(R1.Checked Or R2.Checked Or R3.Checked Or R4.Checked Or R5.Checked) & "',Heavy='" & Trim(R6.Checked Or R7.Checked Or R8.Checked Or R9.Checked Or R10.Checked Or R11.Checked) & "',price='" & Trim(Txtprice.Text) & "'where productid= '" & Trim(txtId.Text) & "'"
End If
With cn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = conStr
.Open()
End With
Dim cm As New SqlCommand
With cm
.CommandType = CommandType.Text
.CommandText = a
.Connection = cn
.Parameters.Clear()
If flagAction = "add" Then
.Parameters.Add("@productID", SqlDbType.NVarChar, 10).Value = Me.txtId.Text
.Parameters.Add("@productname", SqlDbType.NVarChar, 100).Value = Me.txtName.Text
If R1.Checked = True Then
.Parameters.Add("@Item", SqlDbType.NVarChar, 100).Value = "เงิน"
ElseIf R2.Checked = True Then
.Parameters.Add("@Item", SqlDbType.NVarChar, 100).Value = "โรเดียม"
ElseIf R3.Checked = True Then
.Parameters.Add("@Item", SqlDbType.NVarChar, 100).Value = "นิกเกิล"
ElseIf R4.Checked = True Then
.Parameters.Add("@item", SqlDbType.NVarChar, 100).Value = "ทองคำ"
ElseIf R5.Checked = True Then
.Parameters.Add("@item", SqlDbType.NVarChar, 100).Value = "ทองคำขาว"
End If
If R6.Checked = True Then
.Parameters.Add("@heavy", SqlDbType.NVarChar, 10).Value = "1"
ElseIf R7.Checked = True Then
.Parameters.Add("@heavy", SqlDbType.NVarChar, 10).Value = "2"
ElseIf R8.Checked = True Then
.Parameters.Add("@heavy", SqlDbType.NVarChar, 10).Value = "3"
ElseIf R9.Checked = True Then
.Parameters.Add("@heavy", SqlDbType.NVarChar, 10).Value = "4"
ElseIf R10.Checked = True Then
.Parameters.Add("@heavy", SqlDbType.NVarChar, 10).Value = "5"
ElseIf R11.Checked = True Then
.Parameters.Add("@heavy", SqlDbType.NVarChar, 10).Value = "10"
End If
.Parameters.Add("@price", SqlDbType.Float).Value = Me.Txtprice.Text
ElseIf flagAction = "edit" Then
.Parameters.Add("@productID", SqlDbType.NVarChar, 10).Value = Me.txtId.Text
.Parameters.Add("@productname", SqlDbType.NVarChar, 100).Value = Me.txtName.Text
If R1.Checked = True Then
.Parameters.Add("@item", SqlDbType.NVarChar, 100).Value = "เงิน"
ElseIf R2.Checked = True Then
.Parameters.Add("@item", SqlDbType.NVarChar, 100).Value = "โรเดียม"
ElseIf R3.Checked = True Then
.Parameters.Add("@item", SqlDbType.NVarChar, 100).Value = "นิกเกิล"
ElseIf R4.Checked = True Then
.Parameters.Add("@item", SqlDbType.NVarChar, 100).Value = "ทองคำ"
ElseIf R5.Checked = True Then
.Parameters.Add("@item", SqlDbType.NVarChar, 100).Value = "ทองคำขาว"
End If
If R6.Checked = True Then
.Parameters.Add("@heavy", SqlDbType.NVarChar, 10).Value = "1"
ElseIf R7.Checked = True Then
.Parameters.Add("@heavy", SqlDbType.NVarChar, 10).Value = "2"
ElseIf R8.Checked = True Then
.Parameters.Add("@heavy", SqlDbType.NVarChar, 10).Value = "3"
ElseIf R9.Checked = True Then
.Parameters.Add("@heavy", SqlDbType.NVarChar, 10).Value = "4"
ElseIf R10.Checked = True Then
.Parameters.Add("@heavy", SqlDbType.NVarChar, 10).Value = "5"
ElseIf R11.Checked = True Then
.Parameters.Add("@heavy", SqlDbType.NVarChar, 10).Value = "10"
End If
.Parameters.Add("@price", SqlDbType.Float).Value = Me.Txtprice.Text
End If
.ExecuteNonQuery()
End With
MessageBox.Show("บันทึกเรียบร้อยแล้ว")
ShowData()
DataGridView1.Enabled = False
Clear()
bttAdd.Enabled = True
bttEdit.Enabled = True
bttClear.Enabled = True
End Sub