无法显示特定用户的图像

时间:2014-04-22 11:52:51

标签: c# asp.net gridview

我在asp.net工作,我想显示一个Profile_ID为1的用户的图片。我正在以varbinary格式将图片直接保存到数据库中。并在gridview中检索它。我面临的问题是,当用户登录并点击时,"查看照片"按钮,如果用户上传了4张不同的图片,相同的图片会出现四次。它没有拍摄用户上传的所有四张照片。我正在使用处理程序。

这是我的表规范: 表名:(照片) 列:Photo_ID,Profile_ID(外键),照片

下面是我的GridView代码

     <asp:Image ID="Image1" runat="server" ImageUrl='<%# "ThumbNail.ashx?proid="+ Eval("Profile_ID") %>' Height="200px" Width="200px"/>

这就是我将数据绑定到网格

的方式
        SqlConnection connection = new SqlConnection(strCon);
        SqlCommand command = new SqlCommand("SELECT Photo_ID,Profile_ID from [Photo] where Profile_ID=" +Session["profileid"], connection);
        SqlDataAdapter daimages = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        daimages.Fill(dt);
        gridview.DataSource = dt;
        gridview.DataBind();

这是我正在做检索图像的主要工作的处理程序的编码

       string pid = context.Request.QueryString["proid"];
       string query = "select Photo from Photo where  Profile_ID=" + pid;
       SqlDataAdapter adpt = new SqlDataAdapter(query, connection);
       DataTable dt = new DataTable();
       adpt.Fill(dt);
       for (int i = 0; i < dt.Rows.Count; i++)
    {

        context.Response.BinaryWrite((byte[])(dt.Rows[i]["Photo"]));
    }

1 个答案:

答案 0 :(得分:0)

该表似乎同时包含Photo_IDProfile_ID,但您在处理程序中使用Profile_ID

我会使用Photo_ID作为处理程序中的参数,

<%# "ThumbNail.ashx?photoid="+ Eval("Photo_ID") %>' Height="200px" Width="200px"/>

并将处理程序更新为此。

   string pid = context.Request.QueryString["photoid"];
   string query = "select Photo from Photo where  Photo_ID=" + pid;