When i am trying to upload a file to my local database I am getting an error called "Object must implement |Convertible"
I really don't have idea why its saying like that. I need help please.
Error image:
Error removing try and catch- https://imageshack.com/i/kpgFCzkrp
Code I am trying:
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
MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace, "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UploadAttachment(DataGridViewCell dgvCell)
{
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 = true;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
SqlCeConnection cnn = new SqlCeConnection(Properties.Settings.Default.CncConnectionString);
FileInfo fileInfo = new FileInfo(fileDialog.FileName);
byte[] binaryData = File.ReadAllBytes(fileDialog.FileName);
cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value = fileInfo.Name;
SqlCeCommand cmd = new SqlCeCommand("INSERT INTO CncInfo (Drawings) VALUES (@DATA)", cnn);
cmd.Parameters.Add("@DATA", binaryData);
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
}
}
}
When i updated the catch with below codes. i got the error like this https://imageshack.com/i/ip6ESuuhp
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace, "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
`
The Add method is deprecated precisely because it does not work in a way you expect it to. Do what the warning message tells you to and use AddWithValue .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.