Private Sub BTEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTEdit.Click
'FlagAction = "EDIT"
If ToolStripTextBox1.Text.Trim = "" Then
MessageBox.Show("กรุณาค้นหาใบสั่งซื้อที่ต้องการแก้ไขก่อน!!", "การแก้ไข")
Exit Sub
End If
If MessageBox.Show("คุณต้องการแก้ไขใบสั่งซื้อใช่หรือไม่!!", "การแก้ไข", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
Exit Sub
End If
With cn
If .State = ConnectionState.Open Then .Close()
.Open()
End With
Dim Del As String = "Delete from PurchaseDetails where PurID = '" & TxtPurID.Text & "'"
With cm
.CommandType = CommandType.Text
.CommandText = Del
.Connection = cn
.ExecuteNonQuery()
End With
End Sub
Private Sub BTSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTSave.Click
Dim sb As StringBuilder
If TxtPurID.Text.Trim() = "" Then
MessageBox.Show("กรุณากดปุ่มเพิ่มใบสั่งซื้อก่อน!!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
If MessageBox.Show("คุณต้องการเพิ่มใบสั่งซื้อใช่หรือไม่", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
Exit Sub
End If
Dim sqlSave As String = ""
sb = New StringBuilder()
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO Purchases(PurID,SupplieID,Date,NetTotal)")
sb.Append(" VALUES(@PurID,@SupID,@Date,@Tot)")
sqlSave = sb.ToString()
With cm
.Parameters.Clear()
.CommandText = sqlSave
.Connection = cn
cn.Open()
.Parameters.AddWithValue("@PurID", TxtPurID.Text)
.Parameters.AddWithValue("@SupID", CStr(ComboBox1.SelectedValue))
.Parameters.Add("@Date", SqlDbType.DateTime).Value = TxtPurDate.Text
.Parameters.Add("@Tot", SqlDbType.Float).Value = CSng(TxtTotal.Text)
.ExecuteNonQuery()
End With
Dim i As Integer = 0
For i = 0 To ListView1.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO PurchaseDetails (PurID,No,Amount,Price,Total)")
sb.Append(" VALUES (@PurID,@NO,@Amount,@Price,@Tot)")
sqlSave = sb.ToString()
With cm
.Parameters.Clear()
.CommandText = sqlSave
.Parameters.AddWithValue("@PurID", TxtPurID.Text)
.Parameters.Add("@No", SqlDbType.NVarChar, 50).Value = ListView1.Items(i).SubItems(1).Text
.Parameters.AddWithValue("@Amount", CInt(ListView1.Items(i).SubItems(4).Text))
.Parameters.AddWithValue("@Price", CSng(ListView1.Items(i).SubItems(3).Text))
.Parameters.Add("@Tot", SqlDbType.Float).Value = CSng(ListView1.Items(i).SubItems(5).Text)
.ExecuteNonQuery()
End With
sb.Remove(0, sb.Length())
sb.Append("UPDATE Products")
sb.Append(" SET UnitInStock = @un")
sb.Append(" WHERE (No=@ProID)")
Dim sqlUP As String
sqlUP = sb.ToString()
With cm
.Parameters.Clear()
.CommandText = sqlUP
.Parameters.AddWithValue("@un", CInt(ListView1.Items(i).SubItems(6).Text))
.Parameters.AddWithValue("@ProID", ListView1.Items(i).SubItems(1).Text)
.ExecuteNonQuery()
End With
Next
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว")
TxtPurID.Clear()
TxtPurDate.Clear()
TxtProNo.Clear()
TxtProName.Clear()
TxtPrice.Clear()
TxtAmount.Clear()
TxtTotal.Clear()
ListView1.Items.Clear()
cn.Close()
End Sub