【问题标题】:Cannot show the images of a particular user无法显示特定用户的图像
【发布时间】:2014-04-22 11:52:50
【问题描述】:

我在 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"]));
    }

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    该表似乎同时具有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;
    

    【讨论】:

    • 通过使用它,如何仅获取该特定用户上传的那些图片?因为我想要根据 Profile_ID 的图片(由特定的 Profile_ID 上传)
    • 您已经通过网格的数据绑定处理了这一点。 SqlCommand command = new SqlCommand("SELECT Photo_ID,Profile_ID from [Photo] where Profile_ID=" +Session["profileid"], connection);
    • 太棒了!请将此标记为答案,以便其他人可以搜索。
    • 我接受了你的回答,但不能投票,因为它需要很高的声誉:D
    猜你喜欢
    • 2013-12-31
    • 2021-04-29
    • 2015-03-18
    • 2011-01-12
    • 2013-02-10
    • 1970-01-01
    相关资源
    最近更新 更多