Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
VB
Private Sub OK1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
 
    Dim sqlQRY As String = "SELECT * FROM Products WHERE ProductCode = '" & txtCode.Text & "'"
    Dim cmd As OleDbCommand = New OleDbCommand(sqlQRY, conn)
    Dim rdr As OleDbDataReader = cmd.ExecuteReader
 
    If txtCode.Text = "" Then
        MessageBox.Show("Input a product code!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        txtCode.Text = ""
        txtCode.Focus()
    ElseIf rdr.HasRows Then
        rdr.Read()
 
        Dim intProductID As Integer = rdr("ProductID").ToString
 
        Dim newRow As SalesDataSet.SalesDetailsRow = SalesDataSet.SalesDetails.NewSalesDetailsRow
 
        newRow.SalesID = SalesIDTextBox.Text
        newRow.ProductName = intProductID
        newRow.Qty = 1
        Dim Price As Decimal = rdr("Price")
        newRow.Price = Price
        newRow.Amount = Price
 
        SalesDataSet.SalesDetails.Rows.Add(newRow)
 
        txtCode.Text = ""
        txtCode.Focus()
    Else
        MessageBox.Show("Product not found!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        txtCode.Text = ""
        txtCode.Focus()
    End If
 
    txtTotal.Text = Totals()
 
    Dim vat As Decimal
 
    vat = Val(txtTotal.Text) * 0.12
 
    txtVAT.Text = vat
    Button1.Enabled = True
 
End Sub


What I have tried:

I tried to to change " newRow.Qty = 1 " to " newRow.Qty += 1 " but not working !
Please help me for this code. Thanks ! :)
Posted
Updated 22-Jul-16 22:51pm
v2
Comments
Richard MacCutchan 21-Jul-16 7:05am    
You should get the initial quantity from your database record and then add 1 to it. Something like
newRow.Qty = rdr("Quantity").Value + 1;
ionMEMBER 21-Jul-16 7:25am    
When I ok1.performclick() for two times then I show two rows in datagridview.
I want to show one row in datagridview when I click ok1.performclick() for two times but qty in one time is 1 and two times is 2. THANKS
Richard MacCutchan 21-Jul-16 7:35am    
Then you need to change the program logic in your button click method. As it stands, you read a record, and create a new data row. You need to change it so it first checks for an existing row with that product id.

your code should update recor if reader has values and else inserts new record, so your code inside your btnAdd should be exactly as follows:

Dim sqlQRY As String = "SELECT * FROM Products WHERE ProductCode = '" & txtCode.Text & "'"
        Dim cmd As OleDbCommand = New OleDbCommand(sqlQRY, conn)
        Dim rdr As OleDbDataReader = cmd.ExecuteReader

        If rdr.HasRows Then
            rdr.Read()
            Dim r() As DataRow = SalesDataSet.SalesDetailsRow.Select("SalesID = " & rdr("SalesID"))
            r(0)("ProductName") = "exist22"
            r(0)("Qty") += 1
            r(0)("Price") = 10
            r(0)("Amount") = 10

        Else
            Dim newRow As SalesDataSet.SalesDetailsRow = SalesDataSet.SalesDetails.NewSalesDetailsRow
            newRow.SalesID = SalesIDTextBox.Text
            newRow.ProductName = intProductID
            newRow.Qty = 1
            newRow.Price = Price
            newRow.Amount = Price
            SalesDataSet.SalesDetails.Rows.Add(newRow)
            txtCode.Text = ""
            txtCode.Focus()
        End If
        SalesTableAdapter.Update(SalesDataSet)
        SalesDataSet.Worker.AcceptChanges()
 
Share this answer
 
v3
Comments
Richard MacCutchan 22-Jul-16 6:32am    
You should take note of Richard Deeming's comments above.
ionMEMBER 23-Jul-16 3:22am    
THANKS YOU VERY MUCH KHALED EZZART!! :D

This answere it may be correct but I do not understand what is:
For r(0)("ProductName") = "exist22": Index was outside the bounds of the array.
r(0)("Qty") += 1: Index was outside the bounds of the array.
r(0)("Price") = 10: Index was outside the bounds of the array.
r(0)("Amount") = 10: Index was outside the bounds of the array.

Please can you help me for this?
ionMEMBER 23-Jul-16 16:15pm    
THANK YOU VERY MUCH!! :) :) :D

If you have the opportunity to demonstrate in a project and send me LINK (for download) in comment it would be good!
Many thanks anyway. :D
Vb.net
Dim r() As DataRow = SalesDataSet.SalesDetailsRow.Select("SalesID = " & rdr("SalesID"))

The previous code gets rows in the dataset that have the "SalesID" (primary Key) = SalesID retrieved by the reader (rdr("salesID")). And since this is Primary key so r() will get only one row. So r(0) is the only row in the r() collection as it's a zero based index. Finally , as follows you can update each field in this row (r(0)).


r(0)("ProductName") = "exist22"
r(0)("Qty") += 1
r(0)("Price") = 10
r(0)("Amount") = 10


If you still got problems please paste you code here again and state the problem.
 
Share this answer
 
v3
Comments
ionMEMBER 23-Jul-16 16:15pm    
THANK YOU VERY MUCH!! :) :) :D

If you have the opportunity to demonstrate in a project and send me LINK (for download) in comment it would be good!
Many thanks anyway. :D
Vb.net

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900