Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys,

Hope someone can help me. I Have a Openfile dialogue that crashes my application only sometimes as soon as i selected an image. It is doing it on Windows XP and Windows7. I do have a try,catch block in but the program still crashes. If anyone can shed some light on this error, it would be appreciated. Here is my code:

C#
private void loadImageToolStripMenuItem_Click(object sender, EventArgs e)
       {
           try
           {
               Image_Record_Exists = false;
               image_entry_id = 0;
               CAMI_ID = 0;
               OFDImage = new OpenFileDialog();
               OFDImage.Title = "Please select Image.";
               OFDImage.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";

               if (OFDImage.ShowDialog() == DialogResult.OK)
               {
                   try
                   {
                       //check if record exists
                       Reader_String = "";
                       Reader_String = "SELECT CAMI_ID FROM CAM_IMAGES WHERE ((CAMI_COMPNUM = " + Sys_Company + ")AND(CAMI_SITENUM = " + Sys_Site + ")AND(CAMI_BUILDNUM = " + Sys_Building + ")AND(CAMI_CAMNUM = " + Sys_Camera + ")AND(CAMI_ENTRYNUM = " + Sys_Entry + "))";
                       Reader = usedb.ExecuteQuery(Reader_String);

                       while (Reader.Read())
                       {
                           CAMI_ID = (int)Reader["CAMI_ID"];
                           Image_Record_Exists = true;
                       }
                       Reader.Close();

                       Image_New = Image.FromFile(OFDImage.FileName);

                       if (Image_Record_Exists == false) //new record
                       {
                           Reader_String = "";
                           Reader_String = "SELECT MAX(CAMI_ID)FROM CAM_IMAGES";
                           Reader = usedb.ExecuteQuery(Reader_String);

                           if (Reader.Read() == true)
                           {
                               results = Reader[0].ToString();
                               if (results == "")
                               {
                                   image_entry_id = 1;
                               }
                               else
                               {
                                   image_entry_id = Convert.ToInt32(results) + 1;
                               }
                           }
                           Reader.Close();

                           Apt_String = "INSERT INTO CAM_IMAGES(CAMI_ID,CAMI_COMPNUM,CAMI_SITENUM,CAMI_BUILDNUM,CAMI_CAMNUM,CAMI_ENTRYNUM,CAMI_DATE,CAMI_IMGDEVICE)VALUES(" + image_entry_id + "," + Sys_Company + "," + Sys_Site + "," + Sys_Building + "," + Sys_Camera + "," + Sys_Entry + ",'" + DateTime.Now + "',@Image)";
                           Apt = usedb.Image_Insert(Apt_String, Image_New);
                       }
                       else //insert new record
                       {
                           Apt_String = "";
                           Apt_String = "UPDATE CAM_IMAGES SET CAMI_IMGDEVICE = @Image WHERE ((CAMI_COMPNUM = " + Sys_Company + ")AND(CAMI_SITENUM = " + Sys_Site + ")AND(CAMI_BUILDNUM = " + Sys_Building + ")AND(CAMI_CAMNUM = " + Sys_Camera + ")AND(CAMI_ENTRYNUM = " + Sys_Entry + ")AND(CAMI_ID = " + CAMI_ID + "))";
                           Apt = usedb.Image_Insert(Apt_String, Image_New);
                       }
                       PBDevice.Image = Image_New;
                       OFDImage.Dispose();
                   }
                   catch (Exception ex)
                   {
                       Write_Log("An error has occured.\nError: " + Convert.ToString(ex));
                   }
               }
           }
           catch (Exception ex)
           {
               Write_Log("An error has occured.\nError: " + Convert.ToString(ex));
           }
       }
Posted
Comments
OriginalGriff 24-May-12 6:01am    
And what is the exception reporting?
Member 8287316 24-May-12 8:21am    
It doesn't catch an exception. Program gives me not responding windows screen
OriginalGriff 24-May-12 9:59am    
How far does it get? Have you tried stepping through in the debugger?
Member 8287316 24-May-12 10:38am    
It crashes After I pressed ok on the OpenFileDialogue. What I also did was put in MessageBox.Show("step ...") levels after each step in the code and it ran through successfully so it seems to me it is crashing after the process.(after OFDImage.Dispose())
OriginalGriff 24-May-12 11:58am    
You don't need to put MessageBoxes in: put a breakpoint on the line
Reader_String = "";
and execution will stop there. You can then step over (or into) each subsequent instruction, looking at variables as you want.
Just click on the left hand edge of the code window in VS and a red dot will appear to show you where the breakpoint is.
If could be the dispose - but it's worth finding out for sure. If you are sure that it works up to that point, then try an experiment:
Comment out the line
Image_New = Image.FromFile(OFDImage.FileName);
and see if the problem goes away... I don't know if it will, but Image.FromFile does some odd things with files, such as locking them for the lifetime of the Image class instance...

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