Hi Every Body

I am just trying to insert a record in Access 2003 table by code of C#2008
Here is the code -

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Sql;
using System.Globalization;
using System.IO;
using Microsoft.VisualBasic;
using System.Drawing.Imaging;
namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        OleDbCommand aCommand;
        OleDbConnection aConnection;
        MemoryStream ms;
        //byte[] photo_aray;
        String queryStr;
        String v_admission_no; String v_student_name; 
        public Form1()
        {
            InitializeComponent();
            aConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\KVSoftware\\KVStudent\\KV1StudentInformation.mdb");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String v_admitted_class; DateTime v_admission_date;
            String v_present_class; String v_present_section; String v_roll_no; DateTime v_dob; String v_gender;
            String v_cbse_xth_roll; String v_previous_school; String v_conveyance_mode; String v_remark;
            String v_caste; String v_minority_status; String v_house;
            try
            {
                v_admission_no = textBox3.Text;
                v_student_name = textBox6.Text;
                v_admitted_class = comboBox8.SelectedItem.ToString();
                v_admission_date = dateTimePicker1.Value;
                v_present_class = comboBox9.SelectedItem.ToString();
                v_present_section = comboBox10.SelectedItem.ToString();
                v_roll_no = textBox15.Text;
                v_dob = dateTimePicker2.Value;
                Console.WriteLine(v_dob + " " + v_admission_date);
                v_cbse_xth_roll = textBox21.Text;
                v_previous_school = textBox22.Text;
                v_conveyance_mode = comboBox4.SelectedItem.ToString();
                v_remark = textBox1.Text;
                v_gender = comboBox1.SelectedItem.ToString();
                v_caste = comboBox2.SelectedItem.ToString();
                v_minority_status = comboBox7.SelectedItem.ToString();
                v_house = comboBox5.SelectedItem.ToString();
                Console.WriteLine("Check");
                aCommand = new OleDbCommand("INSERT INTO PersonalDetail(admission_no,student_name,admitted_class,admission_date,present_class,present_section,roll_no,dob,gender,caste,minority_status,house,cbse_xth_roll,previous_school,conveyance_mode,remark,photo) VALUES('" + v_admission_no + "','" + v_student_name + "','" + v_admitted_class
                   + "','" + v_admission_date+ "','" + v_present_class + "','" + v_present_section + "','" + v_roll_no + "','"
                   + v_dob+ "','" + v_gender + "','" + v_caste + "','" + v_minority_status + "','" + v_house + "','"
                   + v_cbse_xth_roll + "','" + v_previous_school + "','" + v_conveyance_mode + "','" + v_remark + "',@photo)", aConnection);
                conv_photo();
                aConnection.Open();
                int check = aCommand.ExecuteNonQuery();

                if (check == 1)
                {
                    label1.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
            }
        }
      
        void conv_photo()
        {
            if (pictureBox1.Image != null)
            {
                //using MemoryStream:

                ms = new MemoryStream();
                pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
                byte[] photo_aray = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(photo_aray, 0, photo_aray.Length);
                aCommand.Parameters.AddWithValue("@photo", photo_aray);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*";
            DialogResult res = openFileDialog1.ShowDialog();
            if (res == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
            }
        }
    }
}

And table structure is -
Personal Detail Table
Column Name Data Type Size Description
admission_no text
student_name text
admitted_class text
admission_date date/time
present_class text
present_section text
roll_no text
dob date/time
gender string
caste string
minority_status yes/no
house string
cbse_xth_roll text
previous_school text
conveyance_mode text
remark memo
photo ole object
But I am getting error Datatype mismatch in criteria. Help me somebody...

Thank you all

Line 62: OleDB doesn't allow named paramters. Replace '@photo' with a question mark '?'.

Anf if I remove photo field from qery even then also I am getting same error. I just tested it is from in date filed.

As I found... But I am confused

If you parameterized all your fields the system would format it properly for you.

Thank you very much for kind consideration and suggestion but unfortunately I am new to C# can you please provide me some example of code.

aCommand = new OleDbCommand("INSERT INTO PersonalDetail(admission_no,student_name,admitted_class,admission_date,
present_class,present_section,roll_no,dob,gender,caste,minority_status,
house,cbse_xth_roll,previous_school,conveyance_mode,remark,photo) VALUES
 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", aConnection);

aCommand.Parameters.AddWithValue("@v_admission_no", vadmission_no);
aCommand.Parameters.AddWithValue("@v_student_name", v_student_name);
... etc ... Make sure you add them in the same order as they are listed in the INSERT statement

thank you and does other code will be same

Yes, everything else remains the same.

Edit: I don't use OleDb (ever) so it may require a two step process where you tell it the type of each parameter, then the value. Try this first, though

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.