Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This code reduces the quantities of a particular item when it is sold, but I adjusted the schedule to have validity dates and I want it to reduce the quantities according to the oldest date and then delete it if the quantity is equal to zero and then reduce the latest date


What I have tried:

For Each r As DataGridViewRow In dgvprodac.Rows
            Dim ad As Integer = Val(r.Cells(0).Value)
            Dim txP As String = Val(r.Cells(1).Value)
            Dim xt As Single = Val(r.Cells(2).Value)
            Dim txC As Integer = Val(r.Cells(3).Value)
            cmd.CommandText = String.Format(" UPDATE [store] SET [sanf_kem] = sanf_kem -{0}  WHERE [ID_sanf] = {1} and  [date_ex] as MinDate ", xt, ad)
            cmd.ExecuteNonQuery()
        Next
Posted
Updated 19-Jul-20 7:33am

Don't do it like that! 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?

When you have fixed that through your whole app, tell us what have you tried to do what you wanted, and what happened when you did.
 
Share this answer
 
This code is closer to the solution for me, but a mistake can be modified

sqlstr = "Select min(date_ex),sanf_kem from store where ID_sanf =@pass"
       cmd = New OleDbCommand(sqlstr, con)
       cmd.Parameters.Add(New OleDbParameter("@pass", ad))
       con.Open()
       Dim dr As OleDb.OleDbDataReader = cmd.ExecuteScalar()
       If dr.Read() Then
           DateTimePicker1.Value = dr.Item(0)
           TextBox5.Text = dr.Item(1)
       End If
 
Share this answer
 
v2

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