Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HELLO,

I Have made an application based on the article published by BALA SATHISH
Upload and Download Files From DataGridView Cells[^]

I developed a local database software and it's in the final stage. My Windows application can store the different types of data in GridView (cells) and can search for data. While whenever I upload and save the data in my database, it's properly showing and saving the file in the specified cell and column, and also I am able to download the file to my computer too, yet when I close my application and if I feed the software again, the uploaded data cannot be download to my computer. What to do in this situation?

Additional to the article published by BALA SATHISH, I have added a localdata base called storage.sdf to my application and i am saving all my files to that. Then also i am unable to retrieve my uploaded data if I am running the software for the second time

Also, I have a save button in order to save my entire work and update everything in my database.

I require to call back my file if I close my application and also get it afterward. What to do now?

I need serious help pleasee....


Snapshot of my gridvew: http://tinypic.com/view.php?pic=5z0bpv&s=8#.U8tQRRYYEpE[^]

Snap shot of error i am getting: http://tinypic.com/view.php?pic=2ziuas3&s=8#.U8tQRhYYEpE[^]

My uploading/downloading and save button code is given below:

C#
private void Savebtn_Click(object sender, EventArgs e)
{
    this.Validate();
    this.cncInfoBindingSource.EndEdit();
    this.tableAdapterManager.UpdateAll(this.cncDataSet1);
    try
    {
        MessageBox.Show("File Saved");
    }
    catch (System.Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message, "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

private void btnUpload_Click(object sender, EventArgs e)
{
    try
    {
        //Throw error if attachment cell is not selected.
        //make sure user select only single cell
        if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1)
        {
            UploadAttachment(cncInfoDataGridView.SelectedCells[0]);
        }
        else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 2)
        {
            UploadAttachment(cncInfoDataGridView.SelectedCells[0]);
        }
        else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 3)
        {
            UploadAttachment(cncInfoDataGridView.SelectedCells[0]);
        }
        else
            MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

private void btnDownload_Click(object sender, EventArgs e)
{
    //Throw error if attachment cell is not selected.
    //make sure user select only single cell
    //and the cell have a value in it
    if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1 && cncInfoDataGridView.SelectedCells[0].Value != null)
    {
        DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
    }
    else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 2 && cncInfoDataGridView.SelectedCells[0].Value != null)
    {
        DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
    }
    else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 3 && cncInfoDataGridView.SelectedCells[0].Value != null)
    {
        DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
    }
    else
        MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

private void cncInfoDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    //Throw error if attachment cell is not selected.
    //make sure user select only single cell
    //and the cell have a value in it
    if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1 && cncInfoDataGridView.SelectedCells[0].Value != null)
    {
        DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
    }
    else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 2 && cncInfoDataGridView.SelectedCells[0].Value != null)
    {
        DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
    }
    else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 3 && cncInfoDataGridView.SelectedCells[0].Value != null)
    {
        DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
    }
    else
        MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

/// <summary>
/// Upload Attachment at the provided DataGridViewCell
/// </summary>
/// <param name="dgvCell"></param>
private void UploadAttachment(DataGridViewCell dgvCell)
{
    if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1)
    {
        using (OpenFileDialog fileDialog = new OpenFileDialog())
        {
            //Set File dialog properties
            fileDialog.CheckFileExists = true;
            fileDialog.CheckPathExists = true;
            fileDialog.Filter = "All Files|*.*";
            fileDialog.Title = "Select a file";
            fileDialog.Multiselect = false;

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo fileInfo = new FileInfo(fileDialog.FileName);
                byte[] binaryData = File.ReadAllBytes(fileDialog.FileName);
                cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value = fileInfo.Name;

                if (_myAttachments.ContainsKey(dgvCell.RowIndex))
                    _myAttachments[dgvCell.RowIndex] = binaryData;
                else
                    _myAttachments.Add(dgvCell.RowIndex, binaryData);
            }
        }
    }
    else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 2)
    {
        using (OpenFileDialog fileDialog = new OpenFileDialog())
        {
            //Set File dialog properties
            fileDialog.CheckFileExists = true;
            fileDialog.CheckPathExists = true;
            fileDialog.Filter = "All Files|*.*";
            fileDialog.Title = "Select a file";
            fileDialog.Multiselect = false;

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo fileInfo = new FileInfo(fileDialog.FileName);
                byte[] binaryData = File.ReadAllBytes(fileDialog.FileName);
                cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[2].Value = fileInfo.Name;

                if (_myAttachments.ContainsKey(dgvCell.RowIndex))
                    _myAttachments[dgvCell.RowIndex] = binaryData;
                else
                    _myAttachments.Add(dgvCell.RowIndex, binaryData);
            }
        }
    }
    else 
    {
        using (OpenFileDialog fileDialog = new OpenFileDialog())
        {
            //Set File dialog properties
            fileDialog.CheckFileExists = true;
            fileDialog.CheckPathExists = true;
            fileDialog.Filter = "All Files|*.*";
            fileDialog.Title = "Select a file";
            fileDialog.Multiselect = false;

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo fileInfo = new FileInfo(fileDialog.FileName);
                byte[] binaryData = File.ReadAllBytes(fileDialog.FileName);
                cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[3].Value = fileInfo.Name;

                if (_myAttachments.ContainsKey(dgvCell.RowIndex))
                    _myAttachments[dgvCell.RowIndex] = binaryData;
                else
                    _myAttachments.Add(dgvCell.RowIndex, binaryData);
            }
        }
    }
}

/// <summary>
/// Download Attachment from the provided DataGridViewCell
/// </summary>
/// <param name="dgvCell"></param>
private void DownloadAttachment(DataGridViewCell dgvCell)
{
    string fileName = Convert.ToString(dgvCell.Value);

        //Return if the cell is empty
        if (fileName == string.Empty)
            return;

        FileInfo fileInfo = new FileInfo(fileName);
        string fileExtension = fileInfo.Extension;

        byte[] byteData = null;

        //show save as dialog
        using (SaveFileDialog saveFileDialog1 = new SaveFileDialog())
        {
            //Set Save dialog properties
            saveFileDialog1.Filter = "Files (*" + fileExtension + ")|*" + fileExtension;
            saveFileDialog1.Title = "Save File as";
            saveFileDialog1.CheckPathExists = true;
            saveFileDialog1.FileName = fileName;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                byteData = _myAttachments[dgvCell.ColumnIndex];
                File.WriteAllBytes(saveFileDialog1.FileName, byteData);
            }
        }
    }
Posted
Updated 19-Jul-14 19:55pm
v2

1 solution

"I have added a localdata base called storage.sdf to my application"
Probably, this is your problem. Check the file manually immediately after running your application - do not run your app again to check: use SSMS or VS to do this - and ensure your data is definitely in it. Of it is, then you need to look at how VS is testing the for you added to your project - there is a very good chance that you have set it to be copied into the Bin/Debug folder each time you execute it - which would throw away your changes.
 
Share this answer
 
Comments
Member 10945973 20-Jul-14 1:50am    
If i mail you the entire project can you make changes and give to me. I have no idea about what you saving. please
OriginalGriff 20-Jul-14 4:02am    
No. Because for one thing you don't have (and aren't going to get) my email address, and for another we don;t do your work for you.

Try this: remove the DB from your project and move it so a "safe" place - this will help you find one:
http://www.codeproject.com/Tips/370232/Where-should-I-store-my-data
Then change your code to access it and see if the problem goes away...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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