i am try to update datagridview base on the comb box selecting the bill no base on the value datagridview is fill on update button click it should update getting updated wrong values "SrNo updating into 19,20,21 and itemno. updating 1,2,3"
Private Sub dgvOBillItems_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles dgvOBillItems.RowPostPaint
dgvOBillItems.Rows(e.RowIndex).Cells(5).ReadOnly = True
dgvOBillItems.Rows(e.RowIndex).Cells(5).Value = (e.RowIndex + 1).ToString()
dgvOBillItems.Rows(e.RowIndex).Cells(17).ReadOnly = True
dgvOBillItems.Rows(e.RowIndex).Cells(17).Value = (e.RowIndex + 1).ToString()
End Sub
Private Sub UpdateBillData2()
If con.State = ConnectionState.Open Or con1.State = ConnectionState.Open Then
con.Close()
con1.Close()
End If
Call QRCoderGen3()
Dim ms = New MemoryStream()
PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Dim img1() As Byte
img1 = ms.ToArray
Dim i As Integer = 0
Dim j As Integer = 0
Dim rs1, rs2, rs3, rs4 As String
If cmbCompName.Text = Nothing Or txtRoundOff.Text = Nothing Or dgvOBillItems.RowCount = 0 Then
MsgBox("Can't Save Incomplete Information", MsgBoxStyle.Critical, Me.Text)
Exit Sub
End If
Try
If cmbBillNo.SelectedIndex = -1 Then
MessageBox.Show("Please select a Bill Number first!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
Dim billNo As String = cmbBillNo.SelectedItem.ToString()
rs3 = "Update BillInfo Set CompID = @CompID, compname = @compname, add1 = @add1, vendorcode = @vendorcode, itemtype = @itemtype, Date1 = @Date1, statename = @statename, statetine = @statetine" +
",gstno = @gstno,revchrg = @revchrg,vechicleno = @vechicleno,challno = @challno,challdt = @challdt,yuchno = @yuchno,yuchdt = @yuchdt,pono = @pono" +
",podt = @podt,jwino = @jwino,jwidt = @jwidt,img = @img,irnno = @irnno,TaxSubTot = @TaxSubTot,TaxAmt = @TaxAmt,cgstot = @cgstot" +
",sgsttot = @sgsttot,igsttot = @igsttot,gsttot = @gsttot,gtot = @gtot,roundoff = @roundoff,totpayable = @totpayable,tax3 = @tax3,igst3 = @igst3" +
",cgst3 = @cgst3,sgst3 = @sgst3,gsttot3 = @gsttot3,tax5 = @tax5,igst5 = @igst5,cgst5 = @cgst5,sgst5 = @sgst5,gsttot5 = @gsttot5,tax12 = @tax12" +
",igst12 = @igst12,cgst12 = @cgst12,sgst12 = @sgst12,gsttot12 = @gsttot12,tax18 = @tax18,igst18 = @igst18,cgst18 = @cgst18,sgst18 = @sgst18" +
",gsttot18 = @gsttot18,tax28 = @tax28,igst28 = @igst28,cgst28 = @cgst28,sgst28 = @sgst28,gsttot28 = @gsttot28,tottax = @tottax,totigst = @totigst" +
",totcgst = @totcgst,totsgst = @totsgst,totgst = @totgst,ackno = @ackno,FinancialYr = @FinancialYr,shiptoCompID = @shiptoCompID,shiptocompname = @shiptocompname,shiptocompadd = @shiptocompadd" +
",shiptovendorcode = @shiptovendorcode,shiptostatename = @shiptostatename,shiptostatetin = @shiptostatetin,shiptogstno = @shiptogstno" +
",AmtInWords = @AmtInWords,PaidAmt = @PaidAmt,PaidDate = @PaidDate,PayedStatus = @PayedStatus,CreatedBy = @CreatedBy,EWBNo = @EWBNo,SalesType = @SalesType WHERE billno = @billno"
Dim cmdChall1 As New SqlCommand(rs3, con1)
cmdChall1.Parameters.AddWithValue("@CompID", txtCompID.Text)
cmdChall1.Parameters.AddWithValue("@compname", cmbCompName.Text)
cmdChall1.Parameters.AddWithValue("@add1", txtBCompAdd.Text)
cmdChall1.Parameters.AddWithValue("@vendorcode", txtLVendorCode.Text)
cmdChall1.Parameters.AddWithValue("@itemtype", cmbItemType.Text)
cmdChall1.Parameters.AddWithValue("@date1", dtpBDate.Value.ToString)
cmdChall1.Parameters.AddWithValue("@statename", txtState.Text)
cmdChall1.Parameters.AddWithValue("@statetine", txtStateTin.Text)
cmdChall1.Parameters.AddWithValue("@gstno", txtGSTNO.Text)
cmdChall1.Parameters.AddWithValue("@revchrg", cmbRevChrg.Text)
cmdChall1.Parameters.AddWithValue("@vechicleno", txtLVehicleNo.Text)
cmdChall1.Parameters.AddWithValue("@challno", txtchnolst.Text)
cmdChall1.Parameters.AddWithValue("@challdt", txtchDtLst.Text)
cmdChall1.Parameters.AddWithValue("@yuchno", txtyrchnolst.Text)
cmdChall1.Parameters.AddWithValue("@yuchdt", txtyrchDtLst.Text)
cmdChall1.Parameters.AddWithValue("@pono", txtponolst.Text)
cmdChall1.Parameters.AddWithValue("@podt", txtPODtLst.Text)
cmdChall1.Parameters.AddWithValue("@jwino", txtJWINoLst.Text)
cmdChall1.Parameters.AddWithValue("@jwidt", txtJWIDtLst.Text)
cmdChall1.Parameters.AddWithValue("@IMG", img1)
cmdChall1.Parameters.AddWithValue("@irnno", txtIRNEWB.Text)
cmdChall1.Parameters.AddWithValue("@TaxSubTot", txtITotal.Text)
cmdChall1.Parameters.AddWithValue("@TaxAmt", txtTaxaTot.Text)
cmdChall1.Parameters.AddWithValue("@cgstot", txtCGSTTOT.Text)
cmdChall1.Parameters.AddWithValue("@sgsttot", txtSGSTTOT.Text)
cmdChall1.Parameters.AddWithValue("@igsttot", txtIGSTTOT.Text)
cmdChall1.Parameters.AddWithValue("@gsttot", txtgsttotal.Text)
cmdChall1.Parameters.AddWithValue("@gtot", txtGandTot.Text)
cmdChall1.Parameters.AddWithValue("@roundoff", txtRoundOff.Text)
cmdChall1.Parameters.AddWithValue("@totpayable", txtTotPayable.Text)
cmdChall1.Parameters.AddWithValue("@tax3", txtTaxVal3.Text)
cmdChall1.Parameters.AddWithValue("@igst3", txtigst3.Text)
cmdChall1.Parameters.AddWithValue("@cgst3", txtcgst3.Text)
cmdChall1.Parameters.AddWithValue("@sgst3", txtsgst3.Text)
cmdChall1.Parameters.AddWithValue("@gsttot3", txttotgst3.Text)
cmdChall1.Parameters.AddWithValue("@tax5", txtTaxVal5.Text)
cmdChall1.Parameters.AddWithValue("@igst5", txtigst5.Text)
cmdChall1.Parameters.AddWithValue("@cgst5", txtcgst5.Text)
cmdChall1.Parameters.AddWithValue("@sgst5", txtsgst5.Text)
cmdChall1.Parameters.AddWithValue("@gsttot5", txttotgst5.Text)
cmdChall1.Parameters.AddWithValue("@tax12", txtTaxVal12.Text)
cmdChall1.Parameters.AddWithValue("@igst12", txtigst12.Text)
cmdChall1.Parameters.AddWithValue("@cgst12", txtcgst12.Text)
cmdChall1.Parameters.AddWithValue("@sgst12", txtsgst12.Text)
cmdChall1.Parameters.AddWithValue("@gsttot12", txttotgst12.Text)
cmdChall1.Parameters.AddWithValue("@tax18", txtTaxVal18.Text)
cmdChall1.Parameters.AddWithValue("@igst18", txtigst18.Text)
cmdChall1.Parameters.AddWithValue("@cgst18", txtcgst18.Text)
cmdChall1.Parameters.AddWithValue("@sgst18", txtsgst18.Text)
cmdChall1.Parameters.AddWithValue("@gsttot18", txttotgst18.Text)
cmdChall1.Parameters.AddWithValue("@tax28", txtTaxVal28.Text)
cmdChall1.Parameters.AddWithValue("@igst28", txtigst28.Text)
cmdChall1.Parameters.AddWithValue("@cgst28", txtcgst28.Text)
cmdChall1.Parameters.AddWithValue("@sgst28", txtsgst28.Text)
cmdChall1.Parameters.AddWithValue("@gsttot28", txttotgst28.Text)
cmdChall1.Parameters.AddWithValue("@tottax", txttottaxval.Text)
cmdChall1.Parameters.AddWithValue("@totigst", txttotigst.Text)
cmdChall1.Parameters.AddWithValue("@totcgst", txttotcgst.Text)
cmdChall1.Parameters.AddWithValue("@totsgst", txttotsgst.Text)
cmdChall1.Parameters.AddWithValue("@totgst", txttotgst.Text)
cmdChall1.Parameters.AddWithValue("@ackno", txtAckNoRef.Text)
cmdChall1.Parameters.AddWithValue("@FinancialYr", lbl.Text)
cmdChall1.Parameters.AddWithValue("@shiptoCompID", txtShipCompID.Text)
cmdChall1.Parameters.AddWithValue("shiptocompname", cmbShipCompName.Text)
cmdChall1.Parameters.AddWithValue("shiptocompadd", txtShipCompAdd.Text)
cmdChall1.Parameters.AddWithValue("shiptovendorcode", txtShipVendorCode.Text)
cmdChall1.Parameters.AddWithValue("shiptostatename", txtShipState.Text)
cmdChall1.Parameters.AddWithValue("shiptostatetin", txtShipStateTin.Text)
cmdChall1.Parameters.AddWithValue("shiptogstno", txtShipGSTNO.Text)
cmdChall1.Parameters.AddWithValue("AmtInWords", lblInword.Text)
cmdChall1.Parameters.AddWithValue("PaidAmt", txtPayAmt.Text)
cmdChall1.Parameters.AddWithValue("PaidDate", dtpPayAmtDt.Text)
cmdChall1.Parameters.AddWithValue("PayedStatus", txtPaidStat.Text)
cmdChall1.Parameters.AddWithValue("CreatedBy", Label16.Text)
cmdChall1.Parameters.AddWithValue("EWBNo", txtEWBNo.Text)
cmdChall1.Parameters.AddWithValue("SalesType", cmbSalesType.Text)
cmdChall1.Parameters.AddWithValue("@billno", cmbBillNo.Text)
con1.Open()
cmdChall1.ExecuteNonQuery()
con1.Close()
Using con1 As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("con").ConnectionString)
con1.Open()
For i = 0 To dgvOBillItems.Rows.Count - 1
Dim checkQuery As String = "SELECT COUNT(*) FROM BillItems WHERE ibillno = @ibillno AND itemcode = @itemcode"
Using checkCmd As New SqlCommand(checkQuery, con1)
checkCmd.Parameters.AddWithValue("@ibillno", billNo)
checkCmd.Parameters.AddWithValue("@itemcode", dgvOBillItems.Rows(i).Cells("itemcode").Value)
Dim count As Integer = Convert.ToInt32(checkCmd.ExecuteScalar())
If count > 0 Then
Dim rs2 As String = "UPDATE BillItems SET ibilldt = @ibilldt, ichallid = @ichallid, ichdt = @ichdt, itemno = @itemno, itemname = @itemname, " &
"uom = @uom, price = @price, qty = @qty, hsn = @hsn, batcheat = @batcheat, itotal = @itotal, iyuchno = @iyuchno, iyuchdt = @iyuchdt, " &
"ipono = @ipono, ipodt = @ipodt, ijwino = @ijwino, ijwidt = @ijwidt, gstrate = @gstrate, icompname = @icompname, iitemtype = @iitemtype, " &
"SrNo = @SrNo, iFinancialYr = @iFinancialYr, DiscPer = @DiscPer, DiscValue = @DiscValue WHERE ibillno = @ibillno AND itemcode = @itemcode"
Dim cmdCItem As New SqlCommand(rs2, con1)
If Not String.IsNullOrEmpty(dtpBDate.Value.ToString) Then
cmdCItem.Parameters.AddWithValue("@ibilldt", dtpBDate.Value.ToString)
Else
cmdCItem.Parameters.AddWithValue("@ibilldt", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(16).Value) Then
cmdCItem.Parameters.AddWithValue("@ichallid", dgvOBillItems.Rows(i).Cells(16).Value)
Else
cmdCItem.Parameters.AddWithValue("@ichallid", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(17).Value) Then
cmdCItem.Parameters.AddWithValue("@ichdt", dgvOBillItems.Rows(i).Cells(17).Value)
Else
cmdCItem.Parameters.AddWithValue("@ichdt", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(15).Value) Then
cmdCItem.Parameters.AddWithValue("@itemno", dgvOBillItems.Rows(i).Cells(15).Value)
Else
cmdCItem.Parameters.AddWithValue("@itemno", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(1).Value) Then
cmdCItem.Parameters.AddWithValue("@itemname", dgvOBillItems.Rows(i).Cells(1).Value)
Else
cmdCItem.Parameters.AddWithValue("@itemname", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(5).Value) Then
cmdCItem.Parameters.AddWithValue("@uom", dgvOBillItems.Rows(i).Cells(5).Value)
Else
cmdCItem.Parameters.AddWithValue("@uom", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(2).Value) Then
cmdCItem.Parameters.AddWithValue("@price", dgvOBillItems.Rows(i).Cells(2).Value)
Else
cmdCItem.Parameters.AddWithValue("@price", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(3).Value) Then
cmdCItem.Parameters.AddWithValue("@qty", dgvOBillItems.Rows(i).Cells(3).Value)
Else
cmdCItem.Parameters.AddWithValue("@qty", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(10).Value) Then
cmdCItem.Parameters.AddWithValue("@hsn", dgvOBillItems.Rows(i).Cells(10).Value)
Else
cmdCItem.Parameters.AddWithValue("@hsn", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(11).Value) Then
cmdCItem.Parameters.AddWithValue("@batcheat", dgvOBillItems.Rows(i).Cells(11).Value)
Else
cmdCItem.Parameters.AddWithValue("@batcheat", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(4).Value) Then
cmdCItem.Parameters.AddWithValue("@itotal", dgvOBillItems.Rows(i).Cells(4).Value)
Else
cmdCItem.Parameters.AddWithValue("@itotal", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(6).Value) Then
cmdCItem.Parameters.AddWithValue("@iyuchno", dgvOBillItems.Rows(i).Cells(6).Value)
Else
cmdCItem.Parameters.AddWithValue("@iyuchno", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(7).Value) Then
cmdCItem.Parameters.AddWithValue("@iyuchdt", dgvOBillItems.Rows(i).Cells(7).Value)
Else
cmdCItem.Parameters.AddWithValue("@iyuchdt", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(8).Value) Then
cmdCItem.Parameters.AddWithValue("@ipono", dgvOBillItems.Rows(i).Cells(8).Value)
Else
cmdCItem.Parameters.AddWithValue("@ipono", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(9).Value) Then
cmdCItem.Parameters.AddWithValue("@ipodt", dgvOBillItems.Rows(i).Cells(9).Value)
Else
cmdCItem.Parameters.AddWithValue("@ipodt", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(13).Value) Then
cmdCItem.Parameters.AddWithValue("@ijwino", dgvOBillItems.Rows(i).Cells(13).Value)
Else
cmdCItem.Parameters.AddWithValue("@ijwino", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(14).Value) Then
cmdCItem.Parameters.AddWithValue("@ijwidt", dgvOBillItems.Rows(i).Cells(14).Value)
Else
cmdCItem.Parameters.AddWithValue("@ijwidt", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(20).Value) Then
cmdCItem.Parameters.AddWithValue("@gstrate", dgvOBillItems.Rows(i).Cells(20).Value)
Else
cmdCItem.Parameters.AddWithValue("@gstrate", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(18).Value) Then
cmdCItem.Parameters.AddWithValue("@icompname", dgvOBillItems.Rows(i).Cells(18).Value)
Else
cmdCItem.Parameters.AddWithValue("@icompname", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(12).Value) Then
cmdCItem.Parameters.AddWithValue("@iitemtype", dgvOBillItems.Rows(i).Cells(12).Value)
Else
cmdCItem.Parameters.AddWithValue("@iitemtype", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(19).Value) Then
cmdCItem.Parameters.AddWithValue("@SrNo", dgvOBillItems.Rows(i).Cells(19).Value)
Else
cmdCItem.Parameters.AddWithValue("@SrNo", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(lbl.Text) Then
cmdCItem.Parameters.AddWithValue("@iFinancialYr", lbl.Text)
Else
cmdCItem.Parameters.AddWithValue("@iFinancialYr", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(21).Value) Then
cmdCItem.Parameters.AddWithValue("@DiscPer", dgvOBillItems.Rows(i).Cells(21).Value)
Else
cmdCItem.Parameters.AddWithValue("@DiscPer", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(22).Value) Then
cmdCItem.Parameters.AddWithValue("@DiscValue", dgvOBillItems.Rows(i).Cells(22).Value)
Else
cmdCItem.Parameters.AddWithValue("@DiscValue", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(cmbBillNo.Text) Then
cmdCItem.Parameters.AddWithValue("@ibillno", cmbBillNo.Text)
Else
cmdCItem.Parameters.AddWithValue("@ibillno", DBNull.Value)
Exit For
End If
If Not String.IsNullOrEmpty(dgvOBillItems.Rows(i).Cells(0).Value) Then
cmdCItem.Parameters.AddWithValue("@itemcode", dgvOBillItems.Rows(i).Cells(0).Value)
Else
cmdCItem.Parameters.AddWithValue("@itemcode", DBNull.Value)
Exit For
End If
con1.Open()
cmdCItem.ExecuteNonQuery()
con1.Close()
End If
End Using
End If
Next
UpdateLedgerEntry()
MessageBox.Show("Bill updated successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
btnPrintB.Enabled = True
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub