I have one access database and one windows form. I have some text field and one picture box. I want to Store some data and image into my database, i have written following code to do the task but it says "Syntax error in Insert into statement". I am pasting the C# code that I have written along with the screenshot of the database. Help me in finding the error.

The code that I have written is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace ImageTestCyber
{
    public partial class Form1 : Form
    {
        int flag = 0;
        string currentFile;
        string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\\Cinema 12\\param.accdb;;Jet OLEDB:Database Password=12345";

        public Form1()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            OleDbConnection cn = new OleDbConnection(connectionString);
            string gender = "";

            if (radioBtnMale.Checked == false && radioBtnFemale.Checked == false)
            {
                errorProvider1.SetError(groupBox2, "Select Gender");
                flag = 1;
            }

            if (radioBtnMale.Checked == true)
            {
                gender = "M";
            }

            if (radioBtnFemale.Checked == true)
            {
                gender = "F";
            }

            string date = DateTime.Now.ToShortDateString();
            string InTime = txtInHour.Text + ":" + txtInMin.Text + " " + listBoxIn.SelectedItem.ToString();
            string OutTime = txtOutHour.Text + ":" + txtOutMin.Text + " " + listBoxOut.SelectedItem.ToString();            

            try
            {
                FileStream fs = new FileStream(currentFile, FileMode.OpenOrCreate, FileAccess.Read);
                byte[] rawdata = new byte[fs.Length];
                fs.Read(rawdata, 0, Convert.ToInt32(fs.Length));
                fs.Close();
                string sql = "select * from Customers";
                cn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter(sql, cn);
                OleDbCommandBuilder cmdBldr = new OleDbCommandBuilder(da);
                DataSet ds = new DataSet("Customers");
                da.Fill(ds, "Customers");
                DataRow dr = ds.Tables["Customers"].NewRow();
                dr["Sr_No"] = Convert.ToInt32(txtSrNo.Text);
                dr["PC_No"] = Convert.ToInt32(txtPcNo.Text);
                dr["Names"] = txtName.Text;
                dr["Address"] = txtAddress.Text;
                dr["Contact_No"] = txtContactNo.Text;
                dr["Gender"] = gender;
                dr["Dates"] = date;
                dr["In_Time"] = InTime;
                dr["Out_Time"] = OutTime;
                dr["ID_Proof"] = rawdata;
                dr["Issued_By"] = txtIssuedBy.Text;
                dr["Amt"] = Convert.ToInt32(txtAmount.Text);
                ds.Tables["Customers"].Rows.Add(dr);
                da.Update(ds, "Customers");
                cn.Close();
                MessageBox.Show("Data Saved");                
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                cn.Close();
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog OpenDialogObj = new OpenFileDialog();
            if (OpenDialogObj.ShowDialog() == DialogResult.OK)
            {
                currentFile = OpenDialogObj.FileName;
                txtIdProof.Text = currentFile;
                pictureIDproof.Image = Image.FromFile(OpenDialogObj.FileName);

            }
        }
    }
}

The screenshot of the database is attached

Recommended Answers

All 2 Replies

I don't see an "Insert into" statement in any of the code you posted. Could you post the full error message. It should also provide you with the line number where the error is occurring. Also have you tried walking through it in debug mode to see where the error occurs at?

Please make sure that the customer table must have primary key (sr_no).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.