hi

I am getting this error.
I wrote code to save multiple Images...now when I save all the 4 image I get no exception...
but when I try to save only 1 or 2 or 3 its throws an exception Empty Path is not legal.

Little help plz.

Recommended Answers

All 13 Replies

Please post your code. The exception is clear about what the issue is, so you need to look into why the path you're trying to save is interpreted as being empty. Most likely it's getting cleared somewhere.

  1. Have you stepped through with the debugger to see what's happening?
  2. You will need to post some code.
  byte[] img = null;
                    byte[] img1 = null;
                    byte[] img2 = null;
                    byte[] img3 = null;
                    FileStream fs = new FileStream(picLoc, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);

                    FileStream fs1 = new FileStream(picLoc1, FileMode.Open, FileAccess.Read);
                    BinaryReader br1 = new BinaryReader(fs1);

                    FileStream fs2 = new FileStream(picloc2, FileMode.Open, FileAccess.Read);
                    BinaryReader br2 = new BinaryReader(fs2);

                    FileStream fs3 = new FileStream(picloc3, FileMode.Open, FileAccess.Read);
                    BinaryReader br3 = new BinaryReader(fs3);

                    img = br.ReadBytes((int)fs.Length);
                    img1 = br1.ReadBytes((int)fs1.Length);
                    img2 = br2.ReadBytes((int)fs1.Length);
                    img3 = br3.ReadBytes((int)fs3.Length);
                    string sql = "INSERT INTO customer VALUES(" + txt_custoimerId.Text + ",'" + txt_customername.Text + "', '" + txt_address.Text + "'," + txt_mobile.Text + ",'" + txt_ref1.Text + "','" + txt_ref2.Text + "','" + txt_ref3.Text + "','" + txt_refmobile1.Text + "','" + txt_refmobile2.Text + "','" + txt_refmobile3.Text + "'," + cmb_ename.SelectedValue.ToString() + ",@IMG,@IMG1,@IMG2,@IMG3)";
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
                    command = new SqlCommand(sql, conn);
                    command.Parameters.Add(new SqlParameter("@IMG", img));
                    command.Parameters.Add(new SqlParameter("@IMG1", img1));
                    command.Parameters.Add(new SqlParameter("@IMG2", img2));
                    command.Parameters.Add(new SqlParameter("@IMG3", img3));
                    int x = command.ExecuteNonQuery();
                    MessageBox.Show(x.ToString() + " records saved.");
                    conn.Close();
                    //txtEmpID.Text = "";
                    //txtFirstName.Text = "";
                    //txtLastName.Text = "";
                    pictureBox1.Image = null;
                    pictureBox2.Image = null;
                    pictureBox3.Image = null;
                    pictureBox4.Image = null;
                    clear();
                }
                catch (Exception ex)
                {
                    conn.Close();
                    MessageBox.Show(ex.Message);
                }

here is the code...if I leave any pic box empty it throws this exception....

You can't open an empty location with your FileStream.

You could guard with String.IsNullOrEmpty before you try and load anything.

What you should do is redesign this. All of the work you're doing can be done with a single method over multiple iterations, or, by passing the parameters into an array and passing that to the database methods.

So you could have one method called byte[] GetImageBytes(string filePath) which would load the image up. This way you could call it from a loop, or, individually if you had to.

...
List<byte[]> images = new List<byte[]>();

if(!String.IsNullOrWhiteSpace(picLoc))
{
    images.Add(GetImageBytes(picLoc));
}
...

Finally, you should never, ever, ever build up a query string like that.

You could quite easily write a small script into any of the textboxes that will cause your script to drop your customer table and ignore everything else afterwards. you're already using parameters for your images, so I don't know why you didn't for everything else too.

How are you generating picloc, picloc1, picloc2, and picloc3?

    string picLoc = "";
    string picLoc1 = "";
    string picloc2 = "";
    string picloc3 = "";

Well, unless you're changing those values, it's pretty obvious why you're getting an empty path exception. Blank strings are not valid paths.

@decepticon
I just changed it...now I am getting...

Path cannot be null.
Parameter name: path

Okay Ketsuekiame ,I will get back to u....let me see...

Did you change the to this?

string picLoc = null;
string picLoc1 = null;
string picloc2 = null;
string picloc3 = null;

Because that's not any better. If you're opening files, those strings need to have the path to the file. The framework has no idea what file you're trying to open if you don't. For example:

string picLoc = Path.Combine(rootFolder, "myimage.tif");

No I just edited it to

string picloc;

And that represents a valid path, how?

okay I get it...let me check....I am such a dum dum...

@decepticon

First of all thank you very much...
u wre right
this is what I did and my problem is solved
string picLoc = @"d:\Pc Configure\Pc Configure\Resources\stopgate1.png";
string picLoc1 = @"d:\Pc Configure\Pc Configure\Resources\stopgate1.png";
string picloc2 = @"d:\Pc Configure\Pc Configure\Resources\stopgate1.png";
string picloc3 = @"d:\Pc Configure\Pc Configure\Resources\stopgate1.png";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.