0

private void accView_CellClick(object sender, DataGridViewCellEventArgs e)I have an application with a database. It is an inventory application. When the user goes to add an entry to a table they have the option to upload a photo. It is not required. Once they add the new item without a photo successfully then should be able to go to the main form and select the row on the datagridview and it will populate some labels with data based on what is selected.

The problem starts when they select a row without a photo uploaded to the database. It throws an ArgumentException stating that The path is not of a legal form. Below is my code. I am having a hard time getting around this. If a row is selected that does not have a photo, the picture box should just show the default photo...

private void accView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex == -1)
        return;

    if (!accView.Rows[e.RowIndex].IsNewRow)
    {
        //Visual Studio shows the error on the line of code below.
        accPictureBox.Image = Image.FromFile(accView.Rows[e.RowIndex].Cells[10].Value.ToString(), true);
    }
}

Update:

 private void accView_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        string defImage = (new System.Uri(System.Reflection.Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
        string imageDir = Path.GetDirectoryName(defImage);
        string fullPath = Path.Combine(imageDir, @"C:\Users\Brandon\Documents\Visual Studio 2013\Projects\Firearm Asset Management\Firearm Asset Management\bin\Beta\Images\DefaultImage4.jpg");

        var path = accView.Rows[e.RowIndex].Cells[10].Value;

        //Syntax Error on line below for invalid arguments.      
        if (string.IsNullOrEmpty(path))
        {
            //set the default path
            path = fullPath;
        }

        //Syntax Error on line below for invalid arguments.       
        accPictureBox.Image = Image.FromFile(path, true)
    }
LearningCSharp
  • 87
  • 1
  • 2
  • 12

2 Answers2

2

Where there's no image uploaded, then presumably

accView.Rows[e.RowIndex].Cells[10].Value

will be null or an empty string. Therefore, check

    string defImage = (new System.Uri(System.Reflection.Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
    string imageDir = Path.GetDirectoryName(defImage);
    string fullPath = Path.Combine(imageDir, @"./Images/DefaultImage4.jpg");

    string path = string.Empty;

    if (accView.Rows[e.RowIndex].Cells[10].Value != null)
        path = accView.Rows[e.RowIndex].Cells[10].Value.ToString();

    //Syntax Error on line below for invalid arguments.      
    if (string.IsNullOrEmpty(path))
    {
        //set the default path
        path = fullPath;
    }

    //Syntax Error on line below for invalid arguments.       
    accPictureBox.Image = Image.FromFile(path, true)
dyson
  • 866
  • 6
  • 12
  • Thanks barrick. I have the code, but how would I formulate the path for the default image if I am going to have this deploy to another machine via an installer? – LearningCSharp Jul 31 '14 at 17:04
  • Is the default image included in the deployment? If so, you can be certain of its location and [the answer to this](http://stackoverflow.com/questions/1019641/relative-paths-in-winforms) should help you get the correct path to the image file. – dyson Jul 31 '14 at 17:26
  • Thanks I will try this. If I have any problems I will let you know. – LearningCSharp Jul 31 '14 at 17:58
  • Barrick, I updated the OP to show some new code I have not placed the absolute path yet (will when I get home from work). I am not 100% sure how to code for my issue. I am sure the syntax I have above has problems... – LearningCSharp Jul 31 '14 at 18:32
  • That looks fine to me, you just need to set `path = fullPath` in the `if` statement – dyson Jul 31 '14 at 21:15
  • Update the code after update. I am getting some errors with the syntax. – LearningCSharp Jul 31 '14 at 23:23
  • This is, I think, because the `path` variable is getting an `object` from the `..Cells[10].Value` call. However, we need to account for the chance that it might be `null`, so instead of the `var path = ...` line, try the edit I'm about to make to the answer. – dyson Jul 31 '14 at 23:33
  • The only issue I see is, when I install this on another machine will that path I have above carry over? Seems like it is hard coded to my development machine. – LearningCSharp Aug 01 '14 at 00:26
  • You should be able to use a relative path, presumably if the executable is in the `C:\...\bin\Beta\` directory, then replace all of that with a `.` in the `Path.Combine()` call above, as per the edit, then as long as the `Images` directory sits at the same level as the .exe, then it doesn't matter what the full path is. – dyson Aug 01 '14 at 06:38
0

I imagine that cell[10] is where the image path should be. However, if the user has not uploaded an image you will not have a path.

I would first check the value of cell[10] and if the value is a properly formed path then I would assign it to accPictureBox.Image

MikeV
  • 585
  • 3
  • 11