画像を読み込んでデータベースに保存するフォームを作成しました。
ユーザーが画像を選択しない場合は、値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));
}
}