Hi All
I got lots of help from this site
Here I am facing 1 simple problem but can't point it out, So I need urgent help from you all. I am using Windows 7, VS C# 2008, Access 2007
My Access database have 1 photo column of OLE Object.
Here is the code of C# which is working fine if it work without photo part - After the code Exception which is thrown is mention pls see it All the important section are highlighted
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using System.Drawing;
using System.Drawing.Imaging;
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;
namespace StudentInformation
{
public partial class Form4 : Form
{
OleDbCommand aCommand;
OleDbConnection aConnection;
MemoryStream ms;
//byte[] photo_aray;
String queryStr;
//All Variables are decleared here
//photo
public Form4()
{
InitializeComponent();
aConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\KVSoftware\\KVStudent\\KV1StudentInformation.mdb"); Console.WriteLine("Form4 started");
}
private void button2_Click(object sender, EventArgs e)
{
try
{
*/entered values are stored in local variables for eg. v_admission_no=textBox3.Text; v_student_name=textBox6.Text; v_admitted_class=textBox11.Text; v_admission_date = dateTimePicker1.Value.ToString(); */
queryStr="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)";
Console.WriteLine("query Build");
conv_photo();
aConnection.Open();
aCommand = new OleDbCommand(queryStr, aConnection);
Console.WriteLine("query Executed on Connection");
int check=aCommand.ExecuteNonQuery();
Console.WriteLine("query Executed");
if (check == 1)
{
label1.Visible = true;
}
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
finally
{
MessageBox.Show("Finally of Personal");
aConnection.Close();
}
}
void conv_photo()
{
try
{
//converting photo to binary data if (pictureBox1.Image != null)
{
//using FileStream:(will not work while updating, if image is not changed)
//FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read); //byte[] photo_aray = new byte[fs.Length];
//fs.Read(photo_aray, 0, photo_aray.Length);
//using MemoryStream: Console.WriteLine("Cov1");
ms = new MemoryStream();
Console.WriteLine("Cov2");
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
Console.WriteLine("Cov3");
Console.WriteLine(ms.Length);
byte[] photo_aray = new byte[ms.Length];
Console.WriteLine("Cov4");
ms.Position = 0;
Console.WriteLine("Cov5");
ms.Read(photo_aray, 0, photo_aray.Length);
Console.WriteLine("Cov6");
aCommand.Parameters.AddWithValue("@photo", photo_aray);
Console.WriteLine("Cov7");
}
}
catch (Exception ne)
{
Console.WriteLine("Error: {0}", ne.Message);
}
}
private void tabPage2_Enter(object sender, EventArgs e)
{
label52.Text = v_admission_no; label53.Text = v_student_name;
}
private void button1_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);
}
}
}
}
Exception Part This is the
returned data from Login table
Form4 started
query Build
Cov1
Cov2
A first chance exception of type 'System.NullReferenceException' occurred in StudentInformation.exeA first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Cov3
128755
Cov4
Cov5
Cov6
Error: Object reference not set to an instance of an object.query Executed on Connection
Error: No value given for one or more required parameters.The thread 0x250 has exited with code 0 (0x0).
The thread 0x1598 has exited with code 0 (0x0).
The program '[1480] StudentInformation.vshost.exe: Managed' has exited with code 0 (0x0).