i have a problem with uploading files to db using openFileDialog, i have to upload five files with *.TIF another with *.PDF , then i use five openFileDialog (openFileDialog1,openFileDialog2,openFileDialog3,openFileDialog4,openFileDialog5)
i notice that all five fields upload the same *.TIF file what is the problem ?
this is the code :

    byte[] AreaChartsFileBytes = new byte[64];
    byte[] AlhodChartsFileBytes = new byte[64];
    byte[] AlqetaChartsFileBytes = new byte[64];
    byte[] AlqasiemehChartsFileBytes = new byte[64];
    byte[] UploadFilesFileBytes = new byte[64];


    private void uploadFiles(string linke,ref byte[] FileBytes) 
    {
        string filetype;
        string filename;

        filename = linke.Substring(Convert.ToInt32(linke.LastIndexOf("\\")) + 1, linke.Length - (Convert.ToInt32(linke.LastIndexOf("\\")) + 1));
        filetype = linke.Substring(Convert.ToInt32(linke.LastIndexOf(".")) + 1, linke.Length - (Convert.ToInt32(linke.LastIndexOf(".")) + 1));

        //MessageBox.Show(filename + " " + filetype);

        //Validate user upload only specific bytes - un comment below lines if you need to validate only PDF files

        if (filetype.ToUpper() != "TIF" && filetype.ToUpper() != "PDF")
        {
            AlertLabel.ForeColor = Color.Red;
            AlertLabel.Text = "الرجاء التأكد من إدخال ملف TIF أو PDF...";
            return;
        }

        try
        {
            // Open file to read using file path
            FileStream FS = new FileStream(linke, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            // Add filestream to binary reader
            BinaryReader BR = new BinaryReader(FS);

            // get total byte length of the file
            long allbytes = new FileInfo(linke).Length;

            // read entire file into buffer
            FileBytes = BR.ReadBytes((Int32)allbytes);

            // close all instances
            FS.Close();
            FS.Dispose();
            BR.Close();
        }
        catch (Exception ex)
        {
            AlertLabel.Text = "Error during File Read " + ex.ToString();
            //MessageBox.Show("Error during File Read " + ex.ToString());
        }
    }

           private void AlhodChartsButton_Click(object sender, EventArgs e)
    {
        openFileDialog2.Title = "Select file to be upload";
        //openFileDialog1.Filter = "PDF Files|*.pdf|All Files|*.*";
        openFileDialog2.Filter = "TIF Files|*.TIF";
        if (openFileDialog2.ShowDialog() == DialogResult.OK)
        {
            AlhodChartsTextBox.Text = openFileDialog1.FileName.ToString();
        }
        if (!AlhodChartsTextBox.Text.Trim().Equals(""))
        {
            uploadFiles(AlhodChartsTextBox.Text.Trim(),ref AlhodChartsFileBytes);
        }
    }

    private void AlqetaChartsButton_Click(object sender, EventArgs e)
    {
        openFileDialog1.Title = "Select file to be upload";
        //openFileDialog1.Filter = "PDF Files|*.pdf|All Files|*.*";
        openFileDialog1.Filter = "TIF Files|*.TIF";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            AlqetaChartsTextBox.Text = openFileDialog1.FileName.ToString();
        }
        if (!AlqetaChartsTextBox.Text.Trim().Equals(""))
        {
            uploadFiles(AlqetaChartsTextBox.Text.Trim(),ref AlqetaChartsFileBytes);
        }
    }

    private void AlqasiemehChartsButton_Click(object sender, EventArgs e)
    {
        openFileDialog1.Title = "Select file to be upload";
        //openFileDialog1.Filter = "PDF Files|*.pdf|All Files|*.*";
        openFileDialog1.Filter = "TIF Files|*.TIF";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            AlqasiemehChartsTextBox.Text = openFileDialog1.FileName.ToString();
        }
        if (!AlqasiemehChartsTextBox.Text.Trim().Equals(""))
        {
            uploadFiles(AlqasiemehChartsTextBox.Text.Trim(), ref AlqasiemehChartsFileBytes);
        }
    }

    private void UploadFilesButton_Click(object sender, EventArgs e)
    {
        openFileDialog5.Title = "Select file to be upload";
        //openFileDialog1.Filter = "PDF Files|*.pdf|All Files|*.*";
        openFileDialog5.Filter = "PDF Files|*.PDF";
        if (openFileDialog5.ShowDialog() == DialogResult.OK)
        {
            UploadFilesTextBox.Text = openFileDialog5.FileName.ToString();
        }
        if (!UploadFilesTextBox.Text.Trim().Equals(""))
        {
            uploadFiles(UploadFilesTextBox.Text.Trim(), ref UploadFilesFileBytes);
            MessageBox.Show(UploadFilesFileBytes.Length.ToString());
        }
    }
            private void AreaChartsButton_Click(object sender, EventArgs e)
    {
        openFileDialog1.Title = "Select file to be upload";
        //openFileDialog1.Filter = "PDF Files|*.pdf|All Files|*.*";
        openFileDialog1.Filter = "TIF Files|*.TIF";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            AreaChartsTextBox.Text = openFileDialog1.FileName.ToString();
        }
        if (!AreaChartsTextBox.Text.Trim().Equals(""))
        {
            uploadFiles(AreaChartsTextBox.Text.Trim(), ref AreaChartsFileBytes);

        }
    }

You're using the filename from openfiledialog1 after you show openfiledialog2.

Your code is using dialogs 1,2 and 5, but 1 is used 3 times.

Since you're calling each one separately, you only need to use 1 dialog and change the filter and the title each time you want to use it. A seperate function taking the filter type and returning the filename would keep your code a lot cleaner.

Something like this:

    private string GetFilename(string Filter)
    {
        switch (Filter)
        {
            case "Tif":
                openFileDialog1.Filter = "TIF Files|*.TIF";
                break;
            case "PDF":
                openFileDialog1.Filter = "PDF Files|*.PDF";
                break;

        }
        DialogResult Result = openFileDialog1.ShowDialog();
        if (Result == DialogResult.OK)
        {
            return openFileDialog1.FileName;
        }
        else
        {
            return "";
        }
    }

Then to use it something like this:

 private void AlqetaChartsButton_Click(object sender, EventArgs e)
{
    if (GetFileName("Tif") != "")
    {
    AlqetaChartsTextBox.Text = openFileDialog1.FileName.ToString();
    }
    if (!AlqetaChartsTextBox.Text.Trim().Equals(""))
    {
    uploadFiles(AlqetaChartsTextBox.Text.Trim(),ref AlqetaChartsFileBytes);
    }
}

thanks it's work, i done it as you suggested

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.