Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
// below 10 lines convert image into binary data
C#
byte[] FileByte = null;
       //string filename = textBoxImage.Text.Substring(textBoxImage.Text.LastIndexOf("\\") + 1, textBoxImage.Text.Length - (textBoxImage.Text.LastIndexOf("\\") + 1));
       string path = textBoxImage.Text;
       FileStream fs = new FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
       BinaryReader br = new BinaryReader(fs);
       long allbytes = new FileInfo(path).Length;
       FileByte = br.ReadBytes((Int32)allbytes);
       fs.Close();
       fs.Dispose();
       br.Close();
       //Insert into access database
       string connectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\nic-15\My Documents\test.accdb");
       OleDbConnection con = new OleDbConnection(connectionString);

   //  cmd.Connection = con;
       con.Open();
       string query = "insert into arms_records values (thana,licence_number,sanction_date,licencee_name,father_name,dob,religion,caste,address,village,uid,mobile,email,arms_category,type_of_arm,no_of_arms,arms_number,valid_area,renewal_date,remark,image)"
      + " values (@thana, @lno, @sdate, @lname, @fname, @dob, @religion, @caste, @address, @village, @uid, @mobile, @email, @armscat, @toarm, @narms, @ano, @varea, @rdate, @remark, @image)";
       OleDbCommand cmd = new OleDbCommand(query, con);
       cmd.Parameters.AddWithValue("@thana", comboBoxthana.SelectedItem);
      // (@thana, @lno, @sdate, @lname, @fname, @dob, @religion, @caste, @address, @village, @uid, @mobile, @email, @armscat, @toarm, @narms, @ano, @varea, @rdate, @remark, @image)
       cmd.Parameters.AddWithValue("@lno", textBoxLicence_number.Text);
       cmd.Parameters.AddWithValue("@sdate", dateTimePicker1.Value);
       cmd.Parameters.AddWithValue("@lname", textBoxLicencee_Name.Text );
       cmd.Parameters.AddWithValue("@fname", textBoxFatherName.Text);
        cmd.Parameters.AddWithValue("@dob", dateTimePickerDob.Value);
        cmd.Parameters.AddWithValue("@religion", comboBoxReligion.SelectedItem);
        cmd.Parameters.AddWithValue("@caste", comboBoxCaste.SelectedItem);
        cmd.Parameters.AddWithValue("@address", textBoxAdress.Text);
        cmd.Parameters.AddWithValue("@village", textBoxvillage.Text);
        cmd.Parameters.AddWithValue("@uid", textBoxUid.Text);
        cmd.Parameters.AddWithValue("@mobile", textBox1Mobile.Text);
        cmd.Parameters.AddWithValue("@email", textBoxemail.Text);
        cmd.Parameters.AddWithValue("@armscat", comboBoxArmsCategory.SelectedItem);
        cmd.Parameters.AddWithValue("@toarm", comboBoxtypeofarm.SelectedItem);
        cmd.Parameters.AddWithValue("@narms", textBoxNoOfArm.Text);
        cmd.Parameters.AddWithValue("@ano", textBoxArmsNo.Text);
        cmd.Parameters.AddWithValue("@varea", comboBoxValidArea.SelectedItem);
        cmd.Parameters.AddWithValue("@rdate", dateTimePickerRenewDate.Value);
        cmd.Parameters.AddWithValue("@remark", textBoxRemark.Text);
        cmd.Parameters.AddWithValue("@image", FileByte );
       //cmd.Parameters.AddWithValue("@Im", fname); //alter as per your requirement
       //cmd.Parameters.AddWithValue("@Img", FileBytes);
       cmd.ExecuteNonQuery();
       con.Close();
       MessageBox.Show("Image insert successfully", "Insert Image into Access Database");
Posted
Updated 25-Oct-13 20:00pm
v2

1 solution

You have the word "VALUES" in your SQL twice:
 string query = "insert into arms_records values (...)"
+ " values (...)";
take out the first one, so it is the column names list instead of a set of values.
 
Share this answer
 
Comments
Member 10358986 26-Oct-13 4:12am    
Thank you sir
i have removed first "Value" from sql command but now another is there and that is
"Syntax error in INSERT INTO statement."
OriginalGriff 26-Oct-13 4:21am    
It may be your choice of column name: "image" may be causing confusion, so try surrounding it with '[' and ']' characters:
string query = "insert into arms_records values (..., [image])"
+ " values (...)";
It that doesn't fix it, copy and paste the code you are now using in case you accidentally inserted or removed the wrong character.
Member 10358986 26-Oct-13 5:47am    
Its working Sir.
thank you very much
OriginalGriff 26-Oct-13 6:05am    
You're welcome!
Member 10358986 28-Oct-13 8:08am    
Hello Sir
what should be the UPDATE query for the above parameters.

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