【发布时间】:2012-08-05 16:48:04
【问题描述】:
我创建了一个加载图像然后将其保存到数据库中的表单。
如果用户没有选择任何图像,我想将值 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));
}
}
【问题讨论】:
-
你为什么在
imgData上打电话给ToString()?如果imgData是null,那将引发异常。 -
我认为更多的代码会有所帮助。 imgData 的类型是什么?卓尔的类型是什么,它的运行时价值是什么?
标签: c# sql-server winforms ado.net