1

使用 FileUpload 上传图像文件时出现此错误。我在按钮单击事件插入时收到此错误

  public int InsertData(ProductPhoto prdctphoto)
        {
            int ans = 0;
            SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
            cmd.CommandText = "prcProductPhoto";
            cmd.CommandType = CommandType.StoredProcedure;
           // cmd.Parameters.Add(new SqlParameter("@PhotoID", prdctphoto.PhotoID));
            cmd.Parameters.Add(new SqlParameter("@PhotoName", prdctphoto.PhotoName));
            cmd.Parameters.Add(new SqlParameter("@ExtName", prdctphoto.ExtName));
            cmd.Parameters.Add(new SqlParameter("@PhotoType", prdctphoto.PhotoType));
            cmd.Parameters.Add(new SqlParameter("@PhotoSize", prdctphoto.PhotoSize));
            cmd.Parameters.Add(new SqlParameter("@ProductID", prdctphoto.ProductID));
            ans = int.Parse(cmd.ExecuteScalar().ToString());
            cmd.Dispose();
            DataConnection.CloseConnection();
            return ans;
        }

我遇到了错误

ans = int.Parse(cmd.ExecuteScalar().ToString());

我的存储过程的代码是

create proc prcProductPhoto
(
@PhotoName  Varchar(100),
@ExtName    Varchar(100),
@PhotoType  Varchar(100),
@PhotoSize  int,
@ProductID  Int
)
as
insert into ProductPhoto(PhotoName,ExtName,PhotoType,PhotoSize,ProductID) values (@PhotoName,@ExtName,@PhotoType,@PhotoSize,@ProductID)

按钮点击事件的代码是

protected void Insert_Click(object sender, EventArgs e)
    {
        try
        {

            if (photoupload.HasFile)
            {
                ProductPhoto prdctphoto = new ProductPhoto();
                prdctphoto.PhotoName = photoupload.FileName;
                prdctphoto.PhotoSize = photoupload.PostedFile.ContentLength;
                prdctphoto.PhotoType = photoupload.PostedFile.ContentType;
                prdctphoto.ExtName = prdctphoto.PhotoName.Substring(prdctphoto.PhotoName.LastIndexOf(".") + 1);
                prdctphoto.ProductID = int.Parse(ddlproductname.SelectedValue);
                //Response.Write(data.ExtName);
                int ans = new InsertAction().InsertData(prdctphoto);
                if (ans != 0)
                {
                    string path = Server.MapPath("~/upload/") + ans.ToString() + "." + prdctphoto.ExtName;
                    photoupload.SaveAs(path);
                    Response.Write(" File is Uploaded ");
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
4

2 回答 2

0

ExecuteScalar将从查询产生的第一个结果集的第一行返回第一列的值。

您的存储过程不返回任何结果集,因此ExecuteScalar将返回null。当您调用 时cmd.ExecuteScalar().ToString(),您正在调用null实例上的方法,这就是您获得NullReferenceException.

要解决此问题,您需要更改存储过程以返回适当的值。从查询中不清楚该值应该是什么,但假设您在ProductPhoto要返回的表上有一个标识列,您可以尝试以下操作:

create proc prcProductPhoto
(
   @PhotoName  varchar(100),
   @ExtName    varchar(100),
   @PhotoType  varchar(100),
   @PhotoSize  int,
   @ProductID  Int
)
As
   insert into ProductPhoto
   (
      PhotoName,
      ExtName,
      PhotoType,
      PhotoSize,
      ProductID
   ) 
   values 
   (
      @PhotoName,
      @ExtName,
      @PhotoType,
      @PhotoSize,
      @ProductID
   )

   select Scope_Identity()
于 2013-07-23T16:42:05.940 回答
0

我今天早些时候刚刚遇到这个问题。此错误意味着您应该在“ans”中初始化的值为空。尝试调试它并确保它实际存储了一个值。

于 2013-07-23T16:01:18.327 回答