 |
|
เพื่มข้อมูลลงฐานข้อมูลไม่ได้ครับ ช่วยทีครับ ไม่รู่ว่าผิดตรงไหนครับ |
|
 |
|
|
 |
 |
|
Code (VB.NET)
Imports System.Data.OleDb
Public Class ProductType
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Project\core_song.accdb")
Dim ObjCurrencyMgr As CurrencyManager
Dim ds As New DataSet
Dim da As New OleDbDataAdapter
Dim dr As OleDbDataReader
Dim dt As DataTable
Dim command As OleDbCommand
Dim task As String = ""
Private Sub showData()
conn.Open()
Dim ds As New DataSet
Dim sqlCmd As String = "SELECT * From ProductType ORDER BY รหัสประเภทสินค้า DESC"
da = New OleDbDataAdapter(sqlCmd, conn)
da.Fill(ds, "ProductType")
DGV1.DataSource = ds.Tables(0)
DGV1.Columns(0).HeaderText = "รหัสประเภทสินค้า"
DGV1.Columns(1).HeaderText = "ชื่อประเภทสินค้า"
DGV1.Columns(0).Width = 100
DGV1.Columns(1).Width = 170
conn.Close()
Binding()
setDisable()
End Sub
Private Sub Binding()
Dim ObjC As CurrencyManager
ObjC = Me.BindingContext(DGV1.DataSource, "")
txtID.DataBindings.Clear()
txtName.DataBindings.Clear()
txtID.DataBindings.Add("text", DGV1.DataSource, "รหัสประเภทสินค้า")
txtName.DataBindings.Add("text", DGV1.DataSource, "ชื่อประเภทสินค้า")
End Sub
Private Sub maxId()
Dim maxId As Integer = 0
conn.Open()
Dim sqlCmd As String = "Select Max(รหัสประเภทสินค้า) From ProductType"
command = New OleDbCommand(sqlCmd, conn)
Try
maxId = command.ExecuteScalar()
Catch ex As Exception
maxId = 0
End Try
txtID.Text = maxId + 1
conn.Close()
End Sub
Private Sub setDisable()
txtName.Enabled = False
End Sub
Private Sub setEnable()
txtName.Enabled = True
End Sub
Private Sub setBtnDisable()
btnAdd.Enabled = False
btnEdit.Enabled = False
btnDelete.Enabled = False
btnSave.Enabled = True
btnCancel.Enabled = True
End Sub
Private Sub setBtnEnable()
btnAdd.Enabled = True
btnEdit.Enabled = True
btnDelete.Enabled = True
btnSave.Enabled = False
btnCancel.Enabled = False
End Sub
Private Sub clearText()
txtID.Text = ""
txtName.Text = ""
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
task = "Add"
clearText()
setEnable()
maxId()
setBtnDisable()
End Sub
Private Sub product_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
showData()
setBtnEnable()
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
task = "Edit"
setEnable()
setBtnDisable()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sqlCmd As String = ""
If (task.Equals("Add")) Then
sqlCmd = "Insert Into ProductType(รหัสประเภทสินค้า,ชื่อประเภทสินค้า) Values(" & txtID.Text & ", '" & txtName.Text & "')"
ElseIf (task.Equals("Edit")) Then
sqlCmd = "Update ProductType SET "
sqlCmd &= "ชื่อประเภทสินค้า = '" & txtName.Text & "' Where รหัสประเภทสินค้า = " & txtID.Text
End If
MessageBox.Show(sqlCmd)
'dbQuery(sqlCmd) **************************************************************ตัวนี้คืออะไรครับ มันติดตรงนี้ครับหรือป่าวครับ
showData()
setBtnEnable()
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
clearText()
setDisable()
showData()
setBtnEnable()
End Sub
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MessageBox.Show("คุณต้องการลบข้อมูลนี้", "คำเตือน", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.Yes Then Dim sqlCmd As String = "Delete From ProductType Where รหัสประเภทสินค้า = " & txtID.Text
'dbQuery(sqlCmd)**************************************************************ตัวนี้คืออะไรครับ มันติดตรงนี้ครับหรือป่าวครับ
End If
showData()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
พอคลิกบันทึกแล้ว ข้อมูลไม่บันทึกครับ ไม่รู้ผิดครังไหนครับ ช่วยทีคับ ขอบคุณคับ
access 2008
Tag : .NET, Ms Access, VS 2008 (.NET 3.x)
|
|
 |
 |
 |
 |
Date :
2010-12-06 15:39:54 |
By :
bird |
View :
1404 |
Reply :
12 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
งงกับคุณพี่นะครับ
Select Max(รหัสประเภทสินค้า) From ProductType
คืออะไรอะ เเล้วส่วนอื่นอีกมากมาย งงครับ เอา Code ที่คุณพี่ทำเเล้วเเจ้งว่า Error ตรงไหนดีกว่าไหมครับ มางี้ไม่มีคนตอบให้นะ ผมเลยมาบอก
|
 |
 |
 |
 |
Date :
2010-12-06 15:50:51 |
By :
EucifeR |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโทษครับ มือไหม่ครับ
Error ครังนี่ครับ

|
 |
 |
 |
 |
Date :
2010-12-06 16:41:28 |
By :
bird |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอความนั้นแปลว่า หา dbQuery ไม่เจอครับ
ลองลบบรรทัดนั้นออกแล้วแทรกนี้ไปแทนครับ
command = New OleDbCommand(sqlCmd, conn)
command.ExecuteNonQuery()
|
 |
 |
 |
 |
Date :
2010-12-06 19:23:13 |
By :
stricken |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขึ้นมาอีกแล้วครับ

|
 |
 |
 |
 |
Date :
2010-12-06 22:20:29 |
By :
bird |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
dbQuery(...) ผมว่าเป็น Function หนึ่งที่ประกาศไว้แล้วไปคุณ copy มาไม่ครบ
TxtID.Text คุณไม่มีค่า Null หรือเป็น string ว่าง ๆ "" ทำให้ error Index or primary key cannot contain a Null value
table Product คุณควรเปลี่ยน Field เป็น ภาษาอังกฤษ นะครับ ต่อไปจะได้ชิน อ่าน code / exception ให้เข้าใจ copy มาอย่างเดียวไม่ดีน่ะครับ
|
 |
 |
 |
 |
Date :
2010-12-07 08:58:59 |
By :
numenoy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตรงไหนครับ หาไม่เจอครับ
|
 |
 |
 |
 |
Date :
2010-12-07 12:15:17 |
By :
bird |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใ้ห้มัน Response ออกมาหน่อยสิครับว่า Query มัีนได้อะไรออกมา แล้วมาว่ากันต่อไปครับ (ใ้ห้นำคำสั่ง Response.Write(sqlCmd) ไว้ต่อจาก End if อะครับ แล้วก็ดูด้วยว่า Error ไหม ถ้า Error ปิดโค๊ต command.excutenonquery() ดูครับ ว่าใช้ query อะไรลงไป
|
 |
 |
 |
 |
Date :
2010-12-07 13:01:58 |
By :
zero1150kfc |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้แก้ไขได้ ลบได้ครับ แต่เวลาเพื่มมันขึ้น erroevเหมือนเดิมอ่ะครับ ไม่รู้ติดตรงไหนครับ
|
 |
 |
 |
 |
Date :
2010-12-07 13:30:08 |
By :
b |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาคำสั่ง sql ก่อน query ให้ดูหน่อยครับ
|
 |
 |
 |
 |
Date :
2010-12-07 13:33:00 |
By :
zero1150kfc |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับ ขอบคุณทุกท่านมากๆเลยครับที่สระเวลามาตอบครับ
อีกอย่างครับ ตอนลบ ข้อมูลไม่ได้ลบออกเลยต้องรีไหม่จึงจะหายไปครับ อยากให้แสดงเลยอ่ะครับ
Code (VB.NET)
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
conn.Open()
If MessageBox.Show("คุณต้องการที่จะลบข้อมูลนี้", "คำเตือน", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.Yes Then
Dim sqlCmd As String = "DELETE FROM Product WHERE รหัสสินค้า = " & txtID.Text
'dbQuery(sqlCmd)
command = New OleDbCommand(sqlCmd, conn)
command.ExecuteNonQuery()
End If
conn.Close()
End Sub
|
 |
 |
 |
 |
Date :
2010-12-07 14:37:12 |
By :
bird |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|