If w > 0 Then
MessageBox.Show("แก้ไขข้อมูลลูกค้าเรียบร้อย", "เรียบร้อย")
If ProductPathOld <> "" Then
Pb_ProductImg.Image.Dispose()
System.IO.File.Delete(Application.StartupPath & "..\..\..\ProductNewName\" & ProductImgOld)
'My.Computer.FileSystem.DeleteFile("..\..\ProductNewName\" & ProductImgOld)
ProductImg = Now.Day.ToString("00") & "_" & Now.Month.ToString("00") & "_" & Now.Year.ToString("0000") & "_" & Now.Minute.ToString("00") & "_" & Now.Second.ToString("00") & ".jpg"
Dim ObjBitmap As Bitmap = New System.Drawing.Bitmap(ProductPathOld)
Dim ObjGraphic As Graphics = Graphics.FromImage(ObjBitmap)
ObjBitmap.Save("..\..\ProductNewName\" & ProductImg, ImageFormat.Jpeg)
End If
error อันนี้ละครับ IO Exception was unhandled
The process cannot access the file 'D:\Working\Project4\ProgramProject\ProjectApp\ProjectApp\ProductNewName\21_10_2010_31_29.jpg' because it is being used by another process.
มันเเจ้งประมาณว่าใช้ รูปภาพนี้อยู่ช่ายม่ะครับเลยลบไม่ได้แต่ผมก้อได้ทำการ
Pb_ProductImg.Image.Dispose()
หน้าลบรูปของสินค้าก็ทำได้แต่พอมาหน้าแก้ไขสินค้าใช้โค้ดแบบเดี๋ยวกันแต่แค่มีการ save มาทีหลังกลับ error ครับ
Public ProductId, ProductName1, TypePId, ProductUnit As String
Public ProductImg, ProductPathOld, ProductPathNew As String
Public ProductSaleDouble As Double = 0.0
Private DTableTypeP As New DataTable
Private DSetRearchProduct As New DataSet Code (VB.NET)
Private Sub Btn_Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Update.Click
ProductId = ""
ProductName1 = ""
ProductImg = ""
ProductSaleDouble = 0.0
ProductUnit = ""
TypePId = ""
Txt_ProductSale.DataBindings.Clear()
Txt_ProductName.Text = Trim(Txt_ProductName.Text)
Txt_ProductUnit.Text = Trim(Txt_ProductUnit.Text)
Txt_ProductSale.Text = Trim(Txt_ProductSale.Text)
Dim ProductImgOld As String = Lbl_ParthImage.Text
If Txt_ProductId.Text <> "" Then
If Txt_ProductName.Text <> "" AndAlso Txt_ProductSale.Text <> "" AndAlso Txt_ProductUnit.Text <> "" Then
ProductId = Txt_ProductId.Text
ProductName1 = Txt_ProductName.Text
ProductSaleDouble = Convert.ToDouble(Txt_ProductSale.Text)
ProductUnit = Txt_ProductUnit.Text
TypePId = Cbb_TypeP.SelectedValue
If TypePId <> "" Then
Dim CheckP As Boolean
Connect()
SetCommand("select PD_Name from Product_Table where PD_Name=@ProductName AND PD_Id!=@ProductId")
Cmd.Parameters.AddWithValue("ProductName", ProductName1)
Cmd.Parameters.AddWithValue("ProductId", ProductId)
DSet = New DataSet
Adpter.Fill(DSet, "ProductT")
Dim DTableCheckProduct As New DataTable
DTableCheckProduct = DSet.Tables("ProductT")
If DTableCheckProduct.Rows.Count = 0 Then
CheckP = True
Else
CheckP = False
End If
If CheckP = True Then
Dim Mbr As MsgBoxResult
Mbr = MsgBox("ต้องการแก้ไขข้อมูลสินค้าจริงหรือไม่", _
MsgBoxStyle.OkCancel + _
MsgBoxStyle.Question, _
"ยืนยันการแก้ไขข้อมูลสินค้า")
If (Mbr = MsgBoxResult.Ok) Then
Connect()
SetReader("Update Product_Table Set TP_Id=@TypePId , PD_Name=@ProductName , PD_Unit=@ProductUnit , PD_Sale=@ProductSale , PD_Img=@ProductImg where PD_Id=@ProductId")
Cmd.Parameters.AddWithValue("ProductId", ProductId)
Cmd.Parameters.AddWithValue("TypePId", TypePId)
Cmd.Parameters.AddWithValue("ProductName", ProductName1)
Cmd.Parameters.AddWithValue("ProductUnit", ProductUnit)
Cmd.Parameters.AddWithValue("ProductSale", ProductSaleDouble)
Cmd.Parameters.AddWithValue("ProductImg", ProductImg)
Con.Close()
Dim w As Integer
Con.Open()
w = Cmd.ExecuteNonQuery
If w > 0 Then
MessageBox.Show("แก้ไขข้อมูลลูกค้าเรียบร้อย", "เรียบร้อย")
If ProductPathOld <> "" Then
Pb_ProductImg.Image.Dispose()
'System.IO.File.Delete(Application.StartupPath & "..\..\..\ProductNewName\" & ProductImgOld)
'My.Computer.FileSystem.DeleteFile("..\..\ProductNewName\" & ProductImgOld)
ProductImg = Now.Day.ToString("00") & "_" & Now.Month.ToString("00") & "_" & Now.Year.ToString("0000") & "_" & Now.Minute.ToString("00") & "_" & Now.Second.ToString("00") & ".jpg"
Dim ObjBitmap As Bitmap = New System.Drawing.Bitmap(ProductPathOld)
Dim ObjGraphic As Graphics = Graphics.FromImage(ObjBitmap)
ObjBitmap.Save("..\..\ProductNewName\" & ProductImg, ImageFormat.Jpeg)
End If
ShowDataProduct()
Txt_ProductId.Clear()
Txt_ProductName.Clear()
Txt_TypePId.Clear()
Cbb_TypeP.SelectedIndex = 0
Txt_ProductUnit.Clear()
Txt_ProductSale.Text = ""
ProductSaleDouble = 0.0
Txt_ProductSale.DataBindings.Clear()
ProductImg = ""
Pb_ProductImg.Image = Nothing
Con.Close()
Else
MessageBox.Show("ไม่สามารถแก้ไขข้อมูลลูกค้าได้", "ข้อผิดพลาด")
End If
ElseIf (Mbr = MsgBoxResult.Cancel) Then
Txt_ProductId.Clear()
Txt_ProductName.Clear()
Txt_TypePId.Clear()
Cbb_TypeP.SelectedIndex = 0
Txt_ProductUnit.Clear()
Txt_ProductSale.Text = ""
ProductSaleDouble = 0.0
Txt_ProductSale.DataBindings.Clear()
ProductImg = ""
Pb_ProductImg.Image = Nothing
Me.Height = 369
End If
Else
MessageBox.Show("มีสินค้าที่ชื่อ " & ProductName1 & " นี้แล้ว", "ข้อผิดพลาด")
End If
Else
MessageBox.Show("กรุณาเลือกประเภทสินค้า", "ข้อผิดพลาด")
End If
Else
MessageBox.Show("กรุณากรอกข้อมูลสินค้าให้ครบถ้วน", "ข้อผิดพลาด")
End If
Else
MessageBox.Show("กรุณาเลือกข้อมูลสินค้าที่ต้องการ", "ข้อผิดพลาด")
End If
End Sub
Dim ObjBitmap As Bitmap = New System.Drawing.Bitmap(ProductPathOld)
Dim NewObjBitMap As New System.Drawing.Bitmap(ObjBitmap)
ObjBitmap.Dispose()
Dim ObjGraphic As Graphics = Graphics.FromImage(NewObjBitMap)
NewObjBitMap.Save("..\..\ProductNewName\" & ProductImg.ToString.Trim, ImageFormat.Jpeg)