Hello
I have a grid view, but the problem is the image is not displaying in grid view.
please help
my button submit code is this:
protected void btnUpload_Click(object sender, EventArgs e)
{
string strImageName = txtName.Text.ToString();
string strDesc=txtDesc.Text.ToString();
string strActive=txtActive.Text.ToString();
string strCreatedBy=txtCreatedBy.Text.ToString();
string dCreatedDate = txtCreDate.Text.ToString();
if (FileUpload.PostedFile != null && FileUpload.PostedFile.FileName != "")
{
byte[] imageSize = new byte[FileUpload.PostedFile.ContentLength];
HttpPostedFile uploadImage = FileUpload.PostedFile;
uploadImage.InputStream.Read(imageSize, 0, (int)FileUpload.PostedFile.ContentLength);
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
cmd.CommandText = "sp_SqlImage";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
SqlParameter ID = new SqlParameter("@ID", SqlDbType.BigInt);
ID.Value = 0;
cmd.Parameters.Add(ID);
SqlParameter ImageName = new SqlParameter("@Name", SqlDbType.VarChar, 50);
ImageName.Value = strImageName.ToString();
cmd.Parameters.Add(ImageName);
SqlParameter ImageDesc = new SqlParameter("@Description", SqlDbType.VarChar, 500);
ImageDesc.Value = strDesc.ToString();
cmd.Parameters.Add(ImageDesc);
SqlParameter UploadImage = new SqlParameter("@Image", SqlDbType.Image, imageSize.Length);
UploadImage.Value = imageSize;
cmd.Parameters.Add(UploadImage);
SqlParameter Active = new SqlParameter("@Active", SqlDbType.VarChar, 1);
Active.Value = strActive.ToString();
cmd.Parameters.Add(Active);
SqlParameter CreatedBy = new SqlParameter("@CreatedBy", SqlDbType.VarChar, 10);
CreatedBy.Value = strCreatedBy.ToString();
cmd.Parameters.Add(CreatedBy);
SqlParameter CreatedDate = new SqlParameter("@CreatedDate", SqlDbType.DateTime);
CreatedDate.Value = dCreatedDate.ToString();
cmd.Parameters.Add(CreatedDate);
SqlParameter Flag = new SqlParameter("@Flag", SqlDbType.VarChar, 10);
Flag.Value = "INSERT";
cmd.Parameters.Add(Flag);
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
lblMessage.Text = "File Upload";
}
My aspx page for this is:
<<pre lang="xml">%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainTable.aspx.cs" Inherits="commonlogin.MainTable" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="False"
AutoGenerateDeleteButton="True" onrowdeleted="GridView1_RowDeleted" AutoGenerateEditButton ="True"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onpageindexchanging="GridView1_PageIndexChanging"
onrowcancelingedit="GridView1_RowCancelingEdit"
CellPadding="4"
GridLines="None" ForeColor="#333333"
>
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="ID">
<%----%>
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<EditItemTemplate>
<asp:TextBox ID="txtDesc" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDesc" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<EditItemTemplate>
<asp:TextBox ID="txtImage" runat="server" Text='<%# Bind("Image") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<%----%>
<asp:Label ID="lblImage" runat="server" Text='<%# Bind("Image") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Active">
<EditItemTemplate>
<asp:TextBox ID="txtActive" runat="server" Text='<%# Bind("Active") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblActive" runat="server" Text='<%# Bind("Active") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="CreatedBy">
<EditItemTemplate>
<asp:TextBox ID="txtCreatedBy" runat="server" Text='<%# Bind("CreatedBy") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCreatedBy" runat="server" Text='<%# Bind("CreatedBy") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="CreatedDate">
<EditItemTemplate>
<asp:TextBox ID="txtCreatedDate" runat="server" Text='<%# Bind("CreatedDate") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCreatedDate" runat="server" Text='<%# Bind("CreatedDate") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView><br />
<tr>
<td>
<asp:HyperLink ID="HyperLink1" NavigateUrl ="~/TableAdd.aspx" runat="server">AddNew</asp:HyperLink><br /><br />
</td>
</tr>
<asp:label id="Messagelbl" forecolor="Red" runat="server"/>
</div>
</form>
</body>
</html
>
And my ashx code is this:
using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
namespace commonlogin
{
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT Name, Description, Iamge, Active, CreatedBy, CreatedDate FROM tbl_SqlImage" + "where ID=@ID");
cmd.Connection = conn;
SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.BigInt);
ImageID.Value = context.Request.QueryString["ID"];
cmd.Parameters.Add(ImageID);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((byte[])dr["Image"]);
dr.Close();
conn.Close();
public bool IsReusable
{
get
{
return false;
}
}
}
Please help me with this, is something wrong in this code
I am getting the column for the image in gridview displaying System.Byte[] instead of the image.
Please Help