Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:sigh:
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)
                {
                    //pictureBox1.Visible = true;
                    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).
Posted
Comments
Manfred Rudolf Bihy 1-Jan-11 8:34am    
Hello Vishal, I've updated my answer to reflect some nescessary changes, pleas have a look and try it out.

1 solution

I have an issue with the output supposedly generated by your program. If an exception is thrown after "Cov2" string has been written to console it would be caught by the try catch in method conv_photo and after that the flow of control would be after the try catch block thus effectively returning from method conv_photo.
In your (I would guess simulated) output the method seems to continue with its execution which is impossible as per above argumentation.

Modification:
The OLE DB .NET Provider does not support named parameters. When CommandType is set to Text the question mark (?) placeholder must be used.

For example:
C#
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 + "', ? )"; //changed @photo to ?


In conv_photo
C#
// Adding the parameters will have to be in the same order as they appear in the query
command.Parameters.Add(photo_array, SqlDbType.Binary);

End modification

Best Regards,

Manfred
 
Share this answer
 
v4

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