Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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# 查询字符串未传递给处理程序_C#_Asp.net_Sql Server - Fatal编程技术网

C# 查询字符串未传递给处理程序

C# 查询字符串未传递给处理程序,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我在数据库中有两个表,其中有一个字段类型为“Image”,另一个表中有一些字段我想在FormView中检索。 formView显示来自另一个表的字段,但不在图像控件中显示图像。 aspx文件 <asp:FormView runat="server" ID="ListStories" DefaultMode="ReadOnly" > <ItemTemplate> <table> <tr><td><%#Eval("Subject")

我在数据库中有两个表,其中有一个字段类型为“Image”,另一个表中有一些字段我想在FormView中检索。 formView显示来自另一个表的字段,但不在图像控件中显示图像。 aspx文件

<asp:FormView runat="server" ID="ListStories" DefaultMode="ReadOnly" >
<ItemTemplate>
<table>
<tr><td><%#Eval("Subject") %></td></tr>
<tr><td><%#Eval("Story") %></td></tr>
<tr><td><%#Eval("UserName")%> </td></tr>

</table>
<asp:Image ID="Image1" runat="server" ImageUrl = '~/ShowImage.ashx?Name=<%#Eval("UserName") %>'  Width="150" Height="150" />
</ItemTemplate>


</asp:FormView>
图像处理程序:

public class ShowImage : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string userName = HttpContext.Current.Request.QueryString["Name"];

        context.Response.ContentType = "image/jpeg";
        Stream strm = ShowEmpImage(userName);
        try
        {
            byte[] buffer = new byte[4096];
            int byteSeq = strm.Read(buffer, 0, 4096);

            while (byteSeq > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, byteSeq);
                byteSeq = strm.Read(buffer, 0, 4096);
            }
        }
        catch (NullReferenceException)
        {
            context.Response.WriteFile("img/default.png");
        }
    }

    public Stream ShowEmpImage(string name)
    {
        string connString = ConfigurationManager.ConnectionStrings["Alumnidb"].ConnectionString;
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmdRetiveImage = new SqlCommand("SELECT Photo FROM UserProfile WHERE UserName=@UserName", conn);
        cmdRetiveImage.CommandType = CommandType.Text;
        cmdRetiveImage.Parameters.AddWithValue("@UserName", name);
        conn.Open();
        object img = cmdRetiveImage.ExecuteScalar();
        try
        {
            return new MemoryStream((byte[])img);
        }
        catch
        {
            return null;
        }
        finally
        {
            conn.Close();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
但是图像处理程序总是执行这些行

catch (NullReferenceException)
            {
                context.Response.WriteFile("img/default.png");
            }
为什么它不在传递查询字符串时工作,但仍然执行空值?
感谢您的帮助。请输入此代码。我会帮助你的

public void ProcessRequest(HttpContext context)
    {
        string userName = context.Request.QueryString["Name"];

        context.Response.ContentType = "image/jpeg";
        Stream strm = ShowEmpImage(userName);
        try
        {
            byte[] buffer = new byte[4096];
            int byteSeq = strm.Read(buffer, 0, 4096);

            while (byteSeq > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, byteSeq);
                byteSeq = strm.Read(buffer, 0, 4096);
            }
        }
        catch (NullReferenceException)
        {
            context.Response.WriteFile("img/default.png");
        }
    }
替换此项:

... ImageUrl = '~/ShowImage.ashx?Name=<%#Eval("UserName") %>' ...
。。。ImageUrl='~/ShowImage.ashx?Name='。。。

。。。ImageUrl=''。。。

它应该可以工作。

这行有issue=>HttpContext.Current.Request.QueryString[“Name”];确切地说,但我无法理解它,因为我手动添加用户名。它工作正常,但无法使用查询字符串。请尝试使用上下文。请求[“名称”]仍然无法工作。。标记有什么问题吗?当我从图像控件传递查询字符串时?您是否尝试使用浏览器中的查询字符串直接向处理程序发送请求。另外,请使用browser developer工具检查您的请求头是否确实发送了查询字符串参数。请有人告诉我该代码有什么问题。它工作得很好。。非常感谢:)
... ImageUrl = '~/ShowImage.ashx?Name=<%#Eval("UserName") %>' ...
... ImageUrl = '<%# "~/ShowImage.ashx?Name=" + Server.UrlEncode(Eval("UserName").ToString()) %>' ...