 |
|
การ Add ข้อมูลจากหน้าฟอร์ม VB 2005 ลงใน SQL Server 2005 ผ่าน TextBox |
|
 |
|
|
 |
 |
|
รบกวนพี่ๆให้คำแนะนำหน่อยนะครับ คือผมต้องการ Add ข้อมูลจากหน้าฟอร์มของ VB 2005 ลงในฐานข้อมูล SQL Server 2005 แต่มันฟ้อง Error Msg ว่า "คุณป้อนรหัสพนักงานซ้ำ" (ตามที่ผมได้เขียนไว้ใน Msg) ไม่รู้ว่ามีโค้ดตรงส่วนไหนที่ไม่ถูกต้องหรือเปล่า
ตารางที่ผมจะเพิ่มข้อมูลลงไปนั้นมีอยู่ 7 ฟิลด์ ตามโค้ด รบกวนพี่ด้วยนะครับ ขอบคุณล่วงหน้าครับ
ปล.ในฟอร์มอื่นๆสามารถทำได้ (แต่มีแค่ 2 ฟิลด์)
รายละเอียดโค้ดดังนี้ครับ...
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
If txtDriverID.Text.Trim() = "" Then
MessageBox.Show("กรุณาป้อนรหัสพนักงานก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtDriverID.Focus()
Exit Sub
End If
If txtDriverName.Text.Trim() = "" Then
MessageBox.Show("กรุณาป้อนชื่อพนักงาน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtDriverName.Focus()
Exit Sub
End If
If txtSex.Text.Trim() = "" Then
MessageBox.Show("กรุณาระบุเพศ(M=ชาย,F=หญิง) !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtSex.Focus()
Exit Sub
End If
If txtAddress.Text.Trim() = "" Then
MessageBox.Show("กรุณาป้อนที่อยู่ !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtAddress.Focus()
Exit Sub
End If
If txtProvince.Text.Trim() = "" Then
MessageBox.Show("กรุณาป้อนชื่อจังหวัด !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtProvince.Focus()
Exit Sub
End If
If txtTelephone.Text.Trim() = "" Then
MessageBox.Show("กรุณาป้อนเบอร์โทรศัพท์ !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtTelephone.Focus()
Exit Sub
End If
If txtStartDate.Text.Trim() = "" Then
MessageBox.Show("กรุณาระบุวันที่เริ่มงาน(xx/xx/25xx) !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtStartDate.Focus()
Exit Sub
End If
If MessageBox.Show("คุณต้องการเพิ่มรายชื่อพนักงานใหม่ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
tr = Conn.BeginTransaction()
Try
'sb.Remove(0, sb.Length)
sb = New StringBuilder()
sb.Append("INSERT INTO Driver (DriverID,DriverName,")
sb.Append("Sex,Address,Province,Telephone,StartDate)")
sb.Append(" VALUES (@DriverID,@DriverName,@Sex,")
sb.Append("@Address,@Province@,Telephone,@StartDate)")
Dim sqlAdd As String
sqlAdd = sb.ToString()
With com
.CommandText = sqlAdd
.CommandType = CommandType.Text
.Connection = Conn
.Transaction = tr
.Parameters.Clear()
.Parameters.Add("@DriverID", SqlDbType.NVarChar).Value = txtDriverID.Text.Trim()
.Parameters.Add("@DriverName", SqlDbType.NVarChar).Value = txtDriverName.Text.Trim()
.Parameters.Add("@Sex", SqlDbType.NVarChar).Value = txtSex.Text.Trim()
.Parameters.Add("@Address", SqlDbType.NVarChar).Value = txtAddress.Text.Trim()
.Parameters.Add("@Province", SqlDbType.NVarChar).Value = txtProvince.Text.Trim()
.Parameters.Add("@Telephone", SqlDbType.NVarChar).Value = txtTelephone.Text.Trim()
.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = txtStartDate.Text.Trim()
.ExecuteNonQuery()
End With
tr.Commit() MessageBox.Show("เพิ่มรายชื่อพนักงานใหม่ เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
ClearAllData()
ShowData()
FormatDgvDriverList()
Catch
MessageBox.Show("คุณป้อนรหัสพนักงานซ้ำ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) tr.Rollback()
End Try
End If
txtDriverID.Focus()
End Sub
Tag : .NET, Ms SQL Server 2005, VB.NET, VS 2005 (.NET 2.x)
|
|
 |
 |
 |
 |
Date :
2011-05-04 00:56:04 |
By :
suvivor |
View :
1649 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอแก้ไขสีที่เน้นข้อความด้านบนนะครับ อ่านยากแสบตา
Try
'sb.Remove(0, sb.Length)
sb = New StringBuilder()
sb.Append("INSERT INTO Driver (DriverID,DriverName,")
sb.Append("Sex,Address,Province,Telephone,StartDate)")
sb.Append(" VALUES (@DriverID,@DriverName,@Sex,")
sb.Append("@Address,@Province@,Telephone,@StartDate)")
Dim sqlAdd As String
sqlAdd = sb.ToString()
With com
.CommandText = sqlAdd
.CommandType = CommandType.Text
.Connection = Conn
.Transaction = tr
.Parameters.Clear()
.Parameters.Add("@DriverID", SqlDbType.NVarChar).Value = txtDriverID.Text.Trim()
.Parameters.Add("@DriverName", SqlDbType.NVarChar).Value = txtDriverName.Text.Trim()
.Parameters.Add("@Sex", SqlDbType.NVarChar).Value = txtSex.Text.Trim()
.Parameters.Add("@Address", SqlDbType.NVarChar).Value = txtAddress.Text.Trim()
.Parameters.Add("@Province", SqlDbType.NVarChar).Value = txtProvince.Text.Trim()
.Parameters.Add("@Telephone", SqlDbType.NVarChar).Value = txtTelephone.Text.Trim()
.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = txtStartDate.Text.Trim()
.ExecuteNonQuery()
End With
tr.Commit()
และก็ Msg ที่ฟ้อง Error ครับ
Catch
MessageBox.Show("คุณป้อนรหัสพนักงานซ้ำ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
|
 |
 |
 |
 |
Date :
2011-05-04 01:05:10 |
By :
suvivor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|