troubleshooting Question

How to allow null in image upload

Avatar of rtay
rtayFlag for United States of America asked on
C#Microsoft SQL ServerMicrosoft Visual Studio
2 Comments1 Solution160 ViewsLast Modified:
C# Visual Studio WinForm.  Upload Image.  I have 4 possible images able to upload.  Sometimes only one image needs to be uploaded.  Currently, my code works if I upload all 4 images.  If i leave one blank, it errors out with "Empty path name is not legal."  How do I code to allow null path if no image.

Thanks.    


[code]try

            {
                if (frtBillTextBox.Text.Length>0 && maltIDTextBox.Text.Length>0)

                {
                    string FileName = fBImageContentTextBox.Text;
                    byte[] ImageData;
                    fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                    br = new BinaryReader(fs);
                    ImageData = br.ReadBytes((int)fs.Length);
                    br.Close();
                    fs.Close();



                    string FileName2 = sHImageContentTextBox.Text;
                    byte[] ImageData2;
                    fs2 = new FileStream(FileName2, FileMode.Open, FileAccess.Read);
                    br2 = new BinaryReader(fs2);
                    ImageData2 = br2.ReadBytes((int)fs2.Length);
                    br2.Close();
                    fs2.Close();



                    string FileName3 = wCImageContentTextBox.Text;
                    byte[] ImageData3;
                    fs3 = new FileStream(FileName3, FileMode.Open, FileAccess.Read);
                    br3 = new BinaryReader(fs3);
                    ImageData3 = br3.ReadBytes((int)fs3.Length);
                    fs3.Close();
                    br3.Close();


                    string FileName4 = frImageContentTextBox.Text;
                    byte[] ImageData4;
                    fs4 = new FileStream(FileName4, FileMode.Open, FileAccess.Read);
                    br4 = new BinaryReader(fs4);
                    ImageData4 = br4.ReadBytes((int)fs4.Length);
                    fs4.Close();
                    br4.Close();



                    string CmdString = "Update Malt SET FrtBill=@FrtBill,FBImageContent=@FBImageContent,SHImageContent=@SHImageContent,WCImageContent=@WCImageContent,FrimageContent=@FrimageContent Where MaltID = @MaltID";

                    cmd = new SqlCommand(CmdString, conn);

                    cmd.Parameters.Add("@FrtBill", SqlDbType.NVarChar, 50);
                    cmd.Parameters.Add("@FBImageContent", SqlDbType.Image);
                    cmd.Parameters.Add("@SHImageContent", SqlDbType.Image);
                    cmd.Parameters.Add("@MaltID", SqlDbType.Int);
                    cmd.Parameters.Add("@WCImageContent", SqlDbType.Image);
                    cmd.Parameters.Add("@FrimageContent", SqlDbType.Image);
                    cmd.Parameters.Add("@WTag", SqlDbType.NVarChar, 50);
                    //cmd.Parameters.Add("@NetWt", SqlDbType.Int);
                    cmd.Parameters.Add("@Complete", SqlDbType.NVarChar, 50);


                    cmd.Parameters["@FrtBill"].Value = frtBillTextBox.Text;
                    cmd.Parameters["@FBImageContent"].Value = ImageData;
                    cmd.Parameters["@SHImageContent"].Value = ImageData2;
                    cmd.Parameters["@MaltID"].Value = maltIDTextBox.Text;
                    cmd.Parameters["@WCImageContent"].Value = ImageData3;
                    cmd.Parameters["@FrimageContent"].Value = ImageData4;
                    cmd.Parameters["@WTag"].Value = wTagTextBox.Text;
                    //cmd.Parameters["@NetWt"].Value = netWtTextBox.Text;
                    cmd.Parameters["@Complete"].Value = completeTextBox.Text;


                    conn.Open();
                    int RowsAffected = cmd.ExecuteNonQuery();
                    if (RowsAffected > 0)

                    {
                        MessageBox.Show("Image Saved Sucessfully!");
                    }

                    conn.Close();

                    pictureBox1.Image = null;
                    pictureBox2.Image = null;
                    pictureBox3.Image = null;
                    pictureBox4.Image = null;

                }

                else

                {
                    MessageBox.Show("Incomplete Data", "", MessageBoxButtons.OK);
                }

            }

            catch(Exception ex)

            {

                MessageBox.Show(ex.Message);

            }


            finally
            {
                if(conn.State == ConnectionState.Open)
                {

                    conn.Close();

                }
            }
             
            }
ASKER CERTIFIED SOLUTION
ste5an
Senior Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros