使用 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);
}
}