So i was learning how to use picturebox and its interaction with database, but i pushed my luck and running out of idea.
So here is my code to open/ load picture from picture box
private void btnopen_Click(object sender, EventArgs e)
{
using(OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.JPG", ValidateNames = true, Multiselect = false })
{
if (ofd.ShowDialog() == DialogResult.OK)
{
filename1 = ofd.FileName;
filename2 = ofd.FileName;
filename1 = Path.GetFileName(filename1);
lblfilename.Text = "/" + filename1 ;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromFile(filename2);
}
}
}
here is my SQL queries,
string sql = string.Format("Insert into TB_PRODUCTS (ID,ITEM_name,price,stock,category,picture) Values('{0}','{1}','{2}','{3}','{4}','{5}')", lblid.Text + txtid.Text, txtdes.Text, txtharga.Text,txtstok.Text,txtkat.Text, **"Application.StartupPath + " + lblfilename.Text**);
OleDbConnection con = new OleDbConnection(koneksi);
con.Open();
OleDbCommand cmd = new OleDbCommand(sql, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data Tersimpan", "Pemberitahuan", MessageBoxButtons.OK, MessageBoxIcon.Information);
tampil();
and here is my datagrid with clicked events
private void DG_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.DG.Rows[e.RowIndex];
string text1 = row.Cells["ID"].Value.ToString();
string retString;
retString = text1.Substring(3);
txtid.Text = retString;
txtdes.Text = row.Cells["ITEM_name"].Value.ToString();
txtharga.Text = row.Cells["price"].Value.ToString();
txtstok.Text = row.Cells["stock"].Value.ToString();
pictureBox1.Image = Image.FromFile(row.Cells["picture"].Value.ToString());
}
}
i intend to combine the label from filename for example the filename will become : "/picture.jpg" and when i add the new file it will automatically going to be added just like this : Application.StartupPath + "/Picture.jpg"
but when debugging, and click the datagrid it shows this error :Additional information: Illegal characters in path.
any idea how to show picture apart from this ? thanks and sorry for the messy thread.
As Bizhan advised, you can use Path.Combine Method to connect string. Also I noticed that your code "Application.StartupPath +"
, maybe you need to remove the double quotes.
After that, you only need to set lblfilename.Text
to Picture.jpg
to access the picture.
string sql = string.Format("Insert into TB_PRODUCTS (ID,picture) Values('{0}','{1}')", 1, Path.Combine(new string[]{ Application.StartupPath, lblfilename.Text}));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.