Hello I'm trying to pass image
converted from Byte[]
to String[]
and display it in ReportViewer Image
as following:
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:
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[]'
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
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.