图片上传错误 [英] error in image uploading

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

问题描述

当我在jquery选项卡控件中尝试此代码时,它向我显示错误对象引用未设置为对象的实例".我也在sql中进行了测试,但是在这里它可以成功地插入到数据库中

while i try this code in jquery tab control it shows me error that "Object reference not set to an instance of an object." i also tested in sql but here it can successfully inserted to the database

protected void Button5_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUpload1.PostedFile.FileName != "")
            {
                byte[] imagesize = new byte[FileUpload1.PostedFile.ContentLength];
                HttpPostedFile uploadedimage = FileUpload1.PostedFile;
                uploadedimage.InputStream.Read(imagesize, 0, (int)FileUpload1.PostedFile.ContentLength);
                con.Open();
                SqlCommand cmd = new SqlCommand("insert into room_info(room_image,room_type,info,conditions,single_price,double_price,sno) values(@room_image,'" + TextBox11.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "' ,'" + TextBox14.Text + "','" + TextBox15.Text + "','" + TextBox16.Text + "')", con);
                SqlParameter Uploadedimage = new SqlParameter("@room_image", SqlDbType.Image, imagesize.Length);
                Uploadedimage.Value = imagesize;
                cmd.Parameters.Add(Uploadedimage);
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
        catch
        {
        }
    }

推荐答案

RempoRaaj,


我建议您尝试以下替代方法:

Hi RempoRaaj,


I suggest you to try this alternative:

protected void Button5_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUpload1.PostedFile.FileName != "")
            {
                byte[] imagesize = FileUpload1.FileBytes;                
                con.Open();
                SqlCommand cmd = new SqlCommand("insert into room_info (room_image,room_type,info,conditions,single_price,double_price, sno)  values(@room_image,'" + TextBox11.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "' ,'" + TextBox14.Text + "','" + TextBox15.Text + "','" + TextBox16.Text + "')", con);
                SqlParameter Uploadedimage = new SqlParameter("@room_image", SqlDbType.Image, imagesize.Length);
                Uploadedimage.Value = imagesize;
                cmd.Parameters.Add(Uploadedimage);
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
        catch
        {
        }
    }




希望对您有帮助.

快乐编码;)
Sunny_K




Hope this helps you.

Happy Coding ;)
Sunny_K


这可能对您有所帮助...



Hi this may help you...



if (fileupload1.HasFile)
       {
          byte [] productImage = fileupload1.FileBytes;

          con.Open();
           SqlCommand cmd = new SqlCommand("SP_SetUserDetails", con);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.AddWithValue("@Username", txtuname.Text);
           cmd.Parameters.AddWithValue("@EmailID", txtEmail.Text);
           cmd.Parameters.AddWithValue("@Password", txtpass.Text);
           cmd.Parameters.AddWithValue("@Image", productImage);
           cmd.Parameters.AddWithValue("@Hobbies", str);
           cmd.Parameters.AddWithValue("@country", txtcountry.Text);
           cmd.Parameters.AddWithValue("@Status", sts.ToString());
           //cmd.ExecuteNonQuery();
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataSet ds = new DataSet();
           da.Fill(ds);
           Gridview1.DataSource = ds;
           //Gridview1.DataBind();
           BindGrid();
           //con.Close();

       }




存储过程





Stored Procedure


<pre>ALTER procedure [dbo].[SP_SetUserDetails]
(
@Username varchar(50),
@EmailID varchar(50),
@Password varchar(50),
@Image varbinary(max),
@Hobbies varchar(max),
@country varchar(50),
@Status varchar(50)
)
as
begin
Insert into UserDetails(UserName,EmailId,Password,PImage,Hobbies,Country,Status)values(@Username,@EmailID,@Password,@Image,@Hobbies,@country,@Status)
end


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

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