【发布时间】:2021-02-07 11:14:32
【问题描述】:
我在 C# WPF 中有以下代码,当我选择无图像时,它会显示错误消息“System.ArgumentNullException: Pathe can't be null” 谢谢你4你的帮助
FileStream fileStream = new FileStream(imgName, FileMode.Open, FileAccess.Read);
byte[] imgBytes = new byte[fileStream.Length];
if (string.IsNullOrEmpty(imgBytes.ToString()))
{
fileStream.Read(imgBytes, 0, Convert.ToInt32(fileStream.Length));
fileStream.Close();
db.SpInsProducts(txtProductName.Text.Trim(), txtPrDesc.Text.Trim(), null,
Convert.ToDateTime(lblDate.Content), PublicVariables.gUserId);
}
else
{
fileStream.Read(imgBytes, 0, Convert.ToInt32(fileStream.Length));
fileStream.Close();
db.SpInsProducts(txtProductName.Text.Trim(), txtPrDesc.Text.Trim(), imgBytes,
Convert.ToDateTime(lblDate.Content), PublicVariables.gUserId);
}
【问题讨论】:
-
异常本身很明显。只需添加一张支票。 if( !string.IsNullOrEmpty(imgName) ) {..在此处运行您的代码 ....}
-
谢谢你亲爱的朋友 4 你的评论,我改变了我的代码如下,但它没有保存任何记录
-
使用调试器,在显示的第一行放置一个断点并检查变量的值。
-
if (!string.IsNullOrEmpty(imgName)) { FileStream fileStream = new FileStream(imgName, FileMode.Open, FileAccess.Read);字节[] imgBytes = 新字节[fileStream.Length]; fileStream.Read(imgBytes, 0, Convert.ToInt32(fileStream.Length));文件流。关闭(); db.SpInsProducts(txtProductName.Text.Trim(), txtPrDesc.Text.Trim(), imgBytes, Convert.ToDateTime(lblDate.Content), PublicVariables.gUserId); } db.SaveChanges();
标签: c# wpf exception null arguments