Click here to Skip to main content
15,881,593 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I take image using file upload and save it into database (Sql server)by using byte array. My image datatype id image.
I take one asp handler to bind image to gridview.
But at the time of execution the handler give error image_id is not valid column.
**************
My code is
.aspx file
In gridview my image field is
XML
<asp:TemplateField>
                <ItemTemplate>
                   <asp:Image runat="server" ID="grdvwimg" ImageUrl='<%#"ModelHandler.ashx?id="+Eval("mdl_id")%>' Height="150px" Width="150px" />
               </ItemTemplate>



*********

.cs
public void bind()
       {

         cmd = new SqlCommand();
           con = new SqlConnection(strConnString);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.AddWithValue("@Flag", 13);
           cmd.CommandText = "StoreProcedureSelect";

           cmd.Connection = con;
           try
           {
               con.Open();
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataSet ds = new DataSet();
               da.Fill(ds);
               grdvwModel.DataSource = ds;
                 grdvwModel.DataBind();
              }
           catch (Exception ex)
           {
               throw ex;
           }
              }

************************
hamdler.ashx


<pre> public void ProcessRequest(HttpContext context)
       {
           try
           {
               con = new SqlConnection(strConnString);
               string imageid = context.Request.QueryString["id"];
               SqlCommand command = new SqlCommand("SELECT  mdlimg FROM Model WHERE mdl_id = 'imageid'", con);
               con.Open();
               SqlDataReader dr = command.ExecuteReader();
               dr.Read();
               context.Response.BinaryWrite((Byte[])dr[0]);

               context.Response.End();
           }
           catch (Exception ex)
           {
               throw ex;
           }

       }

       public bool IsReusable
       {
           get
           {
               return false;
           }
       }
   }
Posted
Updated 13-Oct-15 19:32pm
v2

1 solution

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