我试图在ASP.NET上传图像 [英] I was trying to upload image in ASP.NET

查看:59
本文介绍了我试图在ASP.NET上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ASP.NET网页上传图像并将其存储在数据库中..我被计算了

这个错误:(



I was trying to upload image in ASP.NET web page and storing that in database.. I was en counted
with this error :(

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException:
String or binary data would be truncated.
The statement has been terminated.







使用此编码:




Was using this coding:

 if(FileUpload1.HasFile)
   {

      
       byte[] productImage = FileUpload1.FileBytes;

       SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringss"].ConnectionString);
       con1.Open();
       string cmdStr = ("INSERT INTO Reggg(im) VALUES(@im)");
       SqlCommand userExist = new SqlCommand(cmdStr, con1);
       userExist.Parameters.Add("@im", SqlDbType.VarBinary).Value = productImage;
       int result = Convert.ToInt32(userExist.ExecuteScalar().ToString());
         con1.Close();

       if (result > 0)
       {


           Label1.Text = "Product Saved Successfully";
       }
   }
          else
          {
              Label2.Text = "Please Select Product Image File";
          } 
}



请帮我修这个。



代码块添加[/ Edit]


Please help me to retrieve this.

Code block added[/Edit]

推荐答案

这是因为行

It is because of the line
byte[] productImage = FileUpload1.FileBytes;





而是参考 - 使用ASP.NET网页将图像上传到SQL Server [ ^ ]



Instead refer - Upload images onto SQL Server using ASP.NET webpage[^]

if (FileUpload1.PostedFile != null
&& FileUpload1.PostedFile.FileName != "")
{
      byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];

      HttpPostedFile Image = FileUpload1.PostedFile;
      Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);

      SqlConnection myConnection = new SqlConnection("Data Source = ComputerName\\SQLEXPRESS; Initial Catalog= dbname; Integrated Security= SSPI");
      SqlCommand storeimage = new SqlCommand("INSERT INTO Image_Gallery "+"(Img_Id, Image_Content, Image_Type, Image_Size) "+" values (3, @image, @imagetype, @imagesize)", myConnection);

      storeimage.Parameters.Add("@image", SqlDbType.Image, myimage.Length).Value = myimage;

      storeimage.Parameters.Add("@imagetype", SqlDbType.VarChar, 100).Value = FileUpload1.PostedFile.ContentType;

      storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value = FileUpload1.PostedFile.ContentLength;

      myConnection.Open();
      storeimage.ExecuteNonQuery();
      myConnection.Close();

      Respone.Write("successfully upload the image");
}






谢谢......


Thanks...


这篇关于我试图在ASP.NET上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆