Click here to Skip to main content
15,916,463 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Sub AddToCart()
        Try
            If txtQty.Text = String.Empty Or txtQty.Text = "0" Then Return
            Dim sdate As String = Now.ToString("yyyy-MM-dd")
            cn.Open()
            cm = New MySqlCommand("INSERT INTO tblcart (invoice, pid, price, qty, sdate, user) VALUES(@invoice, @pid, @price, @qty, @sdate, @user)", cn)
            With FormPOS
                cm.Parameters.AddWithValue("@invoice", .lblInvoice.Text)
                cm.Parameters.AddWithValue("@pid", lblPID.Text)
                cm.Parameters.AddWithValue("@price", CDbl(lblPrice.Text))
                cm.Parameters.AddWithValue("@qty", CInt(txtQty.Text))
                cm.Parameters.AddWithValue("@sdate", sdate)
                cm.Parameters.AddWithValue("@user", strUser)
                cm.ExecuteNonQuery()
                cn.Close()

                cn.Open()
                cm = New MySqlCommand("update tblcart set total = price * qty where invoice like '" & .lblInvoice.Text & "'", cn)
                cm.ExecuteNonQuery()
                cn.Close()
                .txtSearch.Focus()
                .txtSearch.SelectionStart = 0
                .txtSearch.SelectionLength = .txtSearch.Text.Length
                .LoadCart()
            End With
            Me.Dispose()
        Catch ex As Exception
            cn.Close()
            MsgBox(ex.Message, vbCritical)
        End Try
    End Sub


What I have tried:

Can anyone help? i have check all the problems but nothing seems to solve this problem
Posted
Updated 1-Dec-19 5:17am

We can't tell: we don't have access to the rest of your code, or to your data - and probably both will be needed to work out exactly what is going on.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

But ... I'd be very, very worried about any code that does two things wrong like yours does:
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) Using UPDATE with a LIKE condition to update pricing based on a partial invoice number sends shivers down my spine - I've met VAT inspectors and don't want to again, they are not friendly people - and is probably a very dangerous accounting practice ...
 
Share this answer
 
Quote:
Fatal error encounter during command execution

This give us no information on what is the problem.
You need to give us the exact error message and position.
Advice: while while hunting bugs in your code, remove the try/catch from your code.

Your second SQL command is subject to SQL injection but the first is immune.
 
Share this answer
 
can you provide some more information, like full error, error from first ExecuteNonQuery or second executeNonquery... it could be issue of raiseCondition here. may be your first connection is still not closed and you try to open the same connection again..
can you check by having two connection instance for each query?
 
Share this answer
 
v3

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