Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 要基于SQL值显示图像吗?_C#_Asp.net_.net_Sql - Fatal编程技术网

C# 要基于SQL值显示图像吗?

C# 要基于SQL值显示图像吗?,c#,asp.net,.net,sql,C#,Asp.net,.net,Sql,希望这里有人能帮我!。在gridview中,我有字段附件,值取自SQL。我试图评估附件字段,如果它包含文本,则显示附件Img,否则-nothing或NoAttch.jpg 我尝试创建ASP图像并使用代码隐藏来评估img,但没有显示图像。 任何帮助,让我发疯 <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="BookingsSQL" ShowHeader="False

希望这里有人能帮我!。在gridview中,我有字段附件,值取自SQL。我试图评估附件字段,如果它包含文本,则显示附件Img,否则-nothing或NoAttch.jpg

我尝试创建ASP图像并使用代码隐藏来评估img,但没有显示图像。 任何帮助,让我发疯

 <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="BookingsSQL"
 ShowHeader="False" Width="80%" BackColor="Azure" GridLines="Horizontal" 
        DataKeyNames="ID" AllowSorting="True" HorizontalAlign="Center" AutoGenerateDeleteButton="false">
 <Columns>
      <asp:TemplateField>
           <ItemTemplate>
                <table id="Table1" runat="server" visible="<%# Container.DataItemIndex == 0 %>">
                     <tr>
                          <th width="20%">
                               <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Sort" CommandArgument="programme_name" >
                                    Programme Name
                               </asp:LinkButton>
                          </th>
                          <th width="10%">
                               <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Sort" CommandArgument="Start">
                                    Start
                               </asp:LinkButton>

                          </th>
                          <th width="10%">
                               <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Sort" CommandArgument="Finish">
                                    Finish
                               </asp:LinkButton>
                          </th>
                          <th width="20%">
                               <asp:LinkButton ID="LinkButton4" runat="server" CommandName="Sort" CommandArgument="Source">
                                    Source
                               </asp:LinkButton>
                          </th>
                          <th width="20%">
                               <asp:LinkButton ID="LinkButton5" runat="server" CommandName="Sort" CommandArgument="Destination">
                                    Destination
                               </asp:LinkButton>
                          </th>
                          <th width="40%">
                               <asp:LinkButton ID="LinkButton6" runat="server" CommandName="Sort" CommandArgument="BComment">
                                    Comment
                               </asp:LinkButton>
                          </th>
                          <th width="10%">
                               <asp:LinkButton ID="LinkButton15" runat="server" CommandName="Sort" CommandArgument="Attachment">
                                    Attachment
                               </asp:LinkButton>

                          </th>
                     </tr>
                </table>
                <asp:Panel runat="server" ID="ItemContainer">
                     <table width="80%">
                          <tr>
                               <td width="10%">
                                    <%# Eval("programme_name") %>
                               </td>
                               <td width="10%">
                                    <%# Eval("Start") %>
                               </td>
                               <td width="10%">
                                    <%# Eval("Finish") %>
                               </td>
                               <td width="20%">
                                    <%# Eval("Source") %>
                               </td>
                               <td width="20%">
                                    <%# Eval("Destination") %>
                               </td>
                               <td width="40%">
                                    <%# Eval("BComment") %>
                               </td>
                                 <td width="10%">
                                    <%# Eval("Attachment") %>
                               </td>
                          </tr>
                     </table>
                </asp:Panel>
                <asp:Panel CssClass="popupMenu" ID="PopupMenu" runat="server">
                     <asp:LinkButton ID="LinkButton7" runat="server" CommandName="Edit" Text="Edit" />
                     <br />
                     <asp:LinkButton ID="LinkButton8" runat="server" CommandName="Insert" CausesValidation="true"
                          Text="Insert" />
                     <br />
                       <asp:LinkButton ID="LinkButton16" runat="server" CommandName="Delete" CausesValidation="true"
                          Text="Delete" />
                </asp:Panel>
                <ajaxToolkit:HoverMenuExtender runat="server" TargetControlID="ItemContainer" PopupControlID="popupMenu"
                     HoverCssClass="popupHover" PopupPosition="Left" OffsetX="0" OffsetY="0" PopDelay="20">
                </ajaxToolkit:HoverMenuExtender>
           </ItemTemplate>
           <EditItemTemplate>
                <table id="Table1" runat="server" visible="<%# Container.DataItemIndex == 0 %>">
                     <tr>
                          <th width="10%">
                               <asp:LinkButton ID="LinkButton9" runat="server" CommandName="Sort" CommandArgument="programme_name">
                                    Programme Name
                               </asp:LinkButton>
                          </th>
                          <th width="10%">
                               <asp:LinkButton ID="LinkButton10" runat="server" CommandName="Sort" CommandArgument="Start">
                                    Start
                               </asp:LinkButton>
                          </th>
                          <th width="10%">
                               <asp:LinkButton ID="LinkButton11" runat="server" CommandName="Sort" CommandArgument="Finish">
                                    Finish
                               </asp:LinkButton>
                          </th>
                          <th width="20%">
                               <asp:LinkButton ID="LinkButton12" runat="server" CommandName="Sort" CommandArgument="Source">
                                    Source
                               </asp:LinkButton>
                          </th>
                          <th width="20%">
                               <asp:LinkButton ID="LinkButton13" runat="server" CommandName="Sort" CommandArgument="Destination">
                                    Destination
                               </asp:LinkButton>
                          </th>
                          <th width="40%">
                               <asp:LinkButton ID="LinkButton14" runat="server" CommandName="Sort" CommandArgument="BComment">
                                    Comment
                               </asp:LinkButton>
                          </th>
                           <th width="40%">


                               <asp:LinkButton ID="LinkButton17" runat="server" CommandName="Sort" CommandArgument="Attachment">
                                    Attachment
                               </asp:LinkButton>
                          </th>
                     </tr>
                </table>
            <!-- Add comments -->
                <asp:Panel runat="server" ID="ItemContainer">
                     <asp:HiddenField runat="server" ID="IdHiddenField" Value='<%# Bind("ID") %>' />
                     <table width="100%">
                          <tr>
                               <td width="10%">
                                    <asp:TextBox ID="TextBox0" runat="server" Text='<%# Bind("programme_name") %>' />
                               </td>
                               <td width="10%">
                                 <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Start") %>'  />
                               </td>
                               <td width="10%">
                                     <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Finish") %>'  />
                               </td>
                               <td width="20%">
                                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Source") %>'  />
                               </td>
                               <td width="20%">
                                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Destination") %>' />
                               </td>
                               <td width="40%">
                                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("BComment") %>' />
                               </td>
                                <td width="10%">
                                  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Attachment") %>' />
                               </td>
                          </tr>
                     </table>
                </asp:Panel>
                <asp:Panel CssClass="popupMenu" ID="PopupMenu" runat="server">
                     <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Update" CausesValidation="true"
                          Text="Update" />
                     <br />
                     <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel" CausesValidation="false"
                          Text="Cancel" />
                           <br />


                </asp:Panel>
                <ajaxToolkit:HoverMenuExtender runat="server" TargetControlID="ItemContainer" PopupControlID="popupMenu"
                     HoverCssClass="popupHover" PopupPosition="Left" OffsetX="0" OffsetY="0" PopDelay="100">
                </ajaxToolkit:HoverMenuExtender>
           </EditItemTemplate>
      </asp:TemplateField>
 </Columns>
   </asp:GridView>
  <asp:SqlDataSource ID="BookingsSQL" runat="server" ConnectionString="<%$  ConnectionStrings:BookingsConnectionString %>"
 OldValuesParameterFormatString="original_{0}" ConflictDetection="OverwriteChanges"
 SelectCommand="SELECT [ID], [ProgrammeName] AS programme_name, [Start], [Finish], [Source], [Destination], [BComment],[Attachment] FROM [DailySheets]"
 DeleteCommand="DELETE FROM [Bookings] WHERE [ID] = @ID" 
 InsertCommand="INSERT INTO [DailySheets] ([programme name], [Start], [Finish], [Source], [Destination], [BComment] , [Attachment]) VALUES (@programme_name, @Start, @Finish, @Source, @Destination, @BComment)"
 UpdateCommand="UPDATE [DailySheets] SET [ProgrammeName] = @programme_name, [Start] = @Start, [Finish] = @Finish, [Source] = @Source, [Destination] = @Destination, [BComment] = @BComment , [Attachment] = @Attachment WHERE [ID] = @ID">
 <DeleteParameters>
      <asp:Parameter Name="ID" Type="Int32" />
      <asp:Parameter Name="Start" Type="DateTime" />
      <asp:Parameter Name="Finish" Type="DateTime" />
      <asp:Parameter Name="Source" Type="String" />
      <asp:Parameter Name="Destination" Type="String" />
      <asp:Parameter Name="BComment" Type="String" />
      <asp:Parameter Name="programme_name" Type="String" />
 </DeleteParameters>
 <InsertParameters>
      <asp:Parameter Name="programme_name" Type="String" />
      <asp:Parameter Name="Start" Type="DateTime" />
      <asp:Parameter Name="Finish" Type="DateTime" />
      <asp:Parameter Name="Source" Type="String" />
      <asp:Parameter Name="Destination" Type="String" />
      <asp:Parameter Name="BComment" Type="String" />
 </InsertParameters>
 <UpdateParameters>
      <asp:Parameter Name="ID" Type="Int32" />
      <asp:Parameter Name="Start" Type="DateTime" />
      <asp:Parameter Name="Finish" Type="DateTime" />
      <asp:Parameter Name="Source" Type="String" />
      <asp:Parameter Name="Destination" Type="String" />
      <asp:Parameter Name="BComment" Type="String" />
      <asp:Parameter Name="programme_name" Type="String" />
 </UpdateParameters>
</asp:SqlDataSource>
图像或任何二进制附件通常不会像文本内联那样输出到页面。您应该创建一个单独处理blob请求的方法,并分配适当的内容类型

这就是问题所在

<%# Eval("Attachment") %>
感谢@TomTom的更正

尝试使用GridviewRowDataBound事件来确定是否有从数据库返回的预期值。 放置在gridview中的TemplateField内。
然后根据从数据库返回的值,设置图像的url。要按照SliverNinja的建议创建处理程序,可以执行以下操作。使用以下内容创建ImageHandler.ashx文件:

public class ImageHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string id = context.Request.QueryString["id"];

        if (!string.IsNullOrEmpty(id))
        {
            int programId = 0;

            if (int.TryParse(id, out programId))
            {
                // Get the BLOB image from the database
                var attachment = GetAttachmentImage(programId);

                if (attachment != null)
                {
                    context.Response.ContentType = "image/jpeg";

                    context.Response.BinaryWrite(attachment);
                }
            }
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
然后在您的ASPX页面中:

<img src='ImageHandler.ashx?id=<%# Eval("id") %>' />

这段代码的输出是什么?它是否指向图像的有效URL?您应该只复制代码中不起作用的部分。也许只是查询和图像标签。实际上理论上他们可以,但这对于较大的项目是完全不明智的;但是可以将图片内联到html中。但它也比评估复杂得多。亲爱的原创海报,获取一本关于HTML如何处理图像的基本书籍,然后检查您在浏览器中创建的hre。