0

how do i set a default image when the image file was not found or not exist ? the error i got was

enter image description here

because file with name 8813.jgp was not exist. and i want to set it default as 1.jpg. how do i check if its exist or not.

iam using this code

 public void profilepicture()
    {
    DataTable dtprofile = new DataTable();
    DataSet dsprofile = new DataSet();
    string path;
        SqlCommand command = new SqlCommand();
        SqlDataAdapter adapter = new SqlDataAdapter();
        try
        {
            dsprofile.Clear();
            dtprofile.Clear();
            command.Connection = myConnection;
            command.CommandText = "//some query//'";

            myConnection.Open();
            adapter.SelectCommand = command;
            adapter.Fill(dtprofile);
            adapter.Fill(dsprofile);
        }
        catch (Exception ex)
        {
            MessageBox.Show("error" + ex);
            myConnection.Close();
        }

        path = dsprofile.Tables[0].Rows[0][0].ToString() + "\\" + number + ".jpg";
        pictureEdit2.Image = Image.FromFile(path);

        myConnection.Close();
    }
chopperfield
  • 559
  • 1
  • 7
  • 25

4 Answers4

2

You can check to see if the file exists using File.Exists() in the System.IO namespace.

https://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx

if(File.Exists("filePath"))
{
    // Load file.
}
else
{
    // Load default file.
}
Bojan B
  • 2,091
  • 4
  • 18
  • 26
tphx
  • 96
  • 1
  • 4
0

Its quite trivial actually!

path = dsprofile.Tables[0].Rows[0][0].ToString() + "\\" + number + ".jpg";
if(File.Exists(path)){
    pictureEdit2.Image = Image.FromFile(path);
}
else
{
    pictureEdit2.Image = Image.FromFile("NotFound.jpg");
}
  • well, i dont know, a system.io of `file.exists`. chill ~ – chopperfield Sep 28 '16 at 06:51
  • Include the namespace for System.IO using System.IO; it exists in the Assembly mscorlib.dll which i am sure you would have already! –  Sep 28 '16 at 09:06
0

I suggest you to create a separate method to get the SQL Table, so you can use it in every method you need and then you create a different method for the profile picture using the code as below:

public DataTable GetSqlTable(string query)
{
    using (SqlConnection dbConnection = new SqlConnection(@"Data Source={ServerName};Initial Catalog={DB_Name};UID={UserID}; Password={PWD}"))
    {
        dbConnection.Open();
        SqlDataAdapter da = new SqlDataAdapter(query, dbConnection);
        da.SelectCommand.CommandTimeout = 600000; //optional
        try
        {
            DataTable dt = new DataTable();
            da.Fill(dt);
            da.Update(dt);
            dbConnection.Close();
            return dt;
        }
        catch
        {
            dbConnection.Close();
            return null;
        }
    }
}

public void profilepicture()
{
    DataTable dt = GetSqlTable("/* some query */");
    string number = "some value";
    string defaultImg = "defaultImgPath";
    if(dt.Rows.Count > 0)
    {
        string path = dt.Rows[0][0].ToString() + "\\" + number + ".jpg";
        pictureEdit2.Image = Image.FromFile(File.Exists(path) ? path : defaultImg);
    }
    else
    {
        pictureEdit2.Image = Image.FromFile(defaultImg);
    }
}

Sorry for my English

edisonmecaj
  • 1,062
  • 1
  • 9
  • 20
0

Standalone applications

If your application is a standalone application (requires only exe and no other files in hard disk), you can add the default image in a resource file and use it as required. Following stack overflow link explains how to access images added into a resource file Load image from resources area of project in C#

pictureEdit2.Image = File.Exists(path) ? Image.FromFile(path) : Resources.myImage;

Installed applications

If your application is not a standalone application, you can use Environment.GetFolderPath method or Application.StartupPath to store the default image.

To avoid file not found exception, you may need to use File.Exists in your code something like below

string defaultImagePath = Application.StartupPath + "\\1.jpg";
pictureEdit2.Image = Image.FromFile( File.Exists(path) ? path : defaultImagePath) ;
Community
  • 1
  • 1
Kira
  • 1,403
  • 1
  • 17
  • 46