พี่ค่ะ ติด error Index was outside the bounds of the array
พอกดปุ่ม save เเล้วมันฟ้อง error อ่ะค่ะ
ไม่รุ้ว่า array ตัวไหนมันเรียกใช้เกินอยู่
นี่เป็น code ปุ่ม save นะค่ะ
Code (VB.NET)
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
If txtPONo.Text.Trim() = "" Then
MessageBox.Show("กรุณาใส่หมายเลขใบสั่งซื้อด้วย !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtPONo.Focus()
Exit Sub
End If
If MessageBox.Show("คุณต้องการบันทึกรายการสั่งซื้อสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
tr = Conn.BeginTransaction()
Try
sb.Remove(0, sb.Length)
sb.Append("SELECT TOP 1 OrderID FROM [Order]")
sb.Append(" ORDER BY OrderID DESC")
Dim sqlSave As String
sqlSave = sb.ToString()
Dim NewID As String
Dim FirstID As String
Dim LastID As Long
With com
.Parameters.Clear()
.Transaction = tr
.CommandText = sqlSave
Using dr As System.Data.SqlClient.SqlDataReader = .ExecuteReader()
' dr = .ExecuteReader()
If dr.HasRows Then
With dr
.Read()
NewID = .GetString(.GetOrdinal("OrderID"))
End With
FirstID = Stringex.StringFromLeft(NewID, 8)
LastID = CLng(Stringex.StringFromRight(NewID, 7))
LastID += 1
NewID = String.Format(FirstID, "00000000") & LastID.ToString("0000000")
Else
NewID = Today.Year & CStr(cboCompany.SelectedValue) & "0000001"
End If
' End With
dr.Close()
End Using
End With
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO [Order] (OrderID,Com_ID,PONo,")
sb.Append(" OrderDate,ReceiveDate,PaidDate,NetDc,VATRate,NetVAT,Net,")
sb.Append(" lsReceivegAll,lsPaid,lsNormal,OrderBy,ReceivedBy,PaidBy)")
sb.Append(" VALUES (@OrderID,@Com_ID,@PONo,")
sb.Append(" @OrderDate,@ReceiveDate,@PaidDate,@NetDc,@VATRate,@NetVAT,@Net,")
sb.Append(" @lsReceivegAll,@lsPaid,@lsNormal,@OrderBy,@ReceivedBy,@PaidBy)")
sqlSave = sb.ToString()
With com
.Parameters.Clear()
.CommandText = sqlSave
.Parameters.Add("@OrderID", SqlDbType.NVarChar).Value = NewID
.Parameters.Add("@Com_ID", SqlDbType.NVarChar).Value = CCompanyID
.Parameters.Add("@PONo", SqlDbType.NVarChar).Value = txtPONo.Text.Trim()
.Parameters.Add("@OrderDate", SqlDbType.DateTime).Value = Today
.Parameters.Add("@ReceiveDate", SqlDbType.DateTime).Value = dtpReceiveDate.Value
.Parameters.Add("@PaidDate", SqlDbType.DateTime).Value = dtpPaidDate.Value
.Parameters.Add("@NetDc", SqlDbType.Float).Value = CSng(lblNetDC.Text)
.Parameters.Add("@VATRate", SqlDbType.Float).Value = CSng(txtVATRate.Text)
.Parameters.Add("@NetVAT", SqlDbType.Float).Value = CSng(lblNetVAT.Text)
.Parameters.Add("@Net", SqlDbType.Float).Value = CSng(lblNet.Text)
.Parameters.Add("@lsReceivegAll", SqlDbType.NVarChar).Value = "0"
.Parameters.Add("@lsPaid", SqlDbType.NVarChar).Value = "0"
.Parameters.Add("@lsNormal", SqlDbType.NVarChar).Value = "1"
.Parameters.Add("@OrderBy", SqlDbType.NVarChar).Value = UserName
.Parameters.Add("@ReceivedBy", SqlDbType.NVarChar).Value = ""
.Parameters.Add("@PaidBy", SqlDbType.NVarChar).Value = ""
.ExecuteNonQuery()
End With
Dim i As Integer
Dim CProductID As String
Dim OldProductInOrder As Integer = 0
Dim NewProductInOrder As Integer = 0
Dim CMode As String = ""
For i = 0 To lsvCarpartsList.Items.Count - 1
CProductID = lsvCarpartsList.Items(i).SubItems(0).Text
sb.Remove(0, sb.Length())
sb.Append("INSERT INTO OrderDetail (OrderID,Car_PartsID,NumberToOrder,UnitID,")
sb.Append(" NumberToReceived,Cost,DiscountPerUnit,TotalDiscount,")
sb.Append(" Total,lsReceivegAll)")
sb.Append(" VALUES (@OrderID,@Car_PartsID,@NumberToOrder,@UnitID,")
sb.Append(" @NumberToReceived,@Cost,@DiscountPerUnit,@TotalDiscount,")
sb.Append(" @Total,@lsReceivegAll)")
sqlSave = sb.ToString()
With com
.Parameters.Clear()
.CommandText = sqlSave
.Parameters.Add("@OrderID", SqlDbType.NVarChar).Value = NewID
.Parameters.Add("@Car_PartsID", SqlDbType.NVarChar).Value = CProductID
.Parameters.Add("@NumberToOrder", SqlDbType.Int).Value = CInt(lsvCarpartsList.Items(i).SubItems(3).Text)
.Parameters.Add("@UnitID", SqlDbType.NVarChar).Value = CStr(lsvCarpartsList.Items(i).SubItems(7).Text)
.Parameters.Add("@NumberToReceived", SqlDbType.Int).Value = 0
.Parameters.Add("@Cost", SqlDbType.Float).Value = CSng(lsvCarpartsList.Items(i).SubItems(2).Text)
.Parameters.Add("@DiscountPerUnit", SqlDbType.Float).Value = CSng(lsvCarpartsList.Items(i).SubItems(9).Text)
.Parameters.Add("@TotalDiscount", SqlDbType.Float).Value = CSng(lsvCarpartsList.Items(i).SubItems(5).Text)
.Parameters.Add("@Total", SqlDbType.Float).Value = CSng(lsvCarpartsList.Items(i).SubItems(6).Text)
.Parameters.Add("@lsReceivegAll", SqlDbType.NVarChar).Value = "0"
.ExecuteNonQuery()
End With
sb.Remove(0, sb.Length())
sb.Append("SELECT Car_PartsID,CarpartsInOrder")
sb.Append(" FROM CarpartOnStock")
sb.Append(" WHERE (Car_PartsID=@Car_PartsID)")
sqlSave = sb.ToString()
With com
.Parameters.Clear()
.CommandText = sqlSave
.Parameters.Add("@Car_PartsID", SqlDbType.NVarChar).Value = CProductID
Using dr As System.Data.SqlClient.SqlDataReader = .ExecuteReader()
'dr = .ExecuteReader()
If dr.HasRows Then
CMode = "EDIT"
dr.Read()
OldProductInOrder = CInt(dr(2))
sb.Remove(0, sb.Length())
sb.Append("UPDATE CarpartOnStock")
sb.Append(" SET CarpartsInOrder=@CarpartsInOrder")
sb.Append(" WHERE (Car_PartsID=@Car_PartsID)")
Else
CMode = "ADD"
OldProductInOrder = 0
sb.Remove(0, sb.Length())
sb.Append("INSERT INTO CarpartOnStock(Car_PartsID,")
sb.Append(" UnitID,QuantityWithUnit,RealQuantity,CarpartsInOrder,")
sb.Append(" LowlimitToOrder,LastAccessDate)")
sb.Append(" VALUES (@Car_PartsID,")
sb.Append(" @UnitID,@QuantityWithUnit,@RealQuantity,@CarpartsInOrder,")
sb.Append(" @LowLimitToOrder,@LastAccessDate)")
End If
dr.Close()
End Using
End With
sqlSave = sb.ToString()
With com
.Parameters.Clear()
.CommandText = sqlSave
.Parameters.Add("@Car_PartsID", SqlDbType.NVarChar).Value = CProductID
NewProductInOrder = CInt(lsvCarpartsList.Items(i).SubItems(3).Text) * CInt(lsvCarpartsList.Items(i).SubItems(8).Text)
.Parameters.Add("@CarpartsInOrder", SqlDbType.Int).Value = OldProductInOrder + NewProductInOrder
If CMode = "ADD" Then
.Parameters.Add("@UnitID", SqlDbType.NVarChar).Value = CStr(lsvCarpartsList.Items(i).SubItems(7).Text)
.Parameters.Add("@QuantityWithUnit", SqlDbType.Float).Value = 0
.Parameters.Add("@RealQuantity", SqlDbType.Int).Value = 0
.Parameters.Add("@LowLimitToOrder", SqlDbType.Int).Value = 0
.Parameters.Add("@LastAccessDate", SqlDbType.DateTime).Value = Today
End If
.ExecuteNonQuery()
End With
Next
tr.Commit()
prDoc.Print()
ClearPODetails()
ClearTotal()
ClearProductDetails()
ClearProductList()
MessageBox.Show("บันทึกรายการสั่งซื้อสินค้า เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtPONo.Focus()
Catch ex As Exception
tr.Rollback()
MessageBox.Show("ไม่สามารถบันทึกรายการสั่งซื้อได้ เนื่องจาก " & ex.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
cmdSave.Focus()
End Try
End If
End Sub
Tag : .NET, Ms SQL Server 2008, Crystal Report, Win (Windows App), VS 2010 (.NET 4.x)