Click here to Skip to main content
15,908,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to use gridview with objectdatasource to obtain the images from the sql database. Within the database, a datafield is declare as "varbinary" datatype.

Now in my gridview, it doesn't show me the image instead of showing me a text "System.Byte[]"

May I know how to solve it.
Posted
Comments
rkthiyagarajan 16-Sep-11 1:00am    
Include Image property in gridview.
saeed1364 16-Sep-11 1:06am    
Please give details

You have to convert your byte[] array of image data to image and then show it to grid view

in gridviews row data bound event you have to write your code for this
 
Share this answer
 
Comments
saeed1364 16-Sep-11 1:02am    
With an explanation tell me
You use this code, I was use this its work....After image stored in database you use this...

Aspx Page...

XML
<p style="margin-left: 160px">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                DataKeyNames="inum" DataSourceID="SqlDataSource1" CellPadding="4"
                ForeColor="#333333" GridLines="None">
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <Columns>
                    <asp:BoundField DataField="inum" HeaderText="inum" InsertVisible="False" ReadOnly="True"
                        SortExpression="inum" />
                    <asp:BoundField DataField="iname" HeaderText="iname" SortExpression="iname" />
                    <asp:TemplateField HeaderText="image">
                    <ItemTemplate>

<asp:image ID="image1" runat="server"
ImageUrl='<%# "Handler.ashx?inum=" + Eval("inum")%>'/>
</ItemTemplate>
</asp:TemplateField>

                </Columns>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>

            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:SomeDataBase1 %>"
                SelectCommand="SELECT inum, iname, image FROM ft"
                ConflictDetection="CompareAllValues"
                DeleteCommand="DELETE FROM [ft] WHERE [inum] = @original_inum AND (([iname] = @original_iname) OR ([iname] IS NULL AND @original_iname IS NULL)) AND (([image] = @original_image) OR ([image] IS NULL AND @original_image IS NULL))"
                InsertCommand="INSERT INTO [ft] ([inum], [iname], [image]) VALUES (@inum, @iname, @image)"
                OldValuesParameterFormatString="original_{0}"
                UpdateCommand="UPDATE [ft] SET [iname] = @iname, [image] = @image WHERE [inum] = @original_inum AND (([iname] = @original_iname) OR ([iname] IS NULL AND @original_iname IS NULL)) AND (([image] = @original_image) OR ([image] IS NULL AND @original_image IS NULL))">
                <DeleteParameters>
                    <asp:Parameter Name="original_inum" Type="String" />
                    <asp:Parameter Name="original_iname" Type="String" />
                    <asp:Parameter Name="original_image" Type="Object" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="iname" Type="String" />
                    <asp:Parameter Name="image" Type="Object" />
                    <asp:Parameter Name="original_inum" Type="String" />
                    <asp:Parameter Name="original_iname" Type="String" />
                    <asp:Parameter Name="original_image" Type="Object" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="inum" Type="String" />
                    <asp:Parameter Name="iname" Type="String" />
                    <asp:Parameter Name="image" Type="Object" />
                </InsertParameters>
            </asp:SqlDataSource>



Code Behind...

protected void Page_Load(object sender, EventArgs e)
       {
           SqlConnection con = new SqlConnection();
           string constr = ConfigurationManager.ConnectionStrings["SomeDataBase1"].ToString();
           con = new SqlConnection(constr);
           SqlCommand cmd = new SqlCommand();
           con.Open();

               GridView1.DataBind();
               con.Close();

       }
 
Share this answer
 
Comments
saeed1364 16-Sep-11 12:44pm    
my frined pleas send code Handler.ashx
rkthiyagarajan 16-Sep-11 20:24pm    
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlTypes;



namespace imageuploadlast
{
///
/// Summary description for $codebehindclassname$
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
//SqlConnection con = new SqlConnection();
//con.ConnectionString = ConfigurationManager.ConnectionStrings
//["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection();
string constr = ConfigurationManager.ConnectionStrings["SomeDataBase1"].ToString();
con = new SqlConnection(constr);

// Create SQL Command
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select iname,image from ft" +
" where inum =@inum";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;

SqlParameter ImageID = new SqlParameter
("@inum", System.Data.SqlDbType.NChar,10);
ImageID.Value = context.Request.QueryString["inum"];
cmd.Parameters.Add(ImageID);
con.Open();
SqlDataReader dReader = cmd.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["image"]);
dReader.Close();
con.Close();
//context.Response.ContentType = "text/plain";
// context.Response.Write("Hello World");
}

public bool IsReusable
{
get
{
return false;
}
}
}
}
first convert the data into image format and then bind it to gridview.
can u post ur code ???
 
Share this answer
 
Comments
saeed1364 16-Sep-11 4:29am    
how to convert .i send code

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