[英]Displaying Bitmap Image from Database into GridView with ASP.NET
I am currently trying to display the a QR code bitmap image that is being stored as varbinary in the SQL database into a gridview, and I have referred to this link: [https://www.aspsnippets.com/Articles/Display-images-from-SQL-Server-Database-in-ASP.Net-GridView-control.aspx]我目前正在尝试将在 SQL 数据库中存储为 varbinary 的 QR 码 bitmap 图像显示到 gridview 中,并且我已参考此链接: -来自-SQL-Server-Database-in-ASP.Net-GridView-control.aspx]
However, I am unable to display the image properly in the gridview table and I couldn't seem to figure out the error with it.但是,我无法在 gridview 表中正确显示图像,我似乎无法弄清楚它的错误。
This is my code for the gridview code behind:这是我后面的 gridview 代码的代码:
if (!this.IsPostBack)
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM PARCEL", conn))
{
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView dr = (DataRowView)e.Row.DataItem;
string imageUrl = "data:image/png;base64," + Convert.ToBase64String((byte[])dr["QRCode"]);
(e.Row.FindControl("QRCode") as Image).ImageUrl = imageUrl;
}
}
The markup for gridview: gridview 的标记:
<asp:GridView
ID="GridView1"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="ConsignmentNumber"
AllowPaging="True"
HorizontalAlign="Center"
ViewStateMode="Enabled">
<Columns>
<asp:BoundField DataField="ConsignmentNumber" HeaderText="ConsignmentNumber" ReadOnly="True" SortExpression="ConsignmentNumber" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="QRCode" runat="server" HeaderText="QRCode" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I would like to know if there is any solution or suggestion that I can apply in order to display the images properly.我想知道是否有任何解决方案或建议可以应用以正确显示图像。
Judging by the error you are experiencing, you have defined a DataSourceId
in your markup AND are setting the datasource in codebehind.从您遇到的错误来看,您已经在标记中定义了一个
DataSourceId
并且正在代码隐藏中设置数据源。 You can do either, but not both.你可以做任何一个,但不能同时做。 That is what the error is telling you.
这就是错误告诉你的。
<asp:GridView ID="GridView1" runat="server"
DataSourceID="yourdatasource">
<Columns>
<!-- ... -->
</Columns>
</asp:GridView>
Find the DataSourceID
in your markup and remove it, as you are setting it in your code-behind cs file:在您的标记中找到
DataSourceID
并将其删除,就像您在代码隐藏 cs 文件中设置它一样:
GridView1.DataSource = dt;
GridView1.DataBind();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.