Couldn't insert images which is in database

  • 807 Views
  • Last Post 08 September 2018
[email protected] posted this 30 August 2018

Using c# windows form application how to add images which is in database to report viewer (.RDLC)

Order By: Standard | Newest | Votes
admin posted this 30 August 2018

Ok. I'll use the Northwind database to solve the problem can't insert image from database into rdlc report.

To practice demo, you should insert images to the picture column of your categories table, then you can use Entity Framework to update data of Picture column in categories table.

If you don't know Entity Framework you can view the post Getting Started with Entity Framework Database First

public byte[] ImageToByteArray(Image img)
{
    using (MemoryStream ms = new MemoryStream())
    {
        img.Save(ms, img.RawFormat);
        return ms.ToArray();
    }
}

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "PNG|*.png";
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        NorthwindEntities db = new NorthwindEntities();
        var cat = db.Categories.Find(1);
        if (cat != null)
        {
            cat.Picture = ImageToByteArray(Image.FromFile(ofd.FileName));
        }
        db.SaveChanges();
    }
    MessageBox.Show("Updated!");
}

From your visual studio create a connect to the Northwind database, then create a new dataset with name is Data

You need to drag your categories table to your Dataset

c# dataset

Create a RDLC report, then add a datasource to your report with name is DataSource

Remember select your report datasource is your categories table

Drag an Image control from the report toolbox to your report, then change the image properties as the following

show image to rdlc report from database

Add a report viewer control to your windows forms application, then set datasource for the ReportViewer is your categories table

private void Form1_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'Data.Categories' table. You can move, or remove it, as needed.
    this.CategoriesTableAdapter.Fill(this.Data.Categories);
    this.reportViewer1.RefreshReport();
}

As you can see, the visual studio automatically generate your code as above

Run your project, you can see your image has been loaded

show image in rdlc report from sql database

I hope so you can solve your problem

[email protected] posted this 08 September 2018

Can you show me a video it's very difficult to do using instruction please help me

Close