Tuesday, 16 October 2012

Save image in Database and display it on Asp.Net C#


save
--------------------------------

 SqlCommand cmd = new SqlCommand("insert into product1 values(@product,@image)", cn);
        cmd.Parameters.AddWithValue("@product", TextBox1.Text);

        int imagelen = FileUpload1.PostedFile.ContentLength;
        byte[] picbyte = new byte[imagelen];
        FileUpload1.PostedFile.InputStream.Read(picbyte, 0, imagelen);
        cmd.Parameters.AddWithValue("@image", picbyte);

        cmd.ExecuteNonQuery();
        cn.Close();

--------------------------------------

display
------------------------

 cn.Open();
        SqlCommand cmd = new SqlCommand("select * from product1 where product=@product", cn);
        cmd.Parameters.AddWithValue("@product", product);
        SqlDataReader dr;
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            Byte[] data = (Byte[])dr["image"];
            Response.BinaryWrite(data);
            Response.Write("aa");
        }

No comments:

Post a Comment