 |
|
รบกวนช่วยดูเรื่องการเพิ่มข้อมูล (VB.NET) มันขึ้นข้อความแบบนี้ Incorrect syntax near ')' |
|
 |
|
|
 |
 |
|
syntax ถูกทุกอย่างค่ะ แต่ทำไมผิด
คิดว่า 60% เกิดจากค่าของ sname เพราะ error เกิดใกล้ ) ซึ่ง sname อยู่ฝั่งนั้น
มี invalid charactor ของ sql command ค่ะ เช่น ' " &
เพราะเป็นค่าของ ชื่ออะไรสักอย่าง จึงมีความเป็นไปได้ที่ invalid charactor
เช่น I'm a frog
ทางแก้
เพิ่ม function scan invalid charactor ให้กับตัวแปรแบบ string ที่ส่งเข้าไปใน command string
หรือ
เปลี่ยนไปใช้ command parameter แทน
อยากให้ลองหาคำตอบเพิ่มนะคะ แต่ถ้าหาไม่ได้กลับมาถามอีกทีค่ะ
เพราะในบอร์ดตอบปัญหาลักษณะนี้บ่อยมากแล้วลองค้นดูนะคะ
|
 |
 |
 |
 |
Date :
2010-05-21 22:07:46 |
By :
blurEyes |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวนช่วยดูให้หน่อยค่ะ เพิ่มข้อมูลVB.Net มันขึ้น MessageBox ไม่สามารถเพิ่มได้เนื่องจาก Unclosed quotation mark after ther character string 'ข้อมูลที่กรอก)'. iIncorrect syntax near 'ข้อมูลที่กรอก)'.ค่ะ
Code (VB.NET)
Dim sqlEmployee As String = ""
Dim comEmployee As SqlCommand = New SqlCommand
Dim tmpMSG As String = ""
If (txtEmpID.Text = "") Or (txtEmpName.Text = "") Or (txtEmpPosition.Text = "") Or (txtEmpTel.Text = "") Or (txtUser.Text = "") Or (txtPass.Text = "") Then
MessageBox.Show("กรุณาป้อนข้อมูลสมาชิกให้ครบ !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtEmpID.Focus()
Exit Sub
End If
Try
Select Case ActionFlag
Case "Add"
sqlEmployee = "INSERT INTO Employee (EmployeeID,Emp_Name,Emp_Tel,"
sqlEmployee &= " Emp_Position, Username, Password)"
sqlEmployee &= " VALUES ('" & txtEmpID.Text & "',"
sqlEmployee &= "'" & txtEmpName.Text & "',"
If OptMale.Checked = True Then
sqlEmployee &= "'M',"
ElseIf OptFeMale.Checked = True Then
sqlEmployee &= "'F',"
End If
sqlEmployee &= "'" & txtEmpPosition.Text & "',"
sqlEmployee &= "'" & txtEmpTel.Text & "',"
sqlEmployee &= "'" & txtUser.Text & "',"
sqlEmployee &= "'" & txtPass.Text & ")"
Case "Edit"
sqlEmployee = "UPDATE Employee "
sqlEmployee &= " SET Emp_Name='" & txtEmpName.Text & "',"
If OptMale.Checked = True Then
sqlEmployee &= " Sex='M',"
ElseIf OptFeMale.Checked = True Then
sqlEmployee &= " Sex='F',"
End If
sqlEmployee &= " Emp_Position='" & txtEmpPosition.Text & "',"
sqlEmployee &= " Emp_Tel='" & txtEmpTel.Text & "',"
sqlEmployee &= " Username='" & txtUser.Text & "',"
sqlEmployee &= " Password='" & txtPass.Text & "',"
sqlEmployee &= " WHERE (EmployeeID='" & txtEmpID.Text & "')"
End Select
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
If ActionFlag = "Add" Then
tmpMSG = "คุณต้องการเพิ่มข้อมูล ใช่หรือไม่ ?"
ElseIf ActionFlag = "Edit" Then
tmpMSG = "คุณต้องการแก้ไขข้อมูล ใช่หรือไม่ ?"
End If
If MessageBox.Show(tmpMSG, "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
With comEmployee
.CommandType = CommandType.Text
.CommandText = sqlEmployee
.Connection = Conn
.ExecuteNonQuery()
End With
End If
Catch ErrProcess As Exception
MessageBox.Show("ไม่สามารถเพิ่มสมาชิกใหม่ได้ เนื่องจาก " & ErrProcess.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End Try
MessageBox.Show("บันทึกลงฐานข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
grbSearch.Enabled = True
ControlStatus(False)
btnAdd.Enabled = True
btnEdit.Enabled = False
btnOK.Visible = False
btnCancel.Visible = False
ClearData()
cmdMoveFirst.Enabled = False
cmdMovePrevious.Enabled = False
cmdMoveNext.Enabled = False
cmdMoveLast.Enabled = False
lblShowPositions.Text = ""
txtSearch.Text = ""
txtSearch.Focus()
|
 |
 |
 |
 |
Date :
2012-12-20 03:01:41 |
By :
Teaw |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวนหน่อยค่ะ โค้ดนี้เป็นการ Search ค่ะ เราพิมข้อมูลที่จะค้นหา แล้วขึ้น Error ตรง da.SelectCommand.CommandText = sqlEmployee ต้องแก้ไขตรงไหนค่ะ รบกวนด้วยนะค่ะ
Code (VB.NET)
Private Sub txtSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSearch.KeyDown
If txtSearch.Text <> "" Then
If (e.KeyCode = Keys.Enter) Then
Dim sqlEmployee As String = ""
sqlEmployee = "SELECT * FROM Employee"
If cboSearch.SelectedIndex = 0 Then
sqlEmployee &= " WHERE (EmployeeID='" & txtSearch.Text & "')"
ElseIf cboSearch.SelectedIndex = 1 Then
sqlEmployee &= " WHERE (Emp_Name LIKE '%" & txtSearch.Text & "%')"
End If
sqlEmployee &= " ORDER BY EmployeeID"
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
If IsFind = True Then
ds.Tables("Employee").Clear()
End If
da.SelectCommand.CommandText = sqlEmployee
da.Fill(ds, "Employee")
If ds.Tables("Employee").Rows.Count <> 0 Then
IsFind = True
ClearBinding()
lblEmpID.DataBindings.Add("Text", ds, "Employee.EmployeeID")
txtEmpNumber.DataBindings.Add("Text", ds, "Employee.Emp_Number")
txtEmpName.DataBindings.Add("Text", ds, "Employee.Emp_Name")
txtEmpTel.DataBindings.Add("Text", ds, "Employee.Emp_Tel")
txtEmpTel.DataBindings.Add("Text", ds, "Employee.Emp_Sex")
txtEmpPosition.DataBindings.Add("Text", ds, "Employee.Emp_Position")
txtUser.DataBindings.Add("Text", ds, "Employee.Username")
txtPass.DataBindings.Add("Text", ds, "Employee.Password")
dt = ds.Tables("Employee")
ShowPosition()
btnAdd.Enabled = False
btnEdit.Enabled = True
cmdMoveFirst.Enabled = True
cmdMovePrevious.Enabled = True
cmdMoveNext.Enabled = True
cmdMoveLast.Enabled = True
btnEdit.Focus()
Else
IsFind = False
MessageBox.Show("รายชื่อเจ้าหน้าที่คุณระบุ ไม่มี !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
lblShowPositions.Text = ""
btnAdd.Enabled = True
btnEdit.Enabled = False
cmdMoveFirst.Enabled = False
cmdMovePrevious.Enabled = False
cmdMoveNext.Enabled = False
cmdMoveLast.Enabled = False
txtSearch.Focus()
txtSearch.SelectAll()
Exit Sub
End If
End If
End If
End Sub
|
 |
 |
 |
 |
Date :
2012-12-28 01:09:04 |
By :
Teaw |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตั้งกระทู้ใหม่ พร้อมกับ Error ด้วยครับ
|
 |
 |
 |
 |
Date :
2012-12-28 09:25:34 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โทษทีค่ะมือใหม่ ตั้งกระทู้ใหม่ พร้อมกับ Error ยังไงค่ะ
|
 |
 |
 |
 |
Date :
2012-12-30 00:42:23 |
By :
Teaw |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|