0
votes

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)
    }
2

2 Answers

2
votes

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)
0
votes

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