Click here to Skip to main content
16,015,827 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
dear friends,

i have passed an image athrough fileupload and convert it to binary data and store it in database. it sucesfuly completed but when i am trying to return it from database and shown in image control it show error
here is my code

C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;

public partial class Default11 : System.Web.UI.Page
{
    SqlConnection     cc  = new SqlConnection("server=nilu-pc;initial catalog=project;integrated security=true");
    DataSet           ds  = new DataSet();
    public SqlCommand cmd = new SqlCommand();
    SqlDataAdapter adp;

    protected void Page_Load(object sender, EventArgs e)
    {
        cc.Open();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        BinaryReader rr=new BinaryReader(FileUpload1.PostedFile.InputStream);

        byte[] img = rr.ReadBytes(FileUpload1.PostedFile.ContentLength);
       
        string sql = "insert into image values(1,@IMG)";
        adp = new SqlDataAdapter(sql, cc);
        adp.SelectCommand.Parameters.Add("@IMG",SqlDbType.VarBinary,8000).Value=img;
        adp.Fill(ds,"image");
    }

    protected void showinimagecontrol_Click(object sender, EventArgs e)
    {
        System.Drawing.Image imaf = null;
        SqlCommand command = cc.CreateCommand();
        command.CommandText = "select IMG from image WHERE ID=1";
     
        //adp = new SqlDataAdapter(sql,cc);
        //adp.Fill(ds, "image 
        byte[] imageData = (byte[])command.ExecuteScalar();

        // byte[] imgdata = (byte[])ds.Tables[0].Rows[0][1];
        Response.Write(imageData);
        MemoryStream mm = new MemoryStream(imageData, 0, imageData.Length);
        imaf = System.Drawing.Image.FromStream(mm);
        imaf.Save(Server.MapPath("Photo0074.jpg"));
        Image1.ImageUrl = "Photo0074.jpg";
    }
}


it show error as invalid parameter in image.fromstream line

help me in rectifying error

thanks
Posted
Updated 3-Jun-11 1:37am
v2
Comments
CPallini 3-Jun-11 7:54am    
Why do you pass 8000 (instead of the actual image size) to the SqlParameter ctor?

1 solution

Either the stream is null, or it isn't a valid image format.

Use the debugger to find out which problem you're having.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900