Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all,

Whenever i try to insert data into Access db its through me syntax error exception, i tried everything but still error is coming

What I have tried:

Insert code:
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 + "','" + Convert.ToDouble(textBox13.Text) + "','" + Convert.ToDouble(textBox14.Text) + "',@pictureBox6,@pictureBox4,@pictureBox5)'", con);
conv_photo();
con.Open();
int n = cmd.ExecuteNonQuery(); -- exception coming on this line
con.Close();
if (n > 0)
{
MessageBox.Show("record inserted");
loaddata();
rno++;
}
else
MessageBox.Show("insertion failed");

conv_photo method:

public void conv_photo()
{
//converting photo to binary data


if (pictureBox6.Image != null)
{
//using MemoryStream:
ms = new MemoryStream();
pictureBox6.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
cmd.Parameters.AddWithValue("@pictureBox6", photo_aray);
}

if (pictureBox4.Image != null)
{
//using MemoryStream:
ms = new MemoryStream();
pictureBox4.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
cmd.Parameters.AddWithValue("@pictureBox4", photo_aray);
}
if (pictureBox5.Image != null)
{
//using MemoryStream:
ms = new MemoryStream();
pictureBox5.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
cmd.Parameters.AddWithValue("@pictureBox5", photo_aray);
}
}

output is :

'2','Atul','india','4589787978','collected','12/05/2015','4','12/06/2015','Active','5000','500','5','2500','7500',@pictureBox6,@pictureBox4,@pictureBox5)'.
Posted
Comments
ZurdoDev 16-Feb-16 13:00pm    
Access needs # around the dates, as I recall.
Atul Rokade 17-Feb-16 0:34am    
how could i right that??? if you dont mind can you give me example please
ZurdoDev 17-Feb-16 7:13am    
Right now you have '12/05/2015'. Look it up, but I think it needs to be #12/05/2015# instead.
Michael_Davies 16-Feb-16 13:04pm    
Also..you add parameters at the end of the insert string (please add all the fields as parameters so much more readable and less prone to error) but you do not add the values for the parameters until after the executenonquery.
[no name] 16-Feb-16 13:11pm    
What is error message exactly you are getting?

Secondly always try use parameterised query to avoid SQL injection as you using partially for 2-3 parameters.

For the umpteen billionth time this question has been asked, Google for "C# parameterized queries" to learn how to fix this little problem.

Also, you might want to Google for "SQL Injection Attack" to find out why using string concatenation to build your SQL statements risks destroying your entire database.
 
Share this answer
 
Comments
Atul Rokade 17-Feb-16 0:43am    
ok sir i got it thank you :) , but having little doubt how i insert image into database? Retrieve? and update
Dave Kreskowiak 17-Feb-16 8:33am    
Again, you're not the first person to do this. It's already been documented all over the Web. Google for "C# insert images into access database".
In addition to what Dave says, have a look here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it shows how to save an image in a DB.
 
Share this answer
 
Comments
Atul Rokade 17-Feb-16 12:27pm    
now exception coming null pointer exception cmd.Parameters.AddWithValue("@pictureBox4", photo_aray); line

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