Dim Sql_pro As String = "select * from t_product"
Da_Pro.MissingSchemaAction = MissingSchemaAction.AddWithKey
Da_Pro.SelectCommand = New OleDbCommand(Sql_pro, Cn)
Try
Da_Pro.Fill(Ds_Pro, "t_product")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Dim Dr As DataRow = Ds_Pro.Tables("t_product").Rows.Find(IdPro)
Dr เป็นหารหัสสินค้าสินค้า
Dim a As Integer = Dr("ชื่อฟิลด์ที่เก็บจำนวนสินค้าในตารางคลังสินค้า").ToString
With Dr
.BeginEdit()
.Item("pro_total_pro") = a - Val(FrmSell.TxtSell_Total.Text)
.EndEdit()
End With
Dim CbPro As New OleDbCommandBuilder(Da_Pro)
Da_Pro.UpdateCommand = CbPro.GetUpdateCommand
Da_Pro.Update(Ds_Pro.Tables("t_product"))
Ds_Pro.AcceptChanges()
Dim Sql_pro As String = "select * from ชื่อตารางของคลังสินค้า"
Da_Pro.MissingSchemaAction = MissingSchemaAction.AddWithKey
Da_Pro.SelectCommand = New OleDbCommand(Sql_pro, Cn)
Try
Da_Pro.Fill(Ds_Pro, "ชื่อตารางของคลังสินค้า")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Dim Dr As DataRow = Ds_Pro.Tables("ชื่อตารางของคลังสินค้า").Rows.Find(เป็นรหัสของสินค้า)
Dim TotalPro As Integer = Dr("เป็นฟิลด์ที่เก็บจำนวนสินค้าที่มีอยู่ในตารางคลังสินค้า").ToString
With Dr
.BeginEdit()
.Item("ฟิลด์ที่เก็บจำนวนสินค้าที่มีอยู่ในตารางคลังสินค้า") = TotalPro - จำนวนสินค้าที่ถูกซื้อไป
TotalPro เป็นตัวแปรที่ดึงข้อมูลมาจากในฟิลด์ของจำนวนสินค้าที่อยู่ในตาราง ซึ่งนำจำนวนที่มีอยู่ในสต๊อกทั้งหมดมาลบกับจำนวนสินค้าที่ถูกซื้อ
.EndEdit()
End With
Dim CbPro As New OleDbCommandBuilder(Da_Pro)
Da_Pro.UpdateCommand = CbPro.GetUpdateCommand
Da_Pro.Update(Ds_Pro.Tables("t_product"))
Ds_Pro.AcceptChanges()
โค๊ตอันนี้เป็นโค๊ตที่ไม่ต้องใช้คำสั่ง sql เพื่อ update ข้อมูลเพียงแค่ select ข้อมูลออกมาก่อนเท่านั้น ง่ายดีนะครับ
จากโค้ดที่ babyprogrammer ให้มา มื่อทำการรันแล้วมันฟ้อง error ว่า
Object reference not set to an instance of an object. ที่
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
ได้แล้วค่ะ แต่คราวนี้ มัน error ที่บรรทัด Dim Dr1 As DataRow = data.Tables("product").Rows.Find(txtserialno.Text) ฟ้อง error Object reference not set to an instance of an object. แต่พอเมื่อเรากด บันทึก จะมี msg ออกมาคือ The connectionString Properties has not been initialized ค่ะ
Dim conn As New SqlClient.SqlConnection()
Dim cmd As New SqlClient.SqlCommand()
'..............................................................................
Dim x As String = Today.Month & "/" & Today.Day & "/" & Today.Year
'............................................................................................................
conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Documents and Settings\User\My Documents\Visual Studio 2008\Projects\PROJECT MORYA.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True"
conn.Open()
Dim dt As New DataTable
dt.Columns.Add("รหัสสินค้า")
dt.Columns.Add("ชื่อสินค้า")
dt.Columns.Add("หน่วยสินค้า")
dt.Columns.Add("ราคาขาย")
dt.Columns.Add("จำนวน")
dt.Columns.Add("รวมเป็นเงิน")
Dim i As Integer = 0
While (i < DataGridView1.RowCount)
Dim a As String
a = ("เลขที่ใบเสร็จรับเงิน : " & txtid.Text & "") & vbNewLine
a &= ("วันที่ขายสินค้า : " & txtdate.Text & "") & vbNewLine
a &= ("รหัสเภสัชกร : " & cmbphar.Text & "") & vbNewLine
a &= ("รหัสสินค้า : " & txtserialno.Text & "") & vbNewLine
a &= ("ชื่อสินค้า : " & txtname.Text & "") & vbNewLine
a &= ("หน่วย : " & txtuniit.Text & "") & vbNewLine
a &= ("ราคาขาย/หน่วย: " & txtprice.Text & "") & vbNewLine
a &= ("จำนวน : " & txtquantity.Text & "") & vbNewLine
a &= ("รวมเป็นเงิน : " & txttotal.Text & "")
MessageBox.Show(a, "แสดงผล", MessageBoxButtons.OK, MessageBoxIcon.Information)
Dim cmdTXT As String = "insert into sales (id,date,pharmacist_id) values ('" & txtid.Text & "','" & txtdate.Text & "','" & cmbphar.Text & "')"
cmd.CommandText = cmdTXT
cmd.Connection = conn
i = i + 1
cmd.ExecuteNonQuery()
Dim cmdTXT1 As String = "insert into salesdetail (sales_date,product_serialno,product_name,product_unit,product_saleprice,quantity,total,sumtotal) values ('" & x & "','" & DataGridView1.Rows(i).Cells(0).Value & "','" & DataGridView1.Rows(i).Cells(1).Value & "', '" & DataGridView1.Rows(i).Cells(2).Value & "','" & DataGridView1.Rows(i).Cells(3).Value & "','" & DataGridView1.Rows(i).Cells(4).Value & "','" & DataGridView1.Rows(i).Cells(5).Value & "','" & txtsumtotal.Text & "')"
cmd.CommandText = cmdTXT1
cmd.Connection = conn
i = i + 1
cmd.ExecuteNonQuery()
End While
'..................................................................................
Dim Sql_pro As String = "select * from product"
Dim con As New SqlClient.SqlConnection()
Dim cmd1 As New SqlClient.SqlCommand(Sql_pro, con)
Dim adapter As New SqlDataAdapter(cmd1)
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
adapter.SelectCommand = New SqlCommand(Sql_pro, con)
Dim data As New DataSet
Try
adapter.Fill(data, "product")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Dim Dr1 As DataRow = data.Tables("product").Rows.Find(txtserialno.Text)
Dim totalstock As Integer = Dr1("totalstock").ToString
With dr1
.BeginEdit()
.Item("totalstock") = totalstock - txtquantity.Text
'totalstock เป็นตัวแปรที่ดึงข้อมูลมาจากในฟิลด์ของจำนวนสินค้าที่อยู่ในตาราง ซึ่งนำจำนวนที่มีอยู่ในสต๊อกทั้งหมดมาลบกับจำนวนสินค้าที่ถูกซื้อ
.EndEdit()
End With
Dim CbPro As New SqlCommandBuilder(adapter)
adapter.UpdateCommand = CbPro.GetUpdateCommand
adapter.Update(data.Tables("product"))
data.AcceptChanges()
'...........................................................................................
'....................................................................................
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว ", "บันทึกข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtserialno.Text = ""
txtname.Text = ""
txtuniit.Text = ""
txtprice.Text = ""
txtquantity.Text = ""
txttotal.Text = ""
ลองเปลี่ยน Dim con As New SqlClient.SqlConnection() ที่คุณเขียนเป็น
Public Con As New OleDbConnection(Sql_pro)
และไม่ทราบว่า Dim cmd1 As New SqlClient.SqlCommand(Sql_pro, con) คืออะไร
และที่ err ว่า error Object reference not set to an instance of an object ก็จะประมาณว่ามันหาชื่อ dataset ไม่เจอครับ
Date :
2009-08-01 14:33:52
By :
babyprogrammer
No. 12
Guest
ที่คุณ babyprogrammer บอกว่าให้เปลี่ยนจาก Dim con As New SqlClient.SqlConnection() เป็น
Public Con As New OleDbConnection(Sql_pro) พอดีว่าฐานข้อมูลเป็น sql server ค่ะ ไม่ได้เก็บด้วย access ค่ะ รบกวนหน่อยนะค่ะ ขอบคุณมากค่ะ
ขอเป็นโค้ด vb 2008 ได้ไหมค่ะ เขียนไม่ค่อยถูกค่ะ
มันติดตรง
ivate Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
If lsvProductList.Items.Count > 0 Then
If MessageBox.Show("คุณต้องการบันทึกรายการการซื้อสินค้า ใช่หรือไม่ ?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
'tr = Conn.BeginTransaction()
dr.Close()
Dim i As Integer
Dim CProductID As String
Dim CProductName As String
Dim CAmount As Integer = 0
Dim CPrice As Double = 0.0
Dim CTotal As Double = 0.0
Dim CPaidToSupplier As Double = 0.0
Dim NetTotal As Double = 0.0
Dim OldAmount As Integer = 0
Dim NewAmount As Integer = 0
For i = 0 To lsvProductList.Items.Count - 1
CProductID = lsvProductList.Items(i).SubItems(0).Text
CProductName = lsvProductList.Items(i).SubItems(1).Text
CAmount = CInt(lsvProductList.Items(i).SubItems(3).Text)
CPrice = CDbl(lsvProductList.Items(i).SubItems(2).Text)
CTotal = CDbl(lsvProductList.Items(i).SubItems(4).Text)
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO SalesDetail (SaleID,ProdID,Amount,SalePrice,Total)")
sb.Append(" VALUES (@SaleID,@ProdID,@Amount,@SalePrice,@Total)")
Dim sqlSave As String = sb.ToString()
sqlSave = sb.ToString()
With com
sb.Remove(0, sb.Length)
sb.Append("SELECT ProdID,Amount")
sb.Append(" FROM Product")
sb.Append(" WHERE (ProdID=@ProdID)")
sqlSave = sb.ToString()
With com
.CommandText = sqlSave
.Parameters.Clear()
.Parameters.Add("@ProdID", SqlDbType.Char).Value = CProductID
dr = .ExecuteReader()
If dr.HasRows Then
dr.Read()
OldAmount = dr.GetInt32(3) "มันติดตรงนี้ค่ะ"
dr.Close()
NewAmount = OldAmount - CAmount
sb.Remove(0, sb.Length)
sb.Append("UPDATE Product")
sb.Append(" SET Amount=@Amount")
sb.Append(" WHERE (ProdID=@ProdID)")
sqlSave = sb.ToString()
With com
.CommandText = sqlSave
.Parameters.Clear()
.Parameters.Add("@Amount", SqlDbType.Int).Value = NewAmount
.Parameters.Add("@ProdID", SqlDbType.Char).Value = CProductID
.ExecuteNonQuery()
End With
End If
dr.Close()
End With
lsvProductList.Clear()
ClearCustomer()
ClearProduct()
UnLockCustomerData()
lblNet.Text = "0"
txtSearchCustomer.Focus()
AutoGenerateSaleID()
lblSaleID.Text = ""
End If
End If
End Sub
แก้ให้หน่อยนะค่ะ มันขึ้น Error ว่า Conversion from string "" to type 'Integer' is not valid. ไม่รู้แก้ตรงไหนค่ะ ลองแก้แล้ว มันก็ยัง Error อยู่ ช่วยหน่อยนะค่ะ
Dim dc As Single = 0.0
dc = CSng(Total * (CInt(txtDc.Text) / 100))
Option Explicit On
Option Strict On
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing.Drawing2D
Imports System.Globalization
Imports System.Drawing.Printing
Public Class frmPurchase
Dim strConn As String
Dim Conn As SqlConnection
Dim com As SqlCommand
Dim dr As SqlDataReader
Dim dtSupplier As DataTable
Dim dtUnit As DataTable
Dim dtProduct As DataTable
Dim tr As SqlTransaction
Dim sb As StringBuilder
Dim Today As Date
Dim PageSize As Integer
Dim TotalRecord As Integer
Dim CurrentPage As Integer
Dim PageCount As Integer
Dim RecordToDisplay As Integer
Dim OldPageSize As Integer
Dim CBranchID As String
Dim CSupplierID As String
Dim sqlCheck As String
Dim CheckResult As Integer
Dim dtCheck As DataTable
Dim dtfInfo As DateTimeFormatInfo
Dim IsMouseDown As Boolean = False
Dim p As Point
Dim CurrentProduct(10) As String
Dim UseFont As New Font("MS Sans Serif", 10)
Dim prDoc As PrintDocument = New PrintDocument
Dim prDlg As PrintDialog = New PrintDialog
Dim sw As New Stopwatch()
Dim ProcessTime As Double
Dim _ToolStripLabel As New ToolStripLabel
'Public Sub New(ByRef refToolStripLabel As ToolStripLabel)
' InitializeComponent()
' _ToolStripLabel = refToolStripLabel
'End Sub
Private Sub frmPurchase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
sw.Reset()
sw.Start()
com = New SqlCommand()
With com
.CommandText = sqlIni
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader()
End With
If dr.HasRows Then
Dim dtBranch As DataTable
dtBranch = New DataTable()
dtBranch.Load(dr)
With cboBranch
.BeginUpdate()
.DisplayMember = "BranchName"
.ValueMember = "BranchID"
.DataSource = dtBranch
.EndUpdate()
End With
dtSupplier = New DataTable()
dtSupplier.Load(dr)
With cboSupplier
.BeginUpdate()
.DisplayMember = "SupName"
.ValueMember = "SupID"
.DataSource = dtSupplier
.EndUpdate()
End With
End If
dr.Close()
cboBranch.ContextMenu = New ContextMenu()
cboSupplier.ContextMenu = New ContextMenu()
cboPageSize.ContextMenu = New ContextMenu()
txtSearch.ContextMenu = New ContextMenu()
txtPage.ContextMenu = New ContextMenu()
lblNetVAT.Text = "7"
AutoGenerateOrderID()
AddHandler prDoc.PrintPage, New PrintPageEventHandler(AddressOf Me.StringToPrint_Print)
prDoc.DocumentName = "สั่งซื้อสินค้า"
prDlg.Document = prDoc
End Sub
Private Sub cboBranch_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboBranch.SelectedValueChanged
CBranchID = CStr(cboBranch.SelectedValue)
End Sub
Private Sub cboSupplier_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSupplier.SelectedValueChanged
CSupplierID = CStr(cboSupplier.SelectedValue)
End Sub
Private Sub ClearPODetails()
lblToday.Text = Today.ToString("D")
dtpPaidDate.Value = Today
dtpReceiveDate.Value = Today
End Sub
Private Sub ClearTotal()
lblNetDC.Text = "0"
lblNet.Text = "0"
End Sub
Private Sub ClearProductDetails()
txtSearch.Text = ""
lblProductName.Text = ""
lblPrice.Text = "0"
txtAmount.Text = "1"
txtDc.Text = "0"
lblTotal.Text = "0.00"
pnlProductList.Visible = False
End Sub
Private Sub ClearProductList()
lsvProductList.Items.Clear()
End Sub
Private Sub txtSearch_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSearch.KeyDown
If txtSearch.Text.Trim() = "" Then Exit Sub
If e.KeyCode = Keys.Enter Then
sb.Remove(0, sb.Length)
sb.Append("SELECT ProdID,ProdName,SalePrice")
sb.Append(" FROM Product")
sb.Append(" WHERE (ProdID=@ProdID)")
Dim sqlProduct As String = sb.ToString()
Dim dtProduct As DataTable
With com
.Parameters.Clear()
.Parameters.Add("@ProdID", SqlDbType.Char).Value = txtSearch.Text.Trim()
.CommandText = sqlProduct
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader
If dr.HasRows Then
dtProduct = New DataTable
dtProduct.Load(dr)
If dtProduct.Rows.Count = 1 Then
ClearBinding()
txtSearch.DataBindings.Add("Text", dtProduct, "ProdID")
lblProductName.DataBindings.Add("Text", dtProduct, "ProdName")
lblPrice.DataBindings.Add("Text", dtProduct, "SalePrice")
CalculateTotal()
End If
txtSearch.Focus()
txtSearch.SelectAll()
Else
ClearProductList()
MessageBox.Show("ไม่มีรายชื่อสินค้าตามเงื่อนไขที่คุณระบุ !!!", "ผลการค้นหา", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End With
dr.Close()
End If
sw.Stop()
ProcessTime = sw.ElapsedMilliseconds / 1000
_ToolStripLabel.Text = "เวลาที่ใช้ : " & ProcessTime.ToString("0.0000") & " วินาที"
End Sub
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
lblProductName.Text = ""
lblPrice.Text = "0"
txtAmount.Text = "1"
txtDc.Text = "0"
lblTotal.Text = "0"
pnlProductList.Visible = False
End Sub
Private Sub ClearBinding()
txtSearch.DataBindings.Clear()
lblProductName.DataBindings.Clear()
lblPrice.DataBindings.Clear()
End Sub
Private Sub cboPageSize_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPageSize.SelectedIndexChanged
If CInt(cboPageSize.Text) <> OldPageSize Then
ReCalculateRecordToDisplay()
OldPageSize = CInt(cboPageSize.Text)
Else
CurrentPage = 1
RecordToDisplay = 0
ShowProduct(CurrentPage)
End If
End Sub
Private Sub txtPage_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPage.KeyDown
If e.KeyCode = Keys.Enter Then
PageSize = CInt(cboPageSize.Text)
TotalRecord = dtProduct.Rows.Count
PageCount = CInt(Math.Ceiling(TotalRecord / PageSize))
If CInt(txtPage.Text) > PageCount Then
txtPage.Text = CStr(PageCount)
End If
CurrentPage = CInt(txtPage.Text)
RecordToDisplay = (CurrentPage - 1) * PageSize
ShowProduct(CurrentPage)
End If
End Sub
Private Sub llbMoveFirst_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llbMoveFirst.LinkClicked
If CInt(cboPageSize.Text) <> OldPageSize Then
ReCalculateRecordToDisplay()
OldPageSize = CInt(cboPageSize.Text)
Else
CurrentPage = 1
RecordToDisplay = 0
ShowProduct(CurrentPage)
End If
End Sub
Private Sub llbMovePrevious_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llbMovePrevious.LinkClicked
If CInt(cboPageSize.Text) <> OldPageSize Then
ReCalculateRecordToDisplay()
OldPageSize = CInt(cboPageSize.Text)
Else
CurrentPage = CurrentPage - 1
If CurrentPage < 1 Then
CurrentPage = 1
RecordToDisplay = 0
Else
RecordToDisplay = (CurrentPage - 1) * PageSize
End If
ShowProduct(CurrentPage)
End If
End Sub
Private Sub llbMoveNext_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llbMoveNext.LinkClicked
If CInt(cboPageSize.Text) <> OldPageSize Then
ReCalculateRecordToDisplay()
OldPageSize = CInt(cboPageSize.Text)
Else
CurrentPage = CurrentPage + 1
If CurrentPage > PageCount Then
CurrentPage = PageCount
End If
If RecordToDisplay = TotalRecord Then
Exit Sub
End If
ShowProduct(CurrentPage)
End If
End Sub
Private Sub llbMoveLast_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llbMoveLast.LinkClicked
If CInt(cboPageSize.Text) <> OldPageSize Then
ReCalculateRecordToDisplay()
OldPageSize = CInt(cboPageSize.Text)
Else
CurrentPage = PageCount
RecordToDisplay = (CurrentPage - 1) * PageSize
ShowProduct(CurrentPage)
End If
End Sub
Private Sub ShowProduct(ByVal PageNum As Integer)
Dim i As Integer
Dim FirstRecord As Integer
Dim LastRecord As Integer
Dim dtCurrent As DataTable
dtCurrent = dtProduct.Clone
FirstRecord = RecordToDisplay
If PageNum < PageCount Then
LastRecord = PageNum * PageSize
Else
LastRecord = TotalRecord
End If
For i = FirstRecord To LastRecord - 1
dtCurrent.ImportRow(dtProduct.Rows(i))
RecordToDisplay = RecordToDisplay + 1
Next
Dim p As New Point()
p.X = tbProductDetail.Left + grbProductDetail.Left + txtSearch.Left + 3
p.Y = tbProductDetail.Top + grbProductDetail.Left + txtSearch.Left + txtSearch.Height + 35
With pnlProductList
dgvProductList.DataSource = dtCurrent
.Location = p
.Visible = True
End With
lblPage.Text = "หน้าที่ : " & CurrentPage.ToString() & "/" & PageCount.ToString()
End Sub
Private Sub ReCalculateRecordToDisplay()
If dtProduct Is Nothing Then Exit Sub
PageSize = CInt(cboPageSize.Text)
TotalRecord = dtProduct.Rows.Count
PageCount = CInt(Math.Ceiling(TotalRecord / PageSize))
CurrentPage = 1
RecordToDisplay = 0
ShowProduct(CurrentPage)
End Sub
Private Sub FormatDgvProductList()
With dgvProductList
If .RowCount > 0 Then
.Columns(0).HeaderText = "รหัสสินค้า"
.Columns(1).HeaderText = "ชื่อสินค้า"
.Columns(2).HeaderText = "ราคา"
.Columns(0).ReadOnly = True
.Columns(1).ReadOnly = True
.Columns(2).ReadOnly = True
End If
End With
End Sub
Private Sub dgvProductList_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvProductList.CellFormatting
If e.ColumnIndex = -1 Then Exit Sub
If dgvProductList.Columns(e.ColumnIndex).Name = "LastCost" Then
e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
e.CellStyle.Format = "#,##0.00"
End If
End Sub
Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
pnlProductList.Visible = False
txtSearch.Focus()
txtSearch.SelectAll()
End Sub
Private Sub dgvProductList_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvProductList.CellMouseClick
With dgvProductList
txtSearch.Text = CStr(.Rows.Item(e.RowIndex).Cells(0).Value)
lblProductName.Text = CStr(.Rows.Item(e.RowIndex).Cells(1).Value)
End With
End Sub
Private Sub txtAmount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount.TextChanged
If txtAmount.Text.Trim() = "" Then txtAmount.Text = "1"
If CInt(txtAmount.Text) = 0 Then txtAmount.Text = "1"
CalculateTotal()
End Sub
Private Sub txtDc_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDc.TextChanged
If txtDc.Text.Trim() = "" Then txtDc.Text = "0"
If CInt(txtDc.Text) = 0 Then txtDc.Text = "0"
CalculateTotal()
End Sub
Private Sub CalculateTotal()
Dim Total As Double = 0
Total = CDbl(lblPrice.Text) * CInt(txtAmount.Text)
Dim dc As Single = 0.0
dc = CSng(Total * (CInt(txtDc.Text) / 100))
Total = Total - dc
lblTotal.Text = Total.ToString("#,##0.00")
End Sub
'Private Sub dgvProductList_CellMouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvProductList.CellMouseDown
' CalculateCurrentProduct(dgvProductList, e)
'End Sub
'Private Sub CalculateCurrentProduct(ByVal TargetDataGridView As DataGridView, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
' Dim Price As Single = 0.0
' Dim Total As Single = 0.0
' Dim dc As Single = 0.0
' With TargetDataGridView
' If e.RowIndex = -1 Then Exit Sub
' CurrentProduct(0) = CStr(.Rows.Item(e.RowIndex).Cells(0).Value)
' CurrentProduct(1) = CStr(.Rows.Item(e.RowIndex).Cells(1).Value)
' CurrentProduct(2) = Format(.Rows.Item(e.RowIndex).Cells(2).Value, "#,##0.00")
' Price = CSng(CSng(.Rows.Item(e.RowIndex).Cells(2).Value) * (CInt(txtDc.Text) / 100))
' Total = CSng(.Rows.Item(e.RowIndex).Cells(2).Value) * CInt(txtAmount.Text)
' dc = CSng(Total * (CInt(txtDc.Text) / 100))
' Total = Total - dc
' CurrentProduct(3) = txtAmount.Text
' CurrentProduct(5) = dc.ToString("#,##0.00")
' CurrentProduct(6) = Total.ToString("#,##0.00")
' CurrentProduct(7) = CStr(Price)
' End With
'End Sub
Private Sub dgvProductList_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvProductList.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim AnyData As DataObject
AnyData = New DataObject
AnyData.SetData(DataFormats.Text, CurrentProduct)
Dim dde As DragDropEffects
dde = dgvProductList.DoDragDrop(AnyData, DragDropEffects.Copy)
End If
End Sub
Private Sub lsvProductList_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lsvProductList.DragEnter
If (CurrentProduct(0) = "") OrElse (CurrentProduct(1) = "") Then Exit Sub
If (e.Data.GetDataPresent(DataFormats.Text, True)) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub lsvProductList_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lsvProductList.DragDrop
CurrentProduct = DirectCast(e.Data.GetData(DataFormats.Text, True), String())
Dim i As Integer = 0
Dim lvi As ListViewItem
Dim ProductID As String = ""
For i = 0 To lsvProductList.Items.Count - 1
ProductID = lsvProductList.Items(i).SubItems(0).Text
If CurrentProduct(0) = ProductID Then
MessageBox.Show("คุณเลือกรายการสินค้า ซ้ำกัน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
Next
lvi = New ListViewItem(CurrentProduct)
lsvProductList.Items.Add(lvi)
CalculateNet()
cmdSave.Enabled = True
Array.Clear(CurrentProduct, 0, CurrentProduct.Length)
End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
If (txtSearch.Text.Trim() = "") OrElse (lblProductName.Text.Trim() = "") Then
txtSearch.Focus()
Exit Sub
End If
If CSng(lblPrice.Text) = 0 Then
Exit Sub
End If
If CInt(txtDc.Text) = 0 Then
txtDc.Focus()
Exit Sub
End If
If CInt(txtAmount.Text) = 0 Then
txtAmount.Focus()
Exit Sub
End If
Dim i As Integer = 0
Dim lvi As ListViewItem
Dim ProductID As String = ""
For i = 0 To lsvProductList.Items.Count - 1
ProductID = lsvProductList.Items(i).SubItems(0).Text
If txtSearch.Text.Trim() = ProductID Then
MessageBox.Show("คุณเลือกรายการสินค้า ซ้ำกัน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
ClearProductDetails()
txtSearch.Focus()
Exit Sub
End If
Next
Dim anyData() As String
Dim Price As Single = 0.0
Dim TotalDiscount As Single = 0.0
lvi = New ListViewItem(anyData)
lsvProductList.Items.Add(lvi)
Array.Clear(CurrentProduct, 0, CurrentProduct.Length)
ClearProductDetails()
CalculateNet()
txtSearch.Focus()
End Sub
Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
ClearProductDetails()
txtSearch.Focus()
End Sub
Private Sub lsvProductList_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsvProductList.DoubleClick
Dim i As Integer = 0
For i = 0 To lsvProductList.SelectedItems.Count - 1
Dim lvi As ListViewItem
lvi = lsvProductList.SelectedItems(i)
lsvProductList.Items.Remove(lvi)
Next
If (lsvProductList.Items.Count = 0) Then cmdSave.Enabled = False
CalculateNet()
End Sub
Private Sub CalculateNet()
Dim i As Integer = 0
Dim NetVAT As Single
Dim NetDc As Single = 0.0
Dim Net As Single = 0.0
For i = 0 To lsvProductList.Items.Count - 1
NetDc = NetDc + CSng(lsvProductList.Items(i).SubItems(4).Text)
Next
lblNetDC.Text = NetDc.ToString("#,##0.00")
NetVAT = CSng(lblNetVAT.Text) * CSng(lblNetDC.Text) / 100
lblNet.Text = CSng(Net + NetVAT).ToString("#,##0.00")
End Sub
Private Sub pnlProductList_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlProductList.MouseDown
IsMouseDown = True
p = New Point(e.X, e.Y)
End Sub
Private Sub pnlProductList_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlProductList.MouseUp
IsMouseDown = False
End Sub
Private Sub pnlProductList_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlProductList.MouseMove
Dim xPos As Integer
Dim yPos As Integer
If IsMouseDown = True Then
xPos = (pnlProductList.Location.X + e.X) - p.X
yPos = (pnlProductList.Location.Y + e.Y) - p.Y
pnlProductList.Left = xPos
pnlProductList.Top = yPos
End If
End Sub
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Try
If lsvProductList.Items.Count > 0 Then
If MessageBox.Show("คุณต้องการบันทึกรายการการสั่งซื้อสินค้า ใช่หรือไม่ ?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
'tr = Conn.BeginTransaction()
dr.Close()
Dim i As Integer
Dim CProductID As String
Dim CNumberToOrder As String
Dim CDC As Double = 0.0
Dim CTotal As Double = 0.0
Dim NetTotal As Double = 0.0
For i = 0 To lsvProductList.Items.Count - 1
CProductID = lsvProductList.Items(i).SubItems(0).Text
CNumberToOrder = lsvProductList.Items(i).SubItems(1).Text
CDC = CDbl(lsvProductList.Items(i).SubItems(3).Text)
CTotal = CDbl(lsvProductList.Items(i).SubItems(4).Text)
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO OrderDetail (OrderID,ProdID,NumberToOrder,TotalDiscount,Total)")
sb.Append(" VALUES (@OrderID,@ProdID,@NumberToOrder,@TotalDiscount,@Total)")
Dim sqlSave As String = sb.ToString()
sqlSave = sb.ToString()
With com
Dim i As Integer = 0
Dim CurrentYPosition As Integer = 330
Dim strColumn1 As String = ""
Dim strColumn2 As String = ""
Dim strColumn3 As Integer = 0
Dim strColumn4 As Integer = 0
Dim strColumn5 As String = ""
Dim strColumn6 As Integer = 0
Dim strColumn7 As Integer = 0
For i = 0 To lsvProductList.Items.Count - 1
strColumn1 = lsvProductList.Items(i).SubItems(0).Text
strColumn2 = lsvProductList.Items(i).SubItems(1).Text
strColumn3 = CInt(lsvProductList.Items(i).SubItems(2).Text)
strColumn4 = CInt(lsvProductList.Items(i).SubItems(3).Text)
strColumn5 = lsvProductList.Items(i).SubItems(4).Text
strColumn6 = CInt(lsvProductList.Items(i).SubItems(5).Text)
strColumn7 = CInt(lsvProductList.Items(i).SubItems(6).Text)
AnyString(e.Graphics, lblNetDC.Text, 580, 580)
AnyString(e.Graphics, lblNetVAT.Text, 580, 605)
AnyString(e.Graphics, lblNet.Text, 580, 630)
End Sub
Private Sub AnyString(ByVal g As Graphics, ByVal printString As String, ByVal xPos As Integer, ByVal yPos As Integer)
Dim anyPoint As New PointF(xPos, yPos)
g.DrawString(printString, UseFont, Brushes.Black, anyPoint)
End Sub
Private Sub AutoGenerateOrderID()
Dim sqlTmp As String = ""
Dim comTmp As SqlCommand = New SqlCommand
Dim drTmp As SqlDataReader
Dim tmpOrderID As Integer = 0
sqlTmp = "SELECT TOP 1 OrderID FROM Orders "
sqlTmp &= " ORDER BY OrderID DESC"
tmpOrderID = CInt(drTmp.Item("OrderID"))
tmpOrderID = tmpOrderID + 1
lblOrderID.Text = tmpOrderID.ToString("0000000000000000")
End With
Catch
lblOrderID.Text = "0000000000000001"
End Try
drTmp.Close()
End Sub
End Class