Click here to Skip to main content
15,911,711 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to save multiple Images now when I save all the 2 images I get no exception... but when I try to save only 1 or 2 or its throws an exception "Empty Path is not legal". my code is here

What I have tried:

C#
byte[] img = null;
  FileStream fs = new FileStream(imgloc, FileMode.Open, FileAccess.Read);
  BinaryReader br = new BinaryReader(fs);
  img = br.ReadBytes((int)fs.Length);

  //

  byte[] img1 = null;
  FileStream fs1 = new FileStream(imgloc1, FileMode.Open, FileAccess.Read);
  BinaryReader br1 = new BinaryReader(fs1);
  img1 = br1.ReadBytes((int)fs1.Length);


  //
  con.Open();
  SqlCommand cmd1 = new SqlCommand("insert into easypaisa (trid,selectopt,smobile,rmobile,snic,rnic,tamount,tcharges,totalcharges,date,descr,inv_no,Company,user_name,sprof,dataimg,image2) values ('" + textBox1.Text + "','" + comboBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + dateTimePicker1.Value.ToString("MM-dd-yyyy") + "','" + textBox10.Text + "','" + textBox9.Text + "','" + comboBox2.Text + "','" + Get_User_label.Text + "','" + textBox13.Text + "',@dataimg,@image2)", con);
  cmd1.Parameters.Add(new SqlParameter("@dataimg", img));
  cmd1.Parameters.Add(new SqlParameter("@image2", img1));
  cmd1.ExecuteNonQuery();
  con.Close();

  MessageBox.Show("Data Saved successfully", "Important Message",
                   MessageBoxButtons.OK, MessageBoxIcon.Information);
Posted
Updated 27-Nov-16 2:17am
v2

1 solution

First of all, you should never concatenate the values directly to the SQL statement. This leaves you open for SQL injections, conversion problems and so on.

What comes to the error itself, try to pinpoint the location where the exception is thrown. Use the debugger and have a look at the values you pass to the methods. For example what do the imgloc and imgloc1 contain.

Another thing is that you should use using blocks and try..catch structures to ensure that you properly dispose the objects and handle the errors correctly. For more information, have a look at Properly executing database operations[^]
 
Share this answer
 
v2
Comments
Member 12861580 28-Nov-16 8:48am    
Sorry I dont Understand What can i do for this Error
Wendelius 28-Nov-16 9:13am    
To get more information about the reason for the exception place a breakpoint on the line

FileStream fs = new FileStream(imgloc, FileMode.Open, FileAccess.Read);

Does imgloc contain a correct value. If it does, repeat the same with

FileStream fs1 = new FileStream(imgloc1, FileMode.Open, FileAccess.Read);

Also investigate the exact row that is causing the exception.

When this is done you know more about the reason for the error and if it is that either of those variables contain invalid path.

When this is fixed concentrate on using parameters for the query and implement the using blocks and the try..catch blocks.

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



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