0

I have this code from a tutorial on the internet i moved step by step with the tutorial but my code dosent work and gives me this error: An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll

Additional information: Parameter is not valid.

here is my 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.Data.OleDb;
using System.IO; 

namespace image_retriver
{
    public partial class Form1 : Form
    {
        OleDbConnection connection = new OleDbConnection();
        OleDbDataAdapter adapter;
        DataTable localdatatable=new DataTable ();
        int row_positon = 0;
        int row_number = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void connect_to_database()
        {
            connection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydb.mdb";
            connection.Open();
            adapter = new OleDbDataAdapter("SELECT * FROM `Table1`",connection);
            adapter.Fill(localdatatable);
            if (localdatatable.Rows.Count > 0)
            {
                row_positon = localdatatable.Rows.Count;
            }

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            connect_to_database();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox2.Image = Image.FromFile(openFileDialog1.FileName);
                button5.Enabled = true;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            storedata(converimagetobytes(pictureBox2.Image));
        }

        private byte[] converimagetobytes(Image input)
        {
            Bitmap bm = new Bitmap(input);
            MemoryStream mystream = new MemoryStream();
            bm.Save(mystream, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] imageasbytes = mystream.ToArray();
            return imageasbytes;
        }
        private void storedata(byte[] image)
        {
            if (connection.State.Equals(ConnectionState.Closed))
                connection.Open();
            try
            {

                MessageBox.Show("saving at row :" + row_positon.ToString());
                OleDbCommand oledbinsert = new OleDbCommand("Insert INTO Table1 (Img) Values('@myimg')", connection);
                OleDbParameter imagepram = oledbinsert.Parameters.AddWithValue("@myimg", SqlDbType.Binary);
                imagepram.Value = image;
                imagepram.Size = image.Length;
                int afrow = oledbinsert.ExecuteNonQuery();
                MessageBox.Show("image added");
                row_positon++;

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
                MessageBox.Show(e.StackTrace.ToString());

            }
            finally
            {
                refreshcon();
            }
        }
        private void refreshcon()
        { 
            if(connection.State.Equals(ConnectionState.Open))
            {
            connection.Close();
             localdatatable.Clear();
                connect_to_database();

            }

        }
        private Image readimage()
        {
            Image fetched;
            if (row_number >= 0)
            {
                byte[] fetchedimgbytes = (byte[])localdatatable.Rows[row_number]["Img"];
                MemoryStream stream = new MemoryStream(fetchedimgbytes);
                fetched = Image.FromStream(stream,true,true);//here is where i get the error
                return fetched;

            }
            else
            {
                MessageBox.Show("error");
                return null;
            }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            refreshcon();
            row_number = 0;
            pictureBox2.Image = readimage();
            button2.Enabled = true;
            button3.Enabled = true;

        }

    }
}
mohamed
  • 135
  • 1
  • 14

1 Answers1

1

The quotes around the parameter name are messing up the insert query:

Change this

new OleDbCommand("Insert INTO Table1 (Img) Values ('@myimg')", connection);

to

new OleDbCommand("Insert INTO Table1 (Img) Values (@myimg)", connection);
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • thanks that solved the problem but this database was just a sample the real database dosent work want to see it ? – mohamed Sep 11 '15 at 18:19
  • @user3685700 That would be a different question, and harder to document. You just have to see what the difference is between the two databases and the code you are using against them. – LarsTech Sep 11 '15 at 18:24
  • @user3685700 Is the "real" database essentially the same as the sample you posted in your [other question](http://stackoverflow.com/q/32508117/2144390)? – Gord Thompson Sep 11 '15 at 18:28
  • @GordThompson no it is not it is a database with a .style ext. that is not the problem it gets read and everything is ok the problem is that the long binary data filed i dont know what is it but mainly it is an image this data base was given to me by someone and asked to extract the images – mohamed Sep 11 '15 at 18:33
  • @user3685700 What kind of database is the .style file? Make a copy of it and change the extension to .mdb and see if you can open it. – LarsTech Sep 11 '15 at 18:35
  • @LarsTech the .style database is an arcgis symbols database and i can open it in mdb but cant read the symbol into an image – mohamed Sep 11 '15 at 18:44
  • @user3685700 Yeah, that's a whole different problem. If you need help, you will need to ask a new question and document that properly. – LarsTech Sep 11 '15 at 18:48