Click here to Skip to main content
15,891,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to check tat dlg.Filname have file name available or not?? how to check this i am doing this but it is showing..

C#
using (dlg = new OpenFileDialog())
{
   dlg.Title = "Open Image";
   dlg.Filter = "All Files|*.*";

   if (dlg.ShowDialog() == DialogResult.OK)
   {
      //PictureBox Picture = new PictureBox();
      pictureEdit1.Image = new Bitmap(dlg.FileName);
   }
}


on button i want to get picture path and doing this but it is showing me error when no file is no pic upload object refrence is not set to an instance of an object

C#
if ( dlg.CheckFileExists)
           objProduct.ProductPicture = dlg.FileName.ToString();
Posted
Updated 19-May-14 22:57pm
v5

C#
dlg.CheckFileExists = true;
using (dlg = new OpenFileDialog())
{


Or you can validate as below
C#
if(!string.NullOrEmpty(dlg.FileName) && File.Exist(dlg.FileName)
{
   pictureEdit1.Image = new Bitmap(dlg.FileName);
}

full code:
C#
using (dlg = new OpenFileDialog())
{
   dlg.CheckFileExists = true;
   dlg.Title = "Open Image";
   dlg.Filter = "All Files|*.*";
 
   if (dlg.ShowDialog() == DialogResult.OK)
   {
        if(!string.NullOrEmpty(dlg.FileName) && File.Exist(dlg.FileName)
        {
           pictureEdit1.Image = new Bitmap(dlg.FileName);
        }
   }
}
 
Share this answer
 
v3
Comments
Muhamad Faizan Khan 20-May-14 4:57am    
question updated
DamithSL 20-May-14 5:04am    
check my updated answer
Unless you have changed the CheckFileExists[^] property value - and you haven't - the user can't press OK without a valid, existing file: so the FileName property will always contain a valid existing file name andcx you don't need to check.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900