问题描述
您好我必须在下拉列表中选择学生ID时在图像框上显示学生图像。图像以db2中的二进制格式存储。我想在不使用通用http处理程序的情况下显示图像。使用下面给出的代码生成一个错误。错误是无法将类型字符串转换为byte []。请帮帮我。
代码:
Hi I have to display student image on image box while selecting student ID from drop down list. The image is store in binary format in db.I want to display the image without using generic http handler. While using the given below code generation one error. The error is "Cannot convert type string to byte[]". Please help me.
Code:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet1TableAdapters.TextBoxTableTableAdapter tx;
tx = new DataSet1TableAdapters.TextBoxTableTableAdapter();
DataTable dt = new DataTable();
dt = tx.GetstudData(int.Parse(DropDownList1.SelectedValue));
foreach (DataRow row in dt.Rows)
{
TextBox1.Text = (row["FirstName"].ToString());
TextBox2.Text = (row["SecondName"].ToString());
byte[] barrImg = (byte[])(row["StudImage"].ToString()); // error shown here
string base64String = Convert.ToBase64String(barrImg , 0, barrImg.Length);
Image1.ImageUrl = + base64String;
}
}
SQL查询:
SQL Query:
SELECT FirstName, SecondName, StudentImage FROM TextBoxTable WHERE (Id = @Id)
Aspx来源:
Aspx Source:
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" />
</div>
推荐答案
string s = ...
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(s);
但是......除非你有点傻,否则你的图像根本不应该在数据库的基于字符串的列中:它应该在而是基于字节的数据类型。
见这里: [] - 它讨论了如何正确插入和提取数据库中的图像。
But...unless you have been a bit silly, your image should not be in a string based column in your database at all: it should be in a byte based datatype instead.
See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it talks about how to insert and extract images from your database correctly.
这篇关于无法将'string'类型转换为'byte []'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!