I am trying to insert record in Access 2003 table using c# 2008 But getting error in code -
Datatype mismatch in criteria...
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);
}
}
}
}
Table structure
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
Amit ChoudharyPosted Feb 28, 2011, 4:25 AM
To remove this error of date/time. Please use parametric query to insert the date using INSERT statement like put @dob there. Create an object and of SqlParameter class and pass the type, value and name of the sql parameter to it.
Hope you got the point.
Please mark "Do you like this answer" if it helps you.
Thanks.
Jaganathan BantheswaranPosted Feb 28, 2011, 2:10 AM
Are you getting error in this much lines of code?.
Please the post the particular lines where you are getting error.