fahad sheikhji

fahad sheikhji

  • NA
  • 53
  • 56.6k

Error while saving image to database using winform c#?

Jul 28 2012 10:18 AM
hi am getting error while clicking save.i uploaded image in that image error is displayed..

here is my image browse code

 private void button3_Click(object sender, EventArgs e)
  {
  OpenFileDialog dlg = new OpenFileDialog();
  DialogResult dlgRes = dlg.ShowDialog();
  if (dlgRes != DialogResult.Cancel)
  {
  pcPhoto.SizeMode = PictureBoxSizeMode.StretchImage;
  //pcPhoto.Image = (Image)new img;
  pcPhoto.ImageLocation = dlg.FileName;
  txtImagePath.Text = dlg.FileName;

  }
  }

this code to read image

 byte[] ReadFile(string sPath)
  {

  byte[] data = null;
  FileInfo fInfo = new FileInfo(sPath);
  long numBytes = fInfo.Length;
  FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
  BinaryReader br = new BinaryReader(fStream);
  data = br.ReadBytes((int)numBytes);
  return data;
  }

this button save click code

  private void button1_Click(object sender, EventArgs e)
  {


  try
  {


  byte[] imageData = ReadFile(txtImagePath.Text);
  string Name = tbName.Text;
  string Mobile = tbMobile.Text;
  string Age = tbAge.Text;
  string JoindDate = tbJoinDate.Text;
  string Sex = tbSex.Text;
  string DOB = tbDateOfBirth.Text;
  string IDProof = tbIDProof.Text;
  string ImagePath = txtImagePath.Text;

  SqlConnection con = new SqlConnection(@"Data Source=FAHADSHEIKHJI\\FAHADSHEIKHJI;Initial Catalog=UAEExchangeShiroor;Integrated Security=True;");
  string qry = "Insert into tblCustomerDetails(cdCustomerName,cdCustomerSex,cdCustomerPhNo,cdCustomerDOB,cdCustomerJoinDate,cdCustomerAge,cdCustomerIDProofNo,cdCustomerPhoto) values(@Name,@CustomerSex,@PhoneNo,@DOB,@JoinDate,@Age,@IdProof,@Image)";

  SqlCommand cmd = new SqlCommand(qry, con);
  cmd.Parameters.Add(new SqlParameter("@Name", (object)tbName.Text));
  cmd.Parameters.Add(new SqlParameter("@CustomerSex", (object)tbSex.Text));
  cmd.Parameters.Add(new SqlParameter("@PhoneNo", (object)tbMobile.Text));
  cmd.Parameters.Add(new SqlParameter("@DOB", (object)tbDateOfBirth.Text));
  cmd.Parameters.Add(new SqlParameter("@JoinDate", (object)tbJoinDate.Text));
  cmd.Parameters.Add(new SqlParameter("@Age", (object)tbAge.Text));

  cmd.Parameters.Add(new SqlParameter("@IdProof", (object)tbIDProof.Text));
  cmd.Parameters.Add(new SqlParameter("@Image", (object)imageData));

  con.Open();//error displaying in this line please help me
  cmd.ExecuteNonQuery();
  con.Close();


  MessageBox.Show("Save Successfully");
 
  tbIDProof.Text = "";
  tbAge.Text = "";
  tbDateOfBirth.Text = "";
  tbJoinDate.Text = "";
  tbMobile.Text = "";
  tbName.Text = "";
  tbSex.Text = "";
  //GetImagesFromDatabase();
  }

  catch (Exception ex)
  {
  MessageBox.Show(ex.ToString());
  }
  }
 
please help me to solve this small error..
thanks in advance

Answers (2)