Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't Show image in gridview using templates
    primarykey
    data
    text
    <p>i am trying to load images from the northwind database (categories table, images that are stored in the database) into grid view control. But it dosenot seems to work. Plz! have a look...</p> <p><strong>Default.aspx</strong></p> <p></p> <p> </p> <pre><code> &lt;asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CategoryID" DataSourceID="NorthWindSQLExpressConnectionString" EnableModelValidation="True"&gt; &lt;Columns&gt; &lt;asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" /&gt; &lt;asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" /&gt; &lt;asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /&gt; &lt;asp:TemplateField HeaderText="Picture" SortExpression="Picture"&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID="TextBox1" runat="server" Text='&lt;%# Bind("Picture") %&gt;'&gt;&lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:Image ID="Image1" runat="server" ImageUrl='&lt;%# RetriveImage(Eval("CategoryID")) %&gt;' /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:SqlDataSource ID="NorthWindSQLExpressConnectionString" runat="server" ConnectionString="&lt;%$ ConnectionStrings:NorthwindSQLExpressConnectionString %&gt;" SelectCommand="SELECT [CategoryID], [CategoryName], [Description], [Picture] FROM [Categories]"&gt; &lt;/asp:SqlDataSource&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p> </p> <p><strong>[Partial] Default.aspx.cs</strong></p> <pre><code> protected string RetriveImage(object eval) { return ("ImageHandler.ashx?CategoryID=" + eval.ToString()); } </code></pre> <p><strong>[Partial] ImageHandler.ashx</strong></p> <pre><code> public void ProcessRequest(HttpContext context) { if (context.Request.QueryString == null) { } else { try { using (var sqlCon = new SqlConnection(conString)) { const string cmdString = "Select picture from Categories where CategoryID=@CategoryID"; using (var sqlCmd = new SqlCommand(cmdString, sqlCon)) { sqlCmd.Parameters.AddWithValue("@CategoryID", context.Request.QueryString["CategoryID"]); string trmp = sqlCmd.ToString(); sqlCon.Open(); using (var sqlDr = sqlCmd.ExecuteReader()) { sqlDr.Read(); context.Response.ContentType = "image/bmp"; //Added after "Ed B" Sugetion but still dosenot work :( context.Response.BinaryWrite((byte[])sqlDr["Picture"]); } } } } catch (Exception ex) { context.Response.Write(ex.Message); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload