简体   繁体   English

无法将类型为“ System.Byte []”的对象转换为类型为“ System.String []”的对象

[英]Unable to cast object of type 'System.Byte[]' to type 'System.String[]'

Hello I'm trying to pass image converted from Byte[] to String[] and display it in ReportViewer Image as following: 您好,我正在尝试将从Byte[]转换为String[] image传递到ReportViewer Image ,如下所示:

String[] dataImage;
private void showLogo()
{
    try
    {
        SqlDataAdapter dataAdapter = new SqlDataAdapter( new SqlCommand("SELECT logo
                                           FROM company WHERE id = 1", spojeni));
        DataSet dataSet = new DataSet();
        dataAdapter.Fill(dataSet);
        if (dataSet.Tables[0].Rows.Count == 1)
        {
            dataImage = new String[0];
            dataImage = (String[])(dataSet.Tables[0].Rows[0]["logo"]);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(""+ex);
    }
}

And this is the ReportViewer parameter: 这是ReportViewer参数:

ReportParameter[] parameter = new ReportParameter[24];
parameter[23] = new ReportParameter("rp_logo", dataImage );
this.reportViewer1.LocalReport.SetParameters(parameter);
this.reportViewer1.RefreshReport();

But I get following Exception Unable to cast object of type 'System.Byte[]' to type 'System.String[]' 但是我得到以下Exception无法将类型为“ System.Byte []”的对象转换为类型为“ System.String []”的对象

Can someone help me solve this? 有人可以帮我解决这个问题吗?

Thank you for your time. 感谢您的时间。

Given ReportViewer requires a path to an image and not the actual image data itself, your best bet here is to save your byte[] to disk and reference this in the parameter instead eg 给定ReportViewer需要图像的路径,而不是实际图像数据本身,此处最好的选择是将byte[]保存到磁盘并在参数中引用它,例如

using (var cmd = new SqlCommand("SELECT logo FROM company WHERE id = 1", spojeni))
using (var dataAdapter = new SqlDataAdapter(cmd))
using (var dataSet = new DataSet())
{
    dataAdapter.Fill(dataSet);
    if (dataSet.Tables[0].Rows.Count == 1)
    {
        // generate temp file destination
        dataImage = System.IO.Path.GetTempFileName() + ".jpg"; // use whatever extension you expect the file to be in
        File.WriteAllBytes(dataImage, (byte[])dataSet.Tables[0].Rows[0]["logo"]); // save image to disk
    }

}

byte to string conversion can be done the following way 字节到字符串的转换可以通过以下方式完成

 Encoding.Ascii.GetString(yourByteArray)

if you need a further conversion you can do somthing like that: 如果您需要进一步的转换,可以执行以下操作:

Encoding.Ascii.GetString(yourByteArray).Select(c => c as string).ToArray()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的对象 - Unable to cast object of type 'System.Byte' to type 'System.String' 无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”的对象。 发布后发生错误 - Unable to cast object of type 'System.String' to type 'System.Byte[]'. Error after publish (已解决)无法将“system.byte”类型的 object 转换为“system.string”类型(组合框填充 PictureBox) - (Solved) unable to cast object of type 'system.byte ' to type 'system.string' (combobox populate PictureBox) Linq-收到错误“无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”。” - Linq - Receiving error “Unable to cast object of type 'System.String' to type 'System.Byte[]'.” 无法将类型为“System.String”的 object 转换为类型“System.Byte []”错误 MYSQL NET CORE 3.1 - Unable to cast object of type 'System.String' to type 'System.Byte[]' Error MYSQL NET CORE 3.1 无法将“System.Byte”类型的 object 转换为“System.String”类型。 - Unable to cast object of type 'System.Byte' to type 'System.String'.' 从数据库读取数据导致无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的错误 - Reading data from database gives Unable to cast object of type 'System.Byte' to type 'System.String' error 无法将System.string强制转换为System.Byte [] - Unable to cast System.string to System.Byte[] 反序列化对象时,无法将类型为System.String的对象转换为类型为System.Byte [] - Cannot convert object of type System.String to type System.Byte[] when deserializing object 无法将类型“ System.String”强制转换为类型“ System.Object” - Unable to cast the type 'System.String' to type 'System.Object'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM