Private Sub BarCode20()
Dim Sql As String = ""
Dim ReadId As String = ""
Dim NewId As Integer = 0
Try
Com = New SqlCommand()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
'########################### ลบข้อมูล ##############################
Sql = "DELETE FROM BarCode20"
With Com
.CommandText = Sql
.CommandType = CommandType.Text
.Connection = Conn
.ExecuteNonQuery()
End With
'########################### RunNumber ##############################
Sql = "SELECT TOP 1 ID_Barcode20 FROM BarCode20 ORDER BY ID_Barcode20 DESC"
With Com
.CommandType = CommandType.Text
.CommandText = Sql
.Connection = Conn
dr = .ExecuteReader()
If dr.HasRows Then
dr.Read()
ReadId = dr.Item("ID_Barcode20").ToString()
NewId = CInt(ReadId) + 1
End If
End With
dr.Close()
'########################### Save ##############################
For j = 0 To dgvFreshOrganicLongan.Rows.Count - 1
If CStr(dgvFreshOrganicLongan.Rows(j).Cells(13).Value) <> "" Or CStr(dgvFreshOrganicLongan.Rows(j).Cells(13).Value) <> "0" Then
Dim SizeLongan As String = ""
If dgvFreshOrganicLongan.Rows(j).Cells(0).Value.ToString.Remove(0, 7).Remove(2, 4) = "01" Then
SizeLongan = "Cut"
ElseIf dgvFreshOrganicLongan.Rows(j).Cells(0).Value.ToString.Remove(0, 7).Remove(2, 4) = "02" Then
SizeLongan = "AA"
ElseIf dgvFreshOrganicLongan.Rows(j).Cells(0).Value.ToString.Remove(0, 7).Remove(2, 4) = "03" Then
SizeLongan = "A"
End If
Dim NameProduct_Eng As String = ""
Sql = "SELECT T.NameTypePruduct_Eng,M.Name_Eng"
Sql &= " FROM TypeProduct AS T,Member AS M"
Sql &= " WHERE T.ID_TypeProduct=@ID_TypeProduct AND M.Id_Member=@Id_Member"
Dim dtNameProduct = New DataTable()
With Com
.Parameters.Clear()
.CommandText = Sql
.CommandType = CommandType.Text
.Connection = Conn
.Parameters.Add("@ID_TypeProduct", SqlDbType.NVarChar).Value = dtGanBarcode.Rows(j).Item("BarCode").ToString().Remove(0, 18)
.Parameters.Add("@Id_Member", SqlDbType.NVarChar).Value = dtGanBarcode.Rows(j).Item("BarCode").ToString().Remove(3, 17)
dr = .ExecuteReader
If dr.HasRows Then
dtNameProduct.Load(dr)
NameProduct_Eng = dtNameProduct.Rows(0).Item("NameTypePruduct_Eng").ToString() & " " & SizeLongan & " (" & dtNameProduct.Rows(0).Item("Name_Eng").ToString() & ")"
End If
End With
dr.Close()
For i = 0 To CInt(dgvFreshOrganicLongan.Rows(j).Cells(13).Value) - 1
Sql = "INSERT INTO BarCode20(ID_Barcode20,BarCodeNumber20,NameProduct)"
Sql &= " VALUES(@ID_Barcode20,@BarCodeNumber20,@NameProduct)"
With Com
.CommandText = Sql
.Parameters.Clear()
.Parameters.Add("@ID_Barcode20", SqlDbType.NVarChar).Value = NewId.ToString("0000")
.Parameters.Add("@BarCodeNumber20", SqlDbType.NVarChar).Value = dtGanBarcode.Rows(j).Item("BarCode").ToString()
.Parameters.Add("@NameProduct", SqlDbType.NVarChar).Value = NameProduct_Eng
.ExecuteNonQuery()
NewId = NewId + 1
End With
dr.Close()
Next
End If
Next
Conn.Close()
ShellEx(CInt(Me.Handle), "Open", Application.StartupPath() & "\Barcode21.btw", "", "", 10)
Catch ErrProcess As Exception
MessageBox.Show("ไม่สามารถเพิ่มสมาชิกใหม่ได้ เนื่องจาก" & ErrProcess.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End Try
End Sub