How to allow null in image upload

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();

                }
            }
             
            }
LVL 5
rtayIT DirectorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MishaProgrammerCommented:
May be you should add small check before you use filename from TextBox?

For example,

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

Open in new window

ste5anSenior DeveloperCommented:
Separation of concerns is your friend.  Then use the disposable pattern. I would write it like this (untested outline):

public class WhatEver
{
    private SqlConnection connection;

    private static byte[] ReadFile(string fileName)
    {
        byte[] result;
        using (fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)
        {
            using (br = new BinaryReader(fs))
            {
                result = br.ReadBytes((int)fs.Length);
            }
        }
    }

    private bool SaveData(int maltID, string frtBill, string wTag, string complete, byte[] fbImage, byte[] shImage, byte[] wcImage, byte[] frImage)
    {
        const string SQL_UPDATE = "UPDATE Malt SET FrtBill=@FrtBill, FBImageContent=@FBImageContent, SHImageContent=@SHImageContent, WCImageContent=@WCImageContent, FrimageContent=@FrimageContent WHERE MaltID = @MaltID;";
        bool result = false;
        using (SqlCommand command = new SqlCommand(CmdString, this.connection))
        {
            cmd.Parameters.Add("@MaltID", SqlDbType.Int);
            cmd.Parameters.Add("@FrtBill", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@WTag", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@Complete", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@FBImageContent", SqlDbType.Image);
            cmd.Parameters.Add("@SHImageContent", SqlDbType.Image);
            cmd.Parameters.Add("@WCImageContent", SqlDbType.Image);
            cmd.Parameters.Add("@FrimageContent", SqlDbType.Image);
            cmd.Parameters["@MaltID"].Value = maltID;
            cmd.Parameters["@FrtBill"].Value = frtBill;
            cmd.Parameters["@WTag"].Value = wTag;
            cmd.Parameters["@Complete"].Value = complete;
            cmd.Parameters["@FBImageContent"].Value = DBNull.Value;
            cmd.Parameters["@SHImageContent"].Value = DBNull.Value;
            cmd.Parameters["@WCImageContent"].Value = DBNull.Value;
            cmd.Parameters["@FrimageContent"].Value = DBNull.Value;
            if (fbImage != null)
            {
              cmd.Parameters["@FBImageContent"].Value = fbImage;
            }

            if (shImage != null)
            {
                cmd.Parameters["@SHImageContent"].Value = shImage;
            }

            if (wcImage != null)
            {
                cmd.Parameters["@WCImageContent"].Value = wcImage;
            }

            if (frImage != null)
            {
                cmd.Parameters["@FrimageContent"].Value = frImage;
            }

            try
            {
                this.connection.Open();
                result = (cmd.ExecuteNonQuery() > 0);
            }
            finally
            {
                this.connection.Close();
            }
        }

        return result;
    }

    private void SaveData(string malt, string frtBill, string wTag, string complete, string fbFileName, string shFileName, string wcFileName, string frFileName)
    {
        int maltID;
        byte[] fbImage;
        byte[] shImage;
        byte[] wcImage;
        byte[] frImage;
        if (frtBill.Length > 0 && Int32.TryParse(malt, maltID))
        {
            if File.Exists(fbFileName)
            {
                fbImage = WhatEver.ReadFile(fbFileName)
            }

            if File.Exists(shFileName)
            {
                shImage = WhatEver.ReadFile(shFileName)
            }

            if File.Exists(wcFileName)
            {
                wcImage = WhatEver.ReadFile(wcFileName)
            }

            if File.Exists(frFileName)
            {
                frImage = WhatEver.ReadFile(frFileName)
            }

            this.SaveData(maltID, frtBill, wTag, complete, fbImage, shImage, wcImage, frImage)
        }
        else
        {
            MessageBox.Show("Incomplete Data", "", MessageBoxButtons.OK);
        }
    }

    private void SaveData()
    {
        this.SaveData(
            this.maltIDTextBox.Text,
            this.frtBillTextBox.Text,
            this.wTagTextBox.Text,
            this.completeTextBox.Text,
            this.fBImageContentTextBox.Text,
            this.sHImageContentTextBox.Text,
            this.wCImageContentTextBox.Text,
            this.frImageContentTextBox.Text
        );
    }
}

Open in new window


Obviously you need appropriate error handling.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.