Click here to Skip to main content
15,910,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void btnSave_Click(object sender, EventArgs e)
        {
            //Sale table

            {
                string str = "INSERT INTO Sales (Invoice,CustomerName,SaleDate,TotalAmt,VAT,Discount,TotalPayAmt,Paid,Balance) VALUES ("+ Invoice_tx.Text +", '"+ CustomerName_cb.Text +"', '"+ SaleDate_dt.Value.Date.ToString() +"', "+ TotalAmnt_tx.Text +", "+ VAT_tx.Text +", "+ Discount_tx.Text +", "+ TotalPayAmnt_tx.Text +",​​​ "+ Paid_tx.Text +", "+ Balance_tx.Text +") ";

                OleDbDataAdapter da = new OleDbDataAdapter(str, conn);               
                //DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                da.Fill(dt);
                
            }
            //SaleItem table

            for (int i = 0; i < metroGrid1.Rows.Count; i++)
            {
                string insertPur = "INSERT INTO SaleItems (Invoice,Pid,Product,Qty,Rate,Amount) VALUES ( " + Invoice_tx.Text + ", " + metroGrid1.Rows[i].Cells["Pid"].Value.ToString() + ", '" + metroGrid1.Rows[i].Cells["Product"].Value.ToString() + "',  " + metroGrid1.Rows[i].Cells["Qty"].Value.ToString() + ", " + metroGrid1.Rows[i].Cells["Rate"].Value.ToString() + ", " + metroGrid1.Rows[i].Cells["Amount"].Value.ToString() + ") ";

                OleDbDataAdapter da = new OleDbDataAdapter(insertPur, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);

            }
            //Stock Table
            {

                for (int i = 0; i < metroGrid1.Rows.Count; i++)
                {
                    OleDbDataAdapter da1 = new OleDbDataAdapter("SELECT * FROM Stock WHERE Pid= " + metroGrid1.Rows[i].Cells["Pid"].Value.ToString() + " AND Product= '" + metroGrid1.Rows[i].Cells["Product"].Value.ToString() + "'  ", conn);
                    DataSet ds1 = new DataSet();
                    da1.Fill(ds1);

                    if (ds1.Tables[0].Rows.Count > 0)

                    {

                        //When Item exist in stock!

                        string insertPur = "UPDATE Stock SET Qty= Qty- " + metroGrid1.Rows[i].Cells["Qty"].Value.ToString() + " Where Pid=" + metroGrid1.Rows[i].Cells["Pid"].Value.ToString() + " ";

                        OleDbDataAdapter da = new OleDbDataAdapter(insertPur, conn);
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                    }
                    else
                    {
                        //When Item doesn't exist in stock!

                        string insertPur = "INSERT INTO Stock (Pid,Product,Qty,Rate,Amount) VALUES (" + metroGrid1.Rows[i].Cells["Pid"].Value.ToString() + ", '" + metroGrid1.Rows[i].Cells["Product"].Value.ToString() + "', " + metroGrid1.Rows[i].Cells["Qty"].Value.ToString() + ", " + metroGrid1.Rows[i].Cells["Rate"].Value.ToString() + ",  " + metroGrid1.Rows[i].Cells["Amount"].Value.ToString() + " ) ";

                        OleDbDataAdapter da = new OleDbDataAdapter(insertPur, conn);
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                    }

                }
            }

            MessageBox.Show("The Record Saved Successfully!", "SAVE", MessageBoxButtons.OK, MessageBoxIcon.Information);

            Clear();
            ClearItems();       
            metroGrid1.Rows.Clear();
            NewInvoice();


        }


What I have tried:

I had tried to execute the app many times but still not work it shows the message the syntax error (missing operator) I used MS Access Database and Visual Studio 2019, please help me how to resolve this problem
Posted
Updated 8-Apr-20 22:56pm
v2

Quote:
it shows the message the syntax error (missing operator)

Give exact error message, it usually include the filename and line number where is the error.
C#
string str = "INSERT INTO Sales (Invoice,CustomerName,SaleDate,TotalAmt,VAT,Discount,TotalPayAmt,Paid,Balance) VALUES ("+ Invoice_tx.Text +", '"+ CustomerName_cb.Text +"', '"+ SaleDate_dt.Value.Date.ToString() +"', "+ TotalAmnt_tx.Text +", "+ VAT_tx.Text +", "+ Discount_tx.Text +", "+ TotalPayAmnt_tx.Text +",​​​ "+ Paid_tx.Text +", "+ Balance_tx.Text +") ";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
v2
Comments
CHill60 9-Apr-20 4:08am    
The irony is that using parameters will probably solve the problem! 5'd
Patrice T 9-Apr-20 4:15am    
Thank you, agreed :)
Not even sure the problem is in SQL without the line number.
CHill60 9-Apr-20 4:26am    
I have the "opportunity" to use MS Access every day in my current role. I'm used to seeing that error :-( It usually means a syntax error in the Access SQL but rarely means that there is really an operator missing :-)
Maciej Los 9-Apr-20 4:45am    
Caroline, this sounds like an answer...
CHill60 9-Apr-20 4:57am    
It's more subtle than that .. see my solution! Weird!
In the code at the very start
C#
string str = "INSERT INTO Sales (Invoice,CustomerName,SaleDate,TotalAmt,VAT,Discount,TotalPayAmt,Paid,Balance) VALUES ("+ Invoice_tx.Text +", '"+ CustomerName_cb.Text +"', '"+ SaleDate_dt.Value.Date.ToString() +"', "+ TotalAmnt_tx.Text +", "+ VAT_tx.Text +", "+ Discount_tx.Text +", "+ TotalPayAmnt_tx.Text +",​​​ "+ Paid_tx.Text +", "+ Balance_tx.Text +") ";
you have a non-printing character in the +", "+ between TotalPayAmnt_tx.Text and Paid_tx.Text.

In fact if I paste that section into, say the VBE in Excel it shows as
TotalPayAmnt_tx.Text +",??? "+ Paid_tx.Text
It's those ??? that are causing your syntax error.

If you follow the advice of Patrice T (@ppolymorphe) in Solution 2, and use Parameterized queries then the problem unfortunately won't go away (which is unusual in these cases) - the solution is to delete that character that looks like a space, but which isn't.

But you really, really should use parameterized queries as well e.g. (untested)
C#
string str = "INSERT INTO Sales (Invoice,CustomerName,SaleDate,TotalAmt,VAT,Discount,TotalPayAmt,Paid,Balance) VALUES (?,?,?,?,?,?,?,?,?)";
OleDbDataAdapter da = null;
// these must be in the right order!
da.SelectCommand.Parameters.Add("invoice",OleDbType.VarChar, 100).Value = Invoice_tx.Text;
//etc
 
Share this answer
 
Comments
Maciej Los 9-Apr-20 5:08am    
5ed!
Patrice T 9-Apr-20 6:55am    
My 5 too.
1) I think you should use Visual Studio to narrow down in which statement your error occurs, then update this question

2) putting SQL together like that is just so passe (dangerous, old, WRONG) even for a boomer like me - and as you've seen, prone to hard to spot errors - have a look at https://stackoverflow.com/questions/9433148/generating-sql-queries-safely-in-c-sharp and https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlparameter?redirectedfrom=MSDN&view=netframework-4.8
 
Share this answer
 

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