Click here to Skip to main content
15,909,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.IO;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace PIS
{
    public partial class Images : Form
    {
        public static int SelectedPatientId;
        public Images()
        {
            InitializeComponent();
            if (i == 0)
                btnShowImage_Click(null, null);
        }
            DataSet ds;
            int i = 0;
            
       
       

        private void Images_Load(object sender, EventArgs e)
        {

            txtPatientIdImage.Text = val.ToString();

            txtPatientIdImage.Text = val.ToString();

            string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\PIS(ACU).mdb;";
            Con = new OleDbConnection(@constr);
            Con.Open();
            Com = new OleDbCommand();
            Com.Connection = Con;
            OleDbDataAdapter da = new OleDbDataAdapter("SELECT Photo FROM PatientImages WHERE Patient_Id =  " + val + " ", Con);
            //Initialize dataset
            ds = new DataSet();
            //Fill DataSet using query defined for DataAdapter
            da.Fill(ds);
            //da.Fill(ds, "PatientVisitation ");
                      
        }

        private void groupBoxImage_Enter(object sender, EventArgs e)
        {

        }

        private void pictureBoxForImage_Click(object sender, EventArgs e)
        {
            
        }

        private void btnNextImage_Click(object sender, EventArgs e)
        {
          //  if (ds != null && ds.Tables[0].Count > 0)

            if (i < ds.Tables[0].Rows.Count - 1)
            {
                i++;

                byte[] picbyte = ds.Tables[0].Rows[i]["Photo"] as byte[] ?? null;
                if (picbyte != null)
                {
                    MemoryStream mstream = new MemoryStream(picbyte);
                    pictureBoxForImage.Image = System.Drawing.Image.FromStream(mstream);
                    {
                        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(mstream);
                    }
                }
               // txtPhotoName.Text = ds.Tables[0].Rows[i]["ImageName"].ToString();
            }
            else
            {
                MessageBox.Show("Next Image not available");
            }
            
           }


        private void btnPreviewImage_Click(object sender, EventArgs e)
        {
           
            if (i == ds.Tables[0].Rows.Count - 1 || i != 0)
            {
                i--;
                byte[] picbyte = ds.Tables[0].Rows[i]["Photo"] as byte[] ?? null;
                if (picbyte != null)
                {
                    MemoryStream mstream = new MemoryStream(picbyte);
                    pictureBoxForImage.Image = System.Drawing.Image.FromStream(mstream);
                    {
                        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(mstream);
                    }
                }
              
            }
            else
            {
                MessageBox.Show("Previous Image not available");
            }
           
        }

       
        private void btnShowImage_Click(object sender, EventArgs e)
        {
            string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\PIS(ACU).mdb;";
            Con = new OleDbConnection(@constr);
            Con.Open();
            Com = new OleDbCommand();
            Com.Connection = Con;
        
            Com.CommandText = "SELECT Photo FROM PatientImages WHERE Patient_Id =  " + val + " ";
            OleDbDataReader reader = Com.ExecuteReader();
            if (reader.Read())
                     {
                byte[] picbyte = reader["Photo"] as byte[] ?? null;
                if (picbyte != null)
                {
                    MemoryStream mstream = new MemoryStream(picbyte);
                    pictureBoxForImage.Image = System.Drawing.Image.FromStream(mstream);
                    pictureBoxForImage.SizeMode = PictureBoxSizeMode.StretchImage;
                    {
                        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(mstream);
                    }
                }
            }
         }




        }
    }
Posted
Updated 10-Oct-13 23:31pm
v3
Comments
Thanks7872 10-Oct-13 7:37am    
Post the smallest possible snippet of your code. Remove unnecessary code and point out the line where you got this error. Use Improve question at bottom of your question.
[no name] 10-Oct-13 9:29am    
ok.

Hey there,

You need to check whether the DataSet contains anything before using it.
In your case the DataSet ds is probably null, which is causing the Exception.
Wrap:
C#
if (i < ds.Tables[0].Rows.Count - 1){//your code}

in
C#
if(ds != null && ds.Tables[0].Count > 0){}


You'll need to do the same in other places in your code as where there is a chance of DataSet being null.

It seems you haven't really created the object of DataSet ds; in your code. So, Make sure you fill it before you can use it.

Let me know if it helps.

Azee...
 
Share this answer
 
v2
Comments
thatraja 10-Oct-13 7:49am    
Spot on, 5!
Azee 10-Oct-13 7:54am    
:)
[no name] 10-Oct-13 9:28am    
Thnx.. Now it is done..
Azee 10-Oct-13 13:20pm    
good, mark as solved. :)
Hello Shail.
This error usually means that you're trying to use an object which requires initialization but is not initialized in your code.
Please debug (F11 in Visual Studio)
 
Share this answer
 
Comments
[no name] 10-Oct-13 9:28am    
thnx.
Change your code as given in the following. Try to execute.
C#
private void Images_Load(object sender, EventArgs e)
        {
            txtPatientIdImage.Text = val.ToString();

            string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\PIS(ACU).mdb;";
            Con = new OleDbConnection(@constr);
            Con.Open();
            Com = new OleDbCommand();
            Com.Connection = Con;
          OleDbDataAdapter da = new OleDbDataAdapter("SELECT Photo FROM PatientImages WHERE Patient_Id =  " + val + " ", con);
            //Initialize you dataset
            ds = new DataSet();
            //Fill DataSet using query defined for DataAdapter
            da.Fill(ds, "Patient Details");
        }


for more information refer this link
http://msdn.microsoft.com/en-us/library/aa288452(v=vs.71).aspx[^]
 
Share this answer
 
Comments
[no name] 10-Oct-13 9:27am    
Thnx.. Now it is working..

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