 |
|
บันทึกข้อมูลที่โชว์จาก Datagridview โดยดึงข้อมูลมาจาก Access 2003 ไม่ขึ้นคะ |
|
 |
|
|
 |
 |
|
สร้างข้อมูลจาก Access 2003 แล้วให้ Datagridview โชว์ข้อมูลขึ้นมาคะ พอเรากดที่ข้อมูลใน DataGridview ข้อมูลก็จะแสดงในแต่ละ Textbox ที่ได้กำหนดไว้
สิ่งที่ต้องการจะทำคือ ต้องการแก้ไขใน Textbox หรือ เมื่อข้อมูลโชว์ใน Textbox พอกดบันทึกข้อมูล ต้องการให้ข้อมูลเพิ่มเข้าไปใน Access คะ
แต่ลองทำแล้วไม่ขึ้น + Error อยู่หลายครั้ง ต้องทำการส้ราง Code ยังไงคะ
Code (VB.NET)
Imports System.Data
Imports System.Data.OleDb
Public Class frmOrderProducts
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim cal As Integer
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As String = "Provider = Microsoft.jet.oledb.4.0; Data Source = C:\infoproductsDB.mdb"
cn.ConnectionString = conn
cm.Connection = cn
End Sub
Private Sub DataGridView_CellMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView.CellMouseUp
With DataGridView
Try
txtIDType.Text = .Rows.Item(e.RowIndex).Cells("IDType").Value
txtID.Text = .Rows.Item(e.RowIndex).Cells("ID").Value
txtNameType.Text = .Rows.Item(e.RowIndex).Cells("NameType").Value
txtName.Text = .Rows.Item(e.RowIndex).Cells("Name").Value
txtPriceUnit.Text = .Rows.Item(e.RowIndex).Cells("PriceUnit").Value
Catch ex As Exception
End Try
End With
End Sub
Public Sub SelectProductType()
DataGridView.Columns.Item(0).HeaderText() = "รหัสสินค้า"
DataGridView.Columns.Item(1).HeaderText() = "รหัสประเภทสินค้า"
DataGridView.Columns.Item(2).HeaderText() = "ชื่อประเภทสินค้า"
DataGridView.Columns.Item(3).HeaderText() = "ชื่อสินค้า"
DataGridView.Columns.Item(4).HeaderText() = "ราคาต่อหน่วย"
End Sub
Private Sub btNovel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btNovel.Click
cm.CommandText = "SELECT * FROM ProductsNovel"
da.SelectCommand = cm
da.Fill(ds, "ProductsNovel")
DataGridView.DataSource = ds.Tables("ProductsNovel")
SelectProductType()
End Sub
Private Sub btSStory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSStory.Click
cm.CommandText = "SELECT * FROM ProductsShortStory"
da.SelectCommand = cm
da.Fill(ds, "ProductsShortStory")
DataGridView.DataSource = ds.Tables("ProductsShortStory")
SelectProductType()
End Sub
Private Sub btCom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCom.Click
cm.CommandText = "SELECT * FROM ProductsCom"
da.SelectCommand = cm
da.Fill(ds, "ProductsCom")
DataGridView.DataSource = ds.Tables("ProductsCom")
SelectProductType()
End Sub
Private Sub btCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCal.Click
cal = txtPriceUnit.Text * txtAmount.Text
txtCost.Text = cal
End Sub
Private Sub btClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btClear.Click
ds.Clear()
End Sub
Private Sub btSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSave.Click
cm.CommandText = "INSERT INTO OrderProducts (IDOrder,CusID,Amount,CostTotal) values (" & txtSlip.Text & "," & txtCusID.Text & "," & txtAmount.Text & "," & txtCost.Text & ")"
cn.Open()
cm.ExecuteReader()
cn.Close()
MessageBox.Show("การบันทึกเสร็จเรียบร้อย", "ยืนยัน")
End Sub
End Class
ผลลัพธ์แบบฟอร์มที่ทำไว้คะ

รบกวนด้วยคะ ต้องแก้ Code ยังไงบ้างคะ
Tag : .NET, Ms Access, Win (Windows App), LINQ, VB.NET, VS 2008 (.NET 3.x)
|
|
 |
 |
 |
 |
Date :
2011-02-23 07:45:39 |
By :
ppc493 |
View :
3188 |
Reply :
8 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
บรรทัดที่ 65
เปลี่ยน ExecuteReader
เป็น ExecuteNonQuery
ครับ
|
 |
 |
 |
 |
Date :
2011-02-23 08:50:17 |
By :
kaimuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองแล้วคะ ไม่ได้ ขึ้น Error (ตามรูปเลยคะ)

|
 |
 |
 |
 |
Date :
2011-02-23 11:27:46 |
By :
ppc493 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดูเหมือน cm.CommandText จะเปลี่ยนนิครับ แต่ผมเห็นไม่หมด ลบกวนลง Code บรรทัดนี้ทั้งหมด ได้ไหมครับ
|
 |
 |
 |
 |
Date :
2011-02-23 13:09:59 |
By :
kaimuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Private Sub btSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSave.Click
Try
cm.CommandText = "INSERT INTO OrderProducts (IDOrder,CusID,IDType,NameType,ID,Name,PriceUnit,Amount,CostTotal) values (" & txtSlip.Text & "," & txtCusID.Text & "," & txtIDType.Text & "," & txtNameType.Text & "," & txtID.Text & "," & txtName.Text & "," & txtPriceUnit.Text & "," & txtAmount.Text & "," & txtCost.Text & ")"
cn.Open()
cm.ExecuteReader()
cn.Close()
MessageBox.Show("การบันทึกเสร็จเรียบร้อย", "ยืนยัน")
Catch ex As Exception
MessageBox.Show("คุณกรอกข้อมูลไม่ครบถ้วน กรุณากรอกใหม่อีกครั้ง", "คำเตือน")
End Try
End Sub
จากที่สังเกตอ่าคะ ที่มัน Error ก็เพราะ NameType กับ Name ที่รับค่าเป็น Text แต่ถ้าเป็นตัวเลขมันจะไม่ Error รับค่าได้ปกติคะ
แต่ต้องการจะให้แก้ไขข้อมูลที่เป็น Text หรือ ภาษาไทยได้อ่าคะ ต้องแก้ไขยังไงดีคะ รบกวนด้วยคะะ ขอบคุณมากๆค่า
|
 |
 |
 |
 |
Date :
2011-02-23 14:42:07 |
By :
ppc493 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องใส่ ตัวนี้ด้วยครับกรณีเป็นตัวอักษร ตัวนี้อ่ะครับ '
|
 |
 |
 |
 |
Date :
2011-02-23 15:29:56 |
By :
kaimuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ประมาณนี้ครับ
Code (VB.NET)
Private Sub btSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSave.Click
Dim _String As New System.Text.StringBuilder
Try
_String.Append("INSERT INTO OrderProducts ")
_String.Append("(IDOrder,CusID,IDType,NameType,ID,Name,PriceUnit,Amount,CostTotal) ")
_String.Append("values ('")
_String.Append(txtSlip.Text & "','")
_String.Append(txtCusID.Text & "','")
_String.Append(txtIDType.Text & "','")
_String.Append(txtNameType.Text & "','")
_String.Append(txtID.Text & "','")
_String.Append(txtName.Text & "',")
_String.Append(txtPriceUnit.Textt & ",")
_String.Append(txtAmount.Text & ",")
_String.Append(txtCost.Text & ")")
cm.CommandText = _String.ToString
cN.Open()
cm.ExecuteReader()
cN.Close()
MessageBox.Show("การบันทึกเสร็จเรียบร้อย", "ยืนยัน")
Catch ex As Exception
MessageBox.Show("คุณกรอกข้อมูลไม่ครบถ้วน กรุณากรอกใหม่อีกครั้ง", "คำเตือน")
End Try
End Sub
|
 |
 |
 |
 |
Date :
2011-02-23 15:45:15 |
By :
kaimuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากๆๆเลยคะ ได้แล้ว ^^"  
|
 |
 |
 |
 |
Date :
2011-02-23 21:35:39 |
By :
ppc493 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2013-02-23 03:58:47 |
By :
lusifer |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|