I'm trying to build a Form that can insert images to local DB and load them to flowLayoutPanel. Here is the layout of my form, I insert images in 'admin' tab (pic.1).
In 'Browse Photos' tab I try to load photos of a specific city to flowLayoutPanel by clicking the button (pic.2).
Load images according to button pressed
I inserted photos smoothly but got a problem when I tried to load them.
here is my code:
public Frm_MyAlbum()
{
InitializeComponent();
}
private void loadImage (int id, FlowLayoutPanel flp)
{
try
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = Settings.Default.Database1ConnectionString;
SqlCommand command = new SqlCommand();
command.CommandText = $"Select * from Photos where ID = {id}";
command.Connection = conn;
conn.Open();
SqlDataReader DR = command.ExecuteReader();
this.flowLayoutPanel1.Controls.Clear();
while (DR.Read())
{
byte[] bytes = (byte[])DR["Image"];
MemoryStream MS = new MemoryStream(bytes);
PictureBox pics;
pics = new PictureBox();
pics.Image = Image.FromStream(MS);
pics.Size = new Size(200, 160);
pics.SizeMode = PictureBoxSizeMode.StretchImage;
this.flowLayoutPanel1.Controls.Add(pics);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void GetID(string City, FlowLayoutPanel flp)
{
try
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = Settings.Default.Database1ConnectionString;
conn.Open();
SqlCommand command = new SqlCommand();
command.CommandText = $"Select * from Photos where City = {City}";
command.Connection = conn;
SqlDataReader DR = command.ExecuteReader();
while (DR.Read())
{
loadImage((int)DR["PhotoID"], flp);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e) //button London
{
this.flowLayoutPanel1.Controls.Clear();
GetID("London", flowLayoutPanel1);
}
private void button7_Click(object sender, EventArgs e) //button Add to DB
{
try
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = Settings.Default.Database1ConnectionString;
SqlCommand command = new SqlCommand();
command.CommandText = $"Insert into Photos(City, Description, Image) values(@City, @Desc, @Image)";
command.Connection = conn;
byte[] bytes;
MemoryStream MS = new MemoryStream();
this.pictureBox1.Image.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg);
bytes = MS.GetBuffer();
command.Parameters.Add("@City", SqlDbType.Text).Value = this.textBox2.Text;
command.Parameters.Add("@Desc", SqlDbType.Text).Value = this.textBox1.Text;
command.Parameters.Add("@Image", SqlDbType.Image).Value = bytes;
conn.Open();
command.ExecuteNonQuery();
MessageBox.Show("Adding Successfully");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button8_Click(object sender, EventArgs e) //button browse
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
}
When I ran the code, I pressed London (button1) and an exception showed up as "Invalid column name 'London'." VS indicates there's something wrong in the line 'command.CommandText = $"Select * from Photos where City = {City}";'.
Tried many ways to rewrite it but haven't figure it out. What should I do to solve this issue?
Thank you guys in advance!!
Imagedb format should be replaced withvarbinary(max)-- This:bytes = MS.GetBuffer();is very wrong, you need[MemoryStream].ToArray(), notGetBuffer()-- Forcibly re-format the image from whatever it is to JPEG is bad. -- Finally, this:this.flowLayoutPanel1.Controls.Clear();is probably one of the worst things you can do in a WinForms app. But you have other outstanding problems there.