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:
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
{
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)
{
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)
{
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 UploadAttachment(DataGridViewCell dgvCell)
{
if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1)
{
using (OpenFileDialog fileDialog = new OpenFileDialog())
{
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())
{
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())
{
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);
}
}
}
}
private void DownloadAttachment(DataGridViewCell dgvCell)
{
string fileName = Convert.ToString(dgvCell.Value);
if (fileName == string.Empty)
return;
FileInfo fileInfo = new FileInfo(fileName);
string fileExtension = fileInfo.Extension;
byte[] byteData = null;
using (SaveFileDialog saveFileDialog1 = new SaveFileDialog())
{
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);
}
}
}