ช่วยแก้โค้ดให้หน่อยคับ มันขึ้นว่ารหัสซ้ำคับ
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If MessageBox.Show("คุณต้องการเพิ่มพนักงานใหม่ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then tr = Conn.BeginTransaction() Try sb.Remove(0, sb.Length) sb.Append("INSERT INTO Customer (Customer_ID,FullName,B-date,Age,Sex,HumH,Road,Lane,Subdivision,Amphur,City,PostID,Telephone,E-mail)") sb.Append(" VALUES (@Customer_ID,@FullName,@B-date,@Age,@Sex,@HumH,@Road,@Lane,@Subdivision,@Amphur,@City,@PostID,@Telephone,@E-mail)") Dim sqlAdd As String sqlAdd = sb.ToString()
With Com .CommandText = sqlAdd .CommandType = CommandType.Text .Connection = Conn .Transaction = tr .Parameters.Clear() .Parameters.Add("@Customer_ID", OleDbType.VarChar).Value = TextBox1.Text.Trim() .Parameters.Add("@FullName", OleDbType.VarChar).Value = TextBox2.Text.Trim() .Parameters.Add("@B-date", OleDbType.VarChar).Value = DateTimePicker1.Value.ToShortDateString() .Parameters.Add("@Age", OleDbType.VarChar).Value = TextBox3.Text.Trim()
If RadioButton1.Checked = True Then .Parameters.Add("@Sex", OleDbType.VarChar).Value = "ชาย".Trim() ElseIf RadioButton2.Checked = True Then .Parameters.Add("@Sex", OleDbType.VarChar).Value = "หญิง".Trim() End If
.Parameters.Add("@HumH", OleDbType.VarChar).Value = TextBox4.Text.Trim() .Parameters.Add("@Road", OleDbType.VarChar).Value = TextBox5.Text.Trim() .Parameters.Add("@Lane", OleDbType.VarChar).Value = TextBox6.Text.Trim() .Parameters.Add("@Subdivision", OleDbType.VarChar).Value = TextBox7.Text.Trim() .Parameters.Add("@Amphur", OleDbType.VarChar).Value = TextBox8.Text.Trim() .Parameters.Add("@City", OleDbType.VarChar).Value = ComboBox1.Text.Trim() .Parameters.Add("@PostID", OleDbType.VarChar).Value = TextBox9.Text.Trim() .Parameters.Add("@Telephone", OleDbType.VarChar).Value = TextBox10.Text.Trim() .Parameters.Add("@E-mail", OleDbType.VarChar).Value = TextBox11.Text.Trim()
.ExecuteNonQuery() End With tr.Commit() MessageBox.Show("เพิ่มพนักงานใหม่ เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information) ' ClearAllData() ShowData() FormatDgvCustomerList() Catch ex As Exception MessageBox.Show("คุณป้อนรหัสพนักงานซ้ำ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) tr.Rollback() End Try End If TextBox1.Focus() End Sub
|