locked
insert image into access bt it not insert image into access database what i do RRS feed

  • Question

  • User-1441176230 posted

     

    
    
    
     private void up_Click(object sender, EventArgs e)
            {
               try
               {
                   if(textBox1.Text.Trim()=="")
                    {
                        MessageBox.Show("please select image to upload into database");
                        return;
                    }
                    byte[] FileByte = null;
                    String ffname = textBox1.Text.Substring(textBox1.Text.LastIndexOf("\\") + 1, textBox1.Text.Length - (textBox1.Text.LastIndexOf("\\") + 1));
                    string path = textBox1.Text;
                    FileStream fs = new FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);
                    long allbytes = new FileInfo(path).Length;
                    FileByte = br.ReadBytes((Int32)allbytes);
                    fs.Close();
                    fs.Dispose();
                    br.Close();
                    cn.Open();
                    cmd = new OleDbCommand("Insert into bio (name,dob,age,nof,contact,addr,quali,wp,doj,bn,epf,nomname,nomdb,nomrela,nomaddr,faname,fadob,farela,faaddr,image) values (@name,@dob,@age,@nof,@contact,@addr,@quali,@wp,@doj,@bn,@epf,@nomname,@nomdb,@nomrela,@nomaddr,@faname,@fadob,@farela,@faaddr,@image)", cn);
                    cmd.Parameters.AddWithValue("@name",nm.Text);
                    cmd.Parameters.AddWithValue("@dob",dob.Text );
                    cmd.Parameters.AddWithValue("@age",age.Text );
                    cmd.Parameters.AddWithValue("@nof",fname.Text );
                    cmd.Parameters.AddWithValue("@contact",contact.Text );
                    cmd.Parameters.AddWithValue("@addr",addr .Text);
                    cmd.Parameters.AddWithValue("@quali",quali.Text );
                    cmd.Parameters.AddWithValue("@wp",wp.Text );
                    cmd.Parameters.AddWithValue("@doj",doj.Text );
                    cmd.Parameters.AddWithValue("@bn",bname.Text );
                    cmd.Parameters.AddWithValue("@epf",epf.Text );
                    cmd.Parameters.AddWithValue("@nomname",nomname.Text );
                    cmd.Parameters.AddWithValue("@nomdb",nomdb.Text );
                    cmd.Parameters.AddWithValue("@nomrela",nomrela.Text );
                    cmd.Parameters.AddWithValue("@nomaddr",nomaddr.Text );
                    cmd.Parameters.AddWithValue("@faname",faname.Text );
                    cmd.Parameters.AddWithValue("@fadob",fadob.Text );
                    cmd.Parameters.AddWithValue("@farela",farela.Text );
                    cmd.Parameters.AddWithValue("@faaddr",faaddr.Text);
                   cmd.Parameters.AddWithValue("@image",FileByte.Length);
                    cmd.ExecuteNonQuery();
                    cn.Close();
                    MessageBox.Show("update sucessfully");
                    wp.Text = "";
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex + " ");
    
                }
            }
    
    
    private void browser_Click(object sender, EventArgs e)
            {
                OpenFileDialog fd = new OpenFileDialog();
                fd.Title = "select file to be zip";
                fd.Filter = "JPG Files|*.jpg|JPEG Files|*jpeg";
                if (fd.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text = fd.FileName.ToString();
                    pictureBox1.Image = Image.FromFile(fd.FileName);
    
                }
    
            }

    Thursday, May 7, 2020 9:45 AM

All replies

  • User283571144 posted

    Hi balavelan,

    I found you stored the FileByte.Length not  FileByte  into the database. As far as I know, the FileByte.Length is the length of the FileByte.

    If your image column type is byte array, I suggest you could try to use FileByte instead of FileByte.Length.

    If your image column type is not byte array, I suggest you could tell us the type and then we could find the right solution. 

    Best Regards,

    Brando

    Saturday, May 9, 2020 5:13 AM