 |
|
สอบถาม บันทึกการขายสินค้า listview ลงฐานข้อมูล access ใช้ vb.net |
|
 |
|
|
 |
 |
|
ฐานข้อมูล access
ตาราง access มี 3 ตาราง
คือ 1. ตาราง TableOrder มี Order_ID , Order_Date
2. ตาราง TableOrderdetail มี Order_ID , Product_ID , SalePrice , Quantity , TotalSal
3. ตาราง TableProduct มี Product_ID , Product_Name , CostPrice , SalePrice , QtyStock
Query โดย มี
Order_ID , Order_Date , Product_ID , Product_Name , SalePrice , Quantity , TotalSal
( Order_ID และ Order_Date มาจากตาราง TableOrder )
( Product_ID , SalePrice , Quantity , TotalSalมาจากตาราง TableOrderdetail )
( Product_Name มาจากตาราง TableProduct)

โค้ดบันทึก listview ลงฐานข้อมูล access
Code (VB.NET)
Private Sub ButtonSAVE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSAVE.Click
If ListViewOrder.Items.Count > 0 Then
If MessageBox.Show("คุณต้องการบันทึกรายการสั่งซื้อสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Information
) = Windows.Forms.DialogResult.Yes Then
tr = Conn.BeginTransaction()
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO Tableorder (Order_ID,Order_Date)")
sb.Append("VALUES (" & TextBoxReceiptid.Text.Trim().Replace("", "") & ",")
sb.Append("" & DateTime.Today & ")")
Dim sqlSave As String = sb.ToString
Dim com = Conn.CreateCommand()
With com
.CommandType = CommandType.Text
.CommandText = sqlSave
.Connection = Conn
.Transaction = tr
.ExecuteNonQuery()
End With
sb.Remove(0, sb.Length)
sb.Append("SELECT TOP 1 Order_ID FROM TableOrder")
sb.Append("TableOrder BY Order_ID DESC")
sqlSave = sb.ToString()
Dim LastOrder_ID As Integer
With com
.CommandType = CommandType.Text
.CommandText = sqlSave
.Connection = Conn
dr = .ExecuteReader()
If dr.HasRows Then
dr.Read()
LastOrder_ID = dr.GetInt32(dr.GetOrdinal("Order_ID"))
Else
LastOrder_ID = 10000
End If
End With
Dim i As Integer
For i = 0 To ListViewOrder.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO [TableOrderDetails] (Order_ID,Product_ID,")
sb.Append("SalePrice,Quantity,TotalSale)")
sb.Append("VALUES (" & LastOrder_ID & ",")
sb.Append("" & ListViewOrder.Items(i).SubItems(0).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(1).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(4).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(5).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(6).Text & ",0)")
sqlSave = sb.ToString()
With com
.CommandText = sqlSave
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกรายการสั่งซื้อสินค้าเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
ListViewOrder.Clear()
ClearProductData()
textboxNetTotal.Text = "0"
End If
End If
End Sub
End Class
โค้ด ผิดตรง tr = Conn.BeginTransaction() อยากทราบว่าทำฐานข้อมูลผิด หรือว่าผิดตรงไหนช่วยแนะนำ ที
ขึ้นว่า Object reference not set to an instance of an object.
Tag : .NET, Ms Access, VB.NET, VS 2010 (.NET 4.x)
|
ประวัติการแก้ไข 2014-10-23 23:44:46 2014-10-23 23:46:33 2014-10-23 23:46:50 2014-10-23 23:46:52
|
 |
 |
 |
 |
Date :
2014-10-23 23:41:27 |
By :
niceza07 |
View :
4426 |
Reply :
7 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวแปร conn ไม่ได้ถูกประกาศไว้ครับ ต้องประกาศตัวแปร conn ก่อนครับ
|
 |
 |
 |
 |
Date :
2014-10-24 07:27:57 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Inherits System.Windows.Forms.Form
Const database_file As String = ".\DBMarketProgram.mdb"
Dim strconnection As String = "Provider = Microsoft.jet.OLEDB.4.0;" & "Data Source =" & database_file
Dim cn As New OleDb.OleDbConnection(strconnection)
Dim da As New OleDb.OleDbDataAdapter
Dim Conn As DbConnection
Dim dr As DbDataReader
Dim tr As DbTransaction
ผมได้ประกาศไว้แล้วครับ
|
 |
 |
 |
 |
Date :
2014-10-24 12:27:33 |
By :
niceza07 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Dim Conn As DbConnection แบบนี้ยังไม่คอนเนคกับ database เลยนะครับ แค่ประกาศชนิดตัวแปรเฉย ไม่มีค่าอะไร เป็นแค่ NULL
Dim Conn As DbConnection = new DBConnection( connect กับ อะไร ก็ใส่เข้าไปครับ )
ปล. เห็นเปิด cn olddb แล้ว น่าจะใช้ cn แทน conn ได้เลยนะครับ
มันเป็น คลาส ระดับเดียวกันไม่ใช่เหรอครับ (ถ้าจำไม่ผิด ไม่ได้เขียนนานแล้ว)
|
ประวัติการแก้ไข 2014-10-24 14:23:35
 |
 |
 |
 |
Date :
2014-10-24 14:17:26 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าจะประกาศแบบนั้นทำไมไม่ใช้ cn ไปเลยล่ะ
แล้วยังใช้ transaction ได้ open connection ทิ้งไว้ไหม ???
|
 |
 |
 |
 |
Date :
2014-10-24 15:40:14 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไปลองทำมาแล้วครับ แต่ว่า ยังติดปัญหาอยู่
ตามภาพ กลายเป็นผิดตรงนี้แทน

นี้คือประกาศตัวแปร
Code (VB.NET)
Inherits System.Windows.Forms.Form
Const database_file As String = ".\DatabaseProgramMarKet.mdb"
Dim strconnection As String = "Provider = Microsoft.jet.OLEDB.4.0;" & "Data Source =" & database_file
Dim cn As New OleDb.OleDbConnection(strconnection)
Dim da As New OleDb.OleDbDataAdapter
Dim ds As New DataSet
Dim dr As DbDataReader
Dim tr As DbTransaction
Dim dc As New OleDbCommand
Dim strConn As String = ""
Dim sb As New StringBuilder()
Dim IsFind As Boolean = False
Dim IsFindProduct As Boolean = False
Dim WithEvents objCurrencymanager As CurrencyManager
ส่วนนี้ คือ code บันทึก
Code (VB.NET)
If ListViewOrder.Items.Count > 0 Then
End If
If MessageBox.Show("คุณต้องการบันทึกรายการสั่งซื้อสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Information
) = Windows.Forms.DialogResult.Yes Then
tr = cn.BeginTransaction()
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO Tableorder (Order_ID,Order_Date)")
sb.Append("VALUES (" & TextBoxReceiptid.Text.Trim().Replace("", "") & ",")
sb.Append("" & DateTime.Today & ")")
Dim sqlSave As String = sb.ToString
Dim com = cn.CreateCommand()
With com
.CommandType = CommandType.Text
.CommandText = sqlSave
.Connection = cn
.Transaction = CType(tr, OleDbTransaction)
.ExecuteNonQuery()
End With
tr.Commit()
sb.Remove(0, sb.Length)
sb.Append("SELECT TOP 1 Order_ID FROM TableOrder")
sb.Append("TableOrder BY Order_ID DESC")
sqlSave = sb.ToString()
Dim LastOrder_ID As Integer
With com
.CommandType = CommandType.Text
.CommandText = sqlSave
.Connection = cn
dr = .ExecuteReader()
If dr.HasRows Then
dr.Read()
LastOrder_ID = dr.GetInt32(dr.GetOrdinal("Order_ID"))
Else
LastOrder_ID = 10003
End If
End With
dr.Close()
Dim i As Integer
For i = 0 To ListViewOrder.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO [TableOrderDetails] (Order_ID,Product_ID,")
sb.Append("SalePrice,Quantity,TotalSale)")
sb.Append("VALUES (" & LastOrder_ID & ",")
sb.Append("" & ListViewOrder.Items(i).SubItems(0).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(1).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(4).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(5).Text & ",")
sb.Append(ListViewOrder.Items(i).SubItems(6).Text & ",0)")
sqlSave = sb.ToString()
With com
.CommandText = sqlSave
.ExecuteNonQuery()
End With
Next
MessageBox.Show("บันทึกรายการสั่งซื้อสินค้าเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
ListViewOrder.Clear()
ClearProductData()
textboxNetTotal.Text = "0"
cn.Close()
End If
ทำมาหลายวันแล้วยังไม่ได้ ขอคำแนะนำด้วยครับ ผิดตรงไหน ช่วยอธิบายให้เข้าใจทีครับ
|
 |
 |
 |
 |
Date :
2014-10-28 23:38:48 |
By :
niceza07 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้ทำได้แล้วในการ insert 2 table
แต่ตอนนี้ ติดปัญหา ด้านล่าง รบกวนผู้รู้ช่วยหน่อยครับ

|
 |
 |
 |
 |
Date :
2014-10-30 19:05:35 |
By :
niceza07 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
IIf(TextBox.Text.Length < 0, Convert.ToDouble(0), Convert.ToDouble(TextBox.Text.Length))
|
 |
 |
 |
 |
Date :
2014-10-31 00:55:11 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|