0

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.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Marek
  • 3,555
  • 17
  • 74
  • 123
  • 5
    Why do you need string[] in first place? Isn't that byte[] an image? – Rubens Farias Oct 14 '13 at 11:14
  • @Rubens Isn't it impossible to pass byte[] as reportviewer parameter? – Marek Oct 14 '13 at 11:16
  • 1
    I never used it; googling, seems you'll need to create a physical file and use it, passing you image full path. – Rubens Farias Oct 14 '13 at 11:19
  • Also, take a look here: http://stackoverflow.com/questions/13396798/report-viewer-pass-image-from-form-possible – Rubens Farias Oct 14 '13 at 11:19
  • Are you sure you don't mean `byte[]` to `string`? – James Oct 14 '13 at 11:21
  • @James To be honest, I'm looking for most comfortable way to show that image in ReporViewer. I was told that string array might be a good way to do so. – Marek Oct 14 '13 at 11:23
  • 2
    @Marek as Rubens has already pointed out `ReportViewer` expects a path to a file on Disk - I suggest you save the `byte[]` to disk as a temp file and then pass the path as the parameter value. If `ReportViewer` takes an in-memory copy of the image you can then ditch the temp file. – James Oct 14 '13 at 11:28
  • @James Thanks for that. Will you please help me with saving that byte[] as temp file? And then recall the temp file? – Marek Oct 14 '13 at 11:48

2 Answers2

2

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 e.g.

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
    }

}
James
  • 80,725
  • 18
  • 167
  • 237
  • Thank you so much for this kind of help! Unfortunetly In the very last line I get following error: `The best overloaded method match for System.IO.FIle.WriteAllBytes}string, byte[]]` has some invalid arguments. AND `Cannot convert from `object` to `byte`. May I ask for help ? – Marek Oct 14 '13 at 12:06
  • Thanks a lot but it seems that the errors are still there. Isn't it because the `dataImage` is `String[]` ? Should I use string instead? – Marek Oct 14 '13 at 12:12
  • And one last Q. is it correct then pass the parameter like this `parameter[23] = new ReportParameter("rp_logo", dataImage );` ? – Marek Oct 14 '13 at 12:17
  • @Marek yep that part doesn't change, `dataImage` is now set to the file path, not the data. – James Oct 14 '13 at 12:18
1

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()
Yaugen Vlasau
  • 2,148
  • 1
  • 17
  • 38
  • Hello I have tried that finally it runs well but in the `ReporViewer's Image` is `ErrorImage` picture. Does it mean that it wasn`t passed correctly? – Marek Oct 14 '13 at 11:27