Dale Delacruz

Dale Delacruz

  • NA
  • 72
  • 547

How to populate listview to picturebox?? Pls

Jan 3 2020 4:10 PM
This is my code Error: FileNotFoUnd was Unhandled using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; using System.Globalization; namespace MinPayroll.Employee { public partial class frmemployee : Form { private SqlConnection con = new SqlConnection(@"Data Source=LAPTOP-TMBBDLDB\SQLEXPRESS;Initial Catalog=User;Integrated Security=True;"); private string sql = ""; string filename; public frmemployee() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true, Multiselect = false }) { if (ofd.ShowDialog() == DialogResult.OK) { filename = ofd.FileName; lblFilename.Text = filename; pictureBox1.Image = Image.FromFile(filename); } } } private void btnremove_Click(object sender, EventArgs e) { pictureBox1.Image = null; } private void frmemployee_Load(object sender, EventArgs e) { this.ActiveControl = txtName; loadata(); } private bool checking() { bool result = false; if (txtName.Text.Trim().Length == 0) { MessageBox.Show("Name name is empty!", "INVALID", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtName.SelectAll(); txtName.Focus(); } else if (txtMobile.Text.Trim().Length == 0) { MessageBox.Show("Usernname is empty!", "INVALID", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtMobile.SelectAll(); txtMobile.Focus(); } else if (txtEmail.Text.Trim().Length == 0) { MessageBox.Show("Email is empty!", "INVALID", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtEmail.SelectAll(); txtEmail.Focus(); } else if (txtPno.Text.Trim().Length == 0) { MessageBox.Show("Role is empty!", "INVALID", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtPno.SelectAll(); txtPno.Focus(); } else if (txtPno.Text.Trim().Length == 0) { MessageBox.Show("Role is empty!", "INVALID", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtPno.SelectAll(); txtPno.Focus(); } else if (txtBank.Text.Trim().Length == 0) { MessageBox.Show("Role is empty!", "INVALID", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtBank.SelectAll(); txtBank.Focus(); } else if (txtAdress.Text.Trim().Length == 0) { MessageBox.Show("Role is empty!", "INVALID", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtAdress.SelectAll(); txtAdress.Focus(); } else { result = true; } return result; } private bool ifemployexist(string Name, String Mobile, string Pno) { con.Open(); SqlDataAdapter sda = new SqlDataAdapter("Select 1 From Employee WHERE Name='" + Name + "'and Mobile='" + Mobile + "'and PANno='" + Pno + "'", con); DataTable dt = new DataTable(); sda.Fill(dt); if (dt.Rows.Count > 0) return true; else return false; } byte[] ConvertImagetoBinary(Image img) { using (MemoryStream ms = new MemoryStream()) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); return ms.ToArray(); } } private void cleardata() { txtID.Clear(); txtName.Clear(); txtMobile.Clear(); txtPno.Clear(); dtp.Value = DateTime.Now; txtBank.Clear(); txtAdress.Clear(); txtEmail.Clear(); pictureBox1.Image = null; } private void bunifuFlatButton2_Click(object sender, EventArgs e) { if (checking()) { if (ifemployexist(txtName.Text, txtMobile.Text, txtpan.Text)) { MessageBox.Show("Employee Alreadt Exist"); } else { sql = (@"INSERT INTO Employee(Name, Mobile,Email, PANno, Dob, BankDetails, Adress,Filename, ImageData)VALUES('" + txtName.Text + "','" + txtMobile.Text + "','" + txtEmail.Text + "','" + txtPno.Text + "','" + dtp.Value.ToString("MM/dd/yyyy") + "','" + txtBank.Text + "','" + txtAdress.Text + "','" + filename + "','" + ConvertImagetoBinary(pictureBox1.Image) + "')"); SqlDataAdapter sda = new SqlDataAdapter(sql, con); sda.SelectCommand.ExecuteNonQuery(); if (con.State == ConnectionState.Open) con.Close(); cleardata(); MessageBox.Show("Record save succesfully"); loadata(); } } } private void loadata() { con.Open(); SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Employee", con); DataTable DATA = new DataTable(); con.Close(); sda.Fill(DATA); listView1.Items.Clear(); foreach (DataRow dr in DATA.Rows) { ListViewItem item = new ListViewItem(dr["id"].ToString()); item.SubItems.Add(dr["Name"].ToString()); item.SubItems.Add(dr["Mobile"].ToString()); item.SubItems.Add(dr["Email"].ToString()); item.SubItems.Add(dr["PANno"].ToString()); var dt = DateTime.Parse(dr["Dob"].ToString()); item.SubItems.Add(dt.ToString("MM/dd/yyyy")); item.SubItems.Add(dr["BankDetails"].ToString()); item.SubItems.Add(dr["Adress"].ToString()); item.SubItems.Add(dr["Filename"].ToString()); item.SubItems.Add(dr["ImageData"].ToString()); listView1.Items.Add(item); } } private void listView1_MouseClick(object sender, MouseEventArgs e) { txtID.Text = listView1.SelectedItems[0].SubItems[0].Text; txtName.Text = listView1.SelectedItems[0].SubItems[1].Text; txtMobile.Text = listView1.SelectedItems[0].SubItems[2].Text; txtEmail.Text = listView1.SelectedItems[0].SubItems[3].Text; txtPno.Text = listView1.SelectedItems[0].SubItems[4].Text; DateTime dt = DateTime.ParseExact(listView1.SelectedItems[0].SubItems[5].Text, "MM/dd/yyyy", CultureInfo.InvariantCulture); dtp.Value = dt; txtBank.Text = listView1.SelectedItems[0].SubItems[6].Text; txtAdress.Text = listView1.SelectedItems[0].SubItems[7].Text; lblFilename.Text = listView1.SelectedItems[0].SubItems[8].Text; Here is the error ,?? // pictureBox1.Image= Image.FromFile(listView1.SelectedItems[0].SubItems[9].Text); } }

Answers (1)