Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When iam trying to insert data into Access Database at that time iam showing OleDbException was unhandled actually picturebox5 and picturebox4 image not inserting

values are showing :

Syntax error (missing operator) in query expression


{"Syntax error (missing operator) in query expression '2','Atul','Wadala','24043410','collected','12/12/2015','2','12/01/2016','Active','500','50','2','100.00','550.00','@pictureBox6'."}

What I have tried:

insert code is
C#
cmd = new OleDbCommand("insert into RegistrationForm(ClientCount,Name,Address,Contact,Documents,Money_Taking_Date,Muddat,Money_Return_date,Account_status,Taking_Amout,Interest_per_month,Pending_interest_month,Pending_interst_Amount,Total_Amount,Client_image,Document_image1,Document_image2) values(" + lblcount.Text + "','" + textBox20.Text + "','" + textBox21.Text + "','" + textBox19.Text + "','" + textBox18.Text + "','" + maskedTextBox1.Text.ToString() + "','" + textBox22.Text + "','" + maskedTextBox2.Text.ToString() + "','" + textBox23.Text + "','" + textBox17.Text + "','" + textBox16.Text + "','" + textBox15.Text + "','" + textBox13.Text + "','" + textBox14.Text + "','@pictureBox6,@pictureBox4,@pictureBox5)'", con);
            conv_photo();
            //cmd.Parameters.AddWithValue("@pictureBox6", pictureBox1.Text);
            con.Open();
            int n = cmd.ExecuteNonQuery();
            con.Close();
            if (n > 0)
            {
                MessageBox.Show("record inserted");
                loaddata();
                rno++;
            }
            else
                MessageBox.Show("insertion failed");

loaddata method code is :
C#
public void loaddata()
      {

          adapter = new OleDbDataAdapter("select * from RegistrationForm", con);
           ds = new DataSet();//student-> table name in stud.accdb/stud.mdb file
           adapter.Fill(ds, "RegistrationForm");
           ds.Tables[0].Constraints.Add("ClientCount", ds.Tables[0].Columns[0], true);
      }
Posted
Updated 7-Feb-16 4:21am
v4
Comments
PIEBALDconsult 7-Feb-16 10:18am    
I suspect it's due to the apostrophes around '@pictureBox6' I suggest you remove them.
You need to learn to use parameters, every time, especially when using data from users.

1 solution

Don't do it like that!
Do not 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. Use Parametrized queries instead.
The chances are that not only will your code be more readable, your database be a lot safer, but your problem will go away at the same time.
Try something like:
C#
cmd = new OleDbCommand("INSERT INTO RegistrationForm (ClientCount) VALUES (@CC)", con);
cmd.Parameters.AddWithValue("@CC", lblcount.Text);

But if those last are images, then you are going to have to do more than that anyway: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] shows you what happens when you do that, and what to do to fix it.
 
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