Reputation: 45
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="bookname" Height="504px"
Width="289px">
<Columns>
<asp:TemplateField HeaderText="image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# String.Format("~/path/to/image/" + Eval("image")) %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("image") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="bookname" HeaderText="bookname" ReadOnly="True"
SortExpression="bookname" />
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [image], [bookname], [price] FROM [books]">
</asp:SqlDataSource>
My Image field is saved in (SQL server 2005)...and image inserted in database through file upload button ... and now i want to show data in gridview but not display image field and other field shown in gridview very well becauese other fields are text format.
Upvotes: 0
Views: 6065
Reputation:
<asp:Image ID="Image1" runat="server" ImageUrl='<%# String.Format("~/path/to/image/{0}" , Eval("image")) %>' />
Your Problem was that string.Format
accepts arguments and you were using a concat operation. Here i have used {0} - stands for argument at/for index 0
. Similarly for multiple arguments you can use string.Format("{0} {1} {2}",var1,var2,var3).
Also above i am assuming {0} will be replaced by file name with extension , like abc.png.
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="bookname" Height="504px"
Width="289px">
<Columns>
<asp:TemplateField HeaderText="image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# String.Format("~/path/to/image/{0}" , Eval("image")) %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("image") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="bookname" HeaderText="bookname" ReadOnly="True"
SortExpression="bookname" />
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [image], [bookname], [price] FROM [books]">
</asp:SqlDataSource>
Upvotes: 1
Reputation: 6406
As your image is stored into database, you need to use a handler to retrieve the image. You can get a referance here http://www.codeproject.com/Articles/271590/Show-an-image-saved-in-a-SQL-Table-on-an-ASP-Net-I
Upvotes: 0
Reputation: 48230
I would expect rather something like
ImageUrl='<%# this.ResolveUrl("~/path/to/image/" + Eval("image")) %>' />
rather than string.Format
Upvotes: 0