Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Sql As String = ""
Dim ReadId As String = ""
Dim NewId As Integer = 0
' Dim SeriNumberDLGO As String = ""
' Dim SumTotalLonganFresh As Double = 0
'Dim SeriNumberSDLGB As String = ""
Dim SeriNumberRP As String = ""
'Dim SeriNumberID_DetailGroup As String = ""
Dim SeriTotal As Double = 0
Dim dtlotno As New DataTable()
If MessageBox.Show("คุณต้องการบันทึกข้อมูล ใช่หรือ ?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Try
Com = New SqlCommand()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
'############################## Update Repack ###########################################
For i = 0 To dgvRepack.Rows.Count() - 1
If dgvRepack.Rows(i).ReadOnly = False Then
Sql = "SELECT TOP 1 RP FROM Repack ORDER BY RP DESC"
With Com
.CommandType = CommandType.Text
.CommandText = Sql
.Connection = Conn
dr = .ExecuteReader()
If dr.HasRows Then
dr.Read()
ReadId = dr.Item("RP").ToString()
NewId = CInt(ReadId) + 1
SeriNumberRP = NewId.ToString("00000")
Else
SeriNumberRP = "00001"
End If
End With
dr.Close()
Sql = "INSERT INTO Repack(RP,BarCode,LotNo,TypeProduct,TotalWeight,ID_Repack)"
Sql &= " VALUES(@RP,@BarCode,@LotNo,@TypeProduct,@TotalWeight,@ID_Repack)"
With Com
.CommandText = Sql
.Parameters.Clear()
.Parameters.Add("@RP", SqlDbType.NVarChar).Value = SeriNumberRP
.Parameters.Add("@BarCode", SqlDbType.NVarChar).Value = CStr(dgvRepack.Rows(i).Cells(0).Value)
.Parameters.Add("@LotNo", SqlDbType.NVarChar).Value = CStr(dgvRepack.Rows(i).Cells(1).Value)
.Parameters.Add("@TypeProduct", SqlDbType.NVarChar).Value = CStr(dgvRepack.Rows(i).Cells(2).Value)
.Parameters.Add("@TotalWeight", SqlDbType.Float).Value = CStr(dgvRepack.Rows(i).Cells(3).Value)
.Parameters.Add("@ID_Repack", SqlDbType.NVarChar).Value = CStr(dgvRepack.Rows(i).Cells(4).Value)
.ExecuteNonQuery()
End With
End If
'############################################ Update StorkProduct ############################################
'Sql = "SELECT TotalWeight FROM StorkProduct WHERE BarCode=@BarCode AND LotNo=@LotNo AND TotalWeight<>'0'"
'SeriTotal = 0
'With Com
'.Parameters.Clear()
'.CommandType = CommandType.Text
'.CommandText = Sql
'.Connection = Conn
'.Parameters.Add("@BarCode", SqlDbType.NVarChar).Value = CStr(dgvOrganicLonganTeaPacking.Rows(i).Cells(0).Value)
'.Parameters.Add("@LotNo", SqlDbType.NVarChar).Value = CStr(dgvOrganicLonganTeaPacking.Rows(i).Cells(1).Value)
'dr = .ExecuteReader()
' If dr.HasRows Then
' dr.Read()
' SeriTotal = CDbl(dr.Item("TotalWeight"))
' End If
'End With
'dr.Close()
'Sql = "UPDATE StorkProduct SET TotalWeight=@TotalWeight WHERE BarCode=@BarCode AND LotNo=@LotNo AND TotalWeight<>'0'"
'With Com
' .CommandText = Sql
' .CommandType = CommandType.Text
' .Connection = Conn
' .Parameters.Clear()
' .Parameters.Add("@TotalWeight", SqlDbType.Float).Value = SeriTotal - CDbl(CStr(dgvOrganicLonganTeaPacking.Rows(i).Cells(3).Value))
' .Parameters.Add("@BarCode", SqlDbType.NVarChar).Value = CStr(dgvOrganicLonganTeaPacking.Rows(i).Cells(0).Value)
' .Parameters.Add("@LotNo", SqlDbType.NVarChar).Value = CStr(dgvOrganicLonganTeaPacking.Rows(i).Cells(1).Value)
' .ExecuteNonQuery()
' End With
'########################## Update StorkRepack ###########################################
Sql = "SELECT TOP 1 SRP FROM StorkRepack ORDER BY SRP DESC"
With Com
.CommandType = CommandType.Text
.CommandText = Sql
.Connection = Conn
dr = .ExecuteReader()
End With
dr.Close()
Sql = "INSERT INTO StorkRepack(SRP,BarCode,LotNo,TypeProduct,TotalWeight,ID_Repack,Count_LotNo)"
Sql &= " VALUES(@SRP,@BarCode,@LotNo,@TypeProduct,@TotalWeight,@ID_Repack,@Count_LotNo)"
With Com
.CommandText = Sql
.Parameters.Clear()
.Parameters.Add("@SRP", SqlDbType.NVarChar).Value = TextBox2.Text.Trim()
.Parameters.Add("@BarCode", SqlDbType.NVarChar).Value = "R" & TextBox2.Text.Trim() & "G" & TextBox1.Text.Trim() & CDate(DateTimePicker1.Value).ToString("dd/MM/yyyy", New System.Globalization.CultureInfo("en-US")).Remove(2, 1).Remove(4, 1).Remove(4, 2) & "31"
.Parameters.Add("@LotNo", SqlDbType.NVarChar).Value = "G" & TextBox1.Text.Trim() & CDate(DateTimePicker1.Value).ToString("dd/MM/yyyy", New System.Globalization.CultureInfo("en-US")).Remove(2, 1).Remove(4, 1).Remove(4, 2)
.Parameters.Add("@TypeProduct", SqlDbType.NVarChar).Value = lblInputTypeProduct.Text.Trim()
.Parameters.Add("@TotalWeight", SqlDbType.NVarChar).Value = lblInputTotalWeight.Text.Trim()
.Parameters.Add("@ID_Repack", SqlDbType.NVarChar).Value = "R" & TextBox2.Text.Trim()
.Parameters.Add("@Count_LotNo", SqlDbType.NVarChar).Value = TextBox1.Text.ToString()
.ExecuteNonQuery()
End With
SearchManagementRepack()
SearchAutoID()
SearchAutoCount_LotNo()
dgvRepack.Rows.Clear()
'lblInputTotalWeight.Text = CStr(CDbl(lblInputTotalWeight.Text) + CDbl(dgvRepack.Rows(dgvRepack.Rows.Count - 1).Cells(3).Value))
Next
Conn.Close()
MessageBox.Show("บันทึกลงฐานข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ErrProcess As Exception
MessageBox.Show("Error" & ErrProcess.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End Try
End If
End Sub
ช่วยดูโค้ดให้หน่อยคับมัน Error
ErrorIndex was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index