我创建了一个加载图像然后将其保存到数据库中的表单。
如果用户没有选择任何图像,我想将值 NULL 保存到数据库中。
我试过这段代码:
drow[1] = string.IsNullOrEmpty(imgData.ToString()) ? DBNull.Value : (object)imgData;
但它给了我这个错误:
你调用的对象是空的
这是我用来加载图像的代码:
private void simpleButton5_Click_1(object sender, EventArgs e)
{
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
picture.ImageLocation = openFileDialog1.FileName;
imgData = File.ReadAllBytes(openFileDialog1.FileName);
}
}
catch (Exception ex)
{
// Could not load the image - probably related to Windows file system permissions.
XtraMessageBox.Show(String.Format("Cannot display the image.\n You may not have permission to read the file, or it may be corrupt.\n\nReported error: {0}", ex.Message));
}
}