In This Blog, You Can Have The Complete Source Code Of This Application In Which We Can Add, Read, Update, Delete Images From Database In Windows Forms C#.
Output

Form1.cs Source Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using IMAGES_WITH_DB.Properties;
namespace IMAGES_WITH_DB
{
public partial class Form1 : Form
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
public Form1()
{
InitializeComponent();
BindGridView();
BindComboBox();
}
private void button5_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Select Image";
//ofd.Filter = "PNG FILE (*.png) | *.png|JPG FILE(*.jpg) | *.jpg|BMP FILE(*.bmp) | *.bmp|GIF FILE(*.gif) | *.gif";
ofd.Filter = "Image File (*.png;*.jpg;*.bmp;*.gif) | *.png;*.jpg;*.bmp;*.gif|All files (*.*)|*.*";
if(ofd.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(ofd.FileName);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
string query = "insert into student_details values(@id,@name,@age,@img)";
SqlCommand cmd = new SqlCommand(query,con);
cmd.Parameters.AddWithValue("@id",textBox1.Text);
cmd.Parameters.AddWithValue("@name", textBox2.Text);
cmd.Parameters.AddWithValue("@age", numericUpDown1.Value);
cmd.Parameters.AddWithValue("@img", SavePhoto());
con.Open();
int a = cmd.ExecuteNonQuery();
if(a > 0)
{
MessageBox.Show("DATA INSERTED !!");
BindGridView();
ResetControls();
BindComboBox();
}
else
{
MessageBox.Show("DATA NOT INSERTED !!");
}
con.Close();
}
private byte[] SavePhoto()
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
return ms.GetBuffer();
}
void BindGridView()
{
SqlConnection con = new SqlConnection(cs);
string query = "select * from student_details";
SqlDataAdapter sda = new SqlDataAdapter(query,con);
DataTable data = new DataTable();
sda.Fill(data);
dataGridView1.DataSource = data;
DataGridViewImageColumn dgv = new DataGridViewImageColumn();
dgv = (DataGridViewImageColumn)dataGridView1.Columns[3];
dgv.ImageLayout = DataGridViewImageCellLayout.Stretch;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView1.RowTemplate.Height = 50;
}
private void button4_Click(object sender, EventArgs e)
{
ResetControls();
}
void ResetControls()
{
textBox1.Clear();
textBox2.Clear();
numericUpDown1.Value = 0;
pictureBox1.Image = Resources.no_image_available;
}
private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
numericUpDown1.Value = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[2].Value);
pictureBox1.Image = GetPhoto((byte[])dataGridView1.SelectedRows[0].Cells[3].Value);
}
private Image GetPhoto(byte[] photo)
{
MemoryStream ms = new MemoryStream(photo);
return Image.FromStream(ms);
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
string query = "update student_details set id = @id, name = @name, age = @age, picture = @img where id = @id";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", textBox1.Text);
cmd.Parameters.AddWithValue("@name", textBox2.Text);
cmd.Parameters.AddWithValue("@age", numericUpDown1.Value);
cmd.Parameters.AddWithValue("@img", SavePhoto());
con.Open();
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
MessageBox.Show("DATA UPDATED !!");
BindGridView();
ResetControls();
BindComboBox();
}
else
{
MessageBox.Show("DATA NOT UPDATED !!");
}
con.Close();
}
private void button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
string query = "delete from student_details where id = @id";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", textBox1.Text);
con.Open();
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
MessageBox.Show("DATA DELETED !!");
BindGridView();
ResetControls();
BindComboBox();
}
else
{
MessageBox.Show("DATA NOT DELETED !!");
}
con.Close();
}
private void label9_Click(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
void BindComboBox()
{
comboBox1.Items.Clear();
SqlConnection con = new SqlConnection(cs);
string query = "select * from student_details";
SqlCommand cmd = new SqlCommand(query,con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
int id = dr.GetInt32(0);
comboBox1.Items.Add(id);
}
con.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedItem.ToString() != null)
{
int id = Convert.ToInt32(comboBox1.SelectedItem.ToString());
SqlConnection con = new SqlConnection(cs);
string query = "select * from student_details where id = @id";
SqlDataAdapter sda = new SqlDataAdapter(query,con);
sda.SelectCommand.Parameters.AddWithValue("@id",id);
DataTable data = new DataTable();
sda.Fill(data);
if(data.Rows.Count > 0)
{
textBox3.Text = data.Rows[0]["name"].ToString();
textBox4.Text = data.Rows[0]["age"].ToString();
MemoryStream ms = new MemoryStream((byte[])data.Rows[0]["picture"]);
pictureBox2.Image = new Bitmap(ms);
}
}
}
}
}
App.config Source Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="dbcs" connectionString="Data Source=MOHAMMADADIL-PC;Initial Catalog=WINFORM_DB;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Image Used In This Project

FILTER PROPERTY PATTERN
FOR PNG FORMAT: "PNG FILE (*.png) | *.png"
FOR JPG FORMAT: "JPG FILE (*.jpg) | *.jpg"
FOR BMP FORMAT: "BMP FILE (*.bmp) | *.bmp"
FOR GIF FORMAT: "GIF FILE (*.gif) | *.gif"
FOR 4 IMAGE FORMATS: "Image File (*.png;*.jpg;*.bmp;*.gif) | *.png;*.jpg;*.bmp;*.gif"
FOR ALL FILES: "All files (*.*)|*.*"
CREATING COLUMN FOR IMAGE
DataGridViewImageColumn dgv = new DataGridViewImageColumn();
dgv = (DataGridViewImageColumn)dataGridView1.Columns[3];
dgv.ImageLayout = DataGridViewImageCellLayout.Stretch;
ADJUSTING ALL COLUMNS IN ONE DATA GRID VIEW WINDOW
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
INCREASING THE HEIGHT OF ROWS
dataGridView1.RowTemplate.Height = 50;
Click Below Link to Download Source Code Of This Blog
https://www.mediafire.com/file/bk6rrpsu8aycyvv/IMAGES_WITH_DB.rar/file
No responses yet