Using c# windows form application how to add images which is in database to report viewer (.RDLC)
Couldn't insert images which is in database
- 807 Views
- Last Post 08 September 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
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
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
I hope so you can solve your problem
Can you show me a video it's very difficult to do using instruction please help me
Search
Categories
This Weeks High Earners
-
NormanOW 2
-
hxcxyijniso 2
-
fyaxwiuwuyv 2
-
fylxwiudnwo 2
-
gwcvzftnqsu 2
-
Danielgow 2
-
cxzxvjcdeqs 2
-
Dansiertah 2