错误SqlParameterCollection仅接受非null的SqlParameter类型对象,而不接受HttpPostedFile对象. [英] Error The SqlParameterCollection only accepts non-null SqlParameter type objects, not HttpPostedFile objects.

查看:291
本文介绍了错误SqlParameterCollection仅接受非null的SqlParameter类型对象,而不接受HttpPostedFile对象.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dis是我的代码

Dis is my code

protected void btnUpload_Click(object sender, EventArgs e)
   {
       string strImagedate = txtdate.Text.ToString();
       if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
       {
           byte[] imagesize = new byte[FileUpload1.PostedFile.ContentLength];
           HttpPostedFile uploadedImage = FileUpload1.PostedFile;
           uploadedImage.InputStream.Read(imagesize, 0, (int)FileUpload1.PostedFile.ContentLength);
           SqlConnection con = new SqlConnection();
           con.ConnectionString = ConfigurationManager.ConnectionStrings["ss"].ConnectionString;
           SqlCommand cmd = new SqlCommand();
           cmd.CommandText = "insert into Images(Imagedate,Image)" + "values(@Imagedate,@Image)";
           cmd.CommandType = CommandType.Text;
           cmd.Connection = con;
           SqlParameter Imagedate = new SqlParameter("imagedate", SqlDbType.DateTime);
           Imagedate.Value = strImagedate.ToString();
           cmd.Parameters.Add(Imagedate);
           SqlParameter uploadedimage = new SqlParameter("@Image", SqlDbType.Image, imagesize.Length);
           uploadedimage.Value = imagesize;
           //uploadedImage.value = imagesize;
           cmd.Parameters.Add(uploadedImage);
           con.Open();
           int result = cmd.ExecuteNonQuery();
           con.Close();

           if (result > 0)
               lblmessage.Text = "File Uploaded";


       }

推荐答案

您正在分配cmd.Parameters.Add(uploadedImage);,何时应为cmd.Parameters.Add(uploadedimage);.
这就是为什么您不应该在相同范围内使用相似的变量.您可以花几个小时调试这类东西,但是仅通过阅读代码,很难找到人脑工作这些bug的方式.
You are assigning cmd.Parameters.Add(uploadedImage); when it should be cmd.Parameters.Add(uploadedimage);.
This is why you shouldn''t be using variables in the same scope that are that similar. You can rip out your hair for hours debugging stuff like this, but the way the human brain works these bugs are very, very difficult to find by just reading over the code.


这篇关于错误SqlParameterCollection仅接受非null的SqlParameter类型对象,而不接受HttpPostedFile对象.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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