Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi , i want to retrieve an image from database and i wrote codes below . first it worked but i don't know why it gives the error "parameter is not valid".
There is 2 probability . the codes to save image to database is incorrect or the retrieving code is wrong.
by the way as you see at images below when i'm saving image the byte array's lenght is different from the retrieving byte array . i thing when i retrieving image from database the byte array gets the string value "System.byte[]" that its length is 13 character.

Saving codes:
http://sites.google.com/site/myuploadedimages/_/rsrc/1276807131545/home/barna-1/parameterisnotvalid/sabt.JPG

retrieving codes:

http://sites.google.com/site/myuploadedimages/_/rsrc/1276807121094/home/barna-1/parameterisnotvalid/upd.JPG

and this is my codes:
save;
DataRow myRow;
myRow = ds.Tables["users"].Rows[0];
byte[] MyData =null;
MyData = (byte[])myRow["image"];
MemoryStream stream = new MemoryStream(MyData);
stream.Position=0;
f3.pictureBox1.Image = Image.FromStream(stream);
//***************another way that i've tested it but give's the same error
//ImageConverter imgCV = new ImageConverter();
//f3.pictureBox1.Image = imgCV.ConvertFrom(MyData) as Image;
//***************



retrive:
MemoryStream mStream = new MemoryStream();
pictureBox1.Image.Save(mStream, ImageFormat.Bmp);
mStream.Getbuffer();

//i've tried this one too
//byte[] pic=mStream.ToArray();
:sigh:
Posted
Updated 29-Jun-10 7:17am
v2

yeaaaaaaaaaaah!!!!!!!!!!!!!!
i got it . there is no error while reading or retrieving . the problem happens while inserting . but what's wrong ? well , i got it .
my sqlcommand inserting text has got some incorrect suntax. ofcourse we can't say incorrect syntax . when we use sqlcommand like this, :

funcInsert("INSERT INTO users ([name] ,lname, semat ,username, pass,[image], sabtkonande)"  + "values('"  + textBox1_name.Text + "','" + textBox2_lname.Text + "','" + textBox5_job.Text + "','" + textBox3_uname.Text + "','"  + textBox4_pass.Text + "'," +pic + ",'"  + textBox6_sabtkonande.Text+ "')");


we are forced to put values between ' '. then all of values turn to varchar or other string formats. then my "pic" variable that has got memorysteam.getbuffer() value can't put itself value to DB. and puts "pic" as string . for solving this problem i used sqlparameters and my problem solved. like thisone:
string insertComm = "insert into users (name, lname , semat, username "+
",pass , image, sabtkonande)" +
"values(@name, @lname, @semat , @username , @pass ," +
" @image , @sabtkonande)";
rwd.command = new SqlCommand(insertComm, rwd.connection);
rwd.command.Parameters.Add("@name", SqlDbType.NVarChar);
rwd.command.Parameters.Add("@lname", SqlDbType.NVarChar);
rwd.command.Parameters.Add("@semat", SqlDbType.NVarChar);
rwd.command.Parameters.Add("@username", SqlDbType.NVarChar);
rwd.command.Parameters.Add("@pass", SqlDbType.NVarChar);
rwd.command.Parameters.Add("@image", SqlDbType.Image);
rwd.command.Parameters.Add("@sabtkonande", SqlDbType.NVarChar);
rwd.command.Parameters["@name"].Value = textBox1_name.Text;
rwd.command.Parameters["@lname"].Value = textBox2_lname.Text;
rwd.command.Parameters["@semat"].Value = textBox5_job.Text; rwd.command.Parameters["@username"].Value = textBox3_uname.Text;
rwd.command.Parameters["@pass"].Value = textBox4_pass.Text;
rwd.command.Parameters["@image"].Value = pic;
rwd.command.Parameters["@sabtkonande"].Value = textBox6_sabtkonande.Text;
rwd.connection.Open();
rwd.command.ExecuteScalar();
rwd.connection.Close();


:rose:
 
Share this answer
 
Comments
William Winner 29-Jun-10 13:16pm    
by the way, to avoid SQL injection attacks, using parameters is the way to go anyway. Also, when you add the paramater, you can set the value right after instead of on another line as in:
rwd.command.Parameters.Add("@name", SqlDbType.NVarChar).Value = textBox1_name.Text;
oujeboland 30-Jun-10 2:11am    
Great ! thx sir.
what is the solition?

i get this error and not find the solition
 
Share this answer
 
Comments
Sandeep Mewara 2-Sep-12 2:58am    
This is not an answer. Please use 'Have a Question or Comment' link to respond to an answer. It will notify answerer of your comment such that he can come back and reply. Or if needed, use Improve Question link to edit/update your question at anytime.
//BROWSE button
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName;
            }
            pictureBox1.Image = Image.FromFile(textBox1.Text);
            FileInfo fi_image = new FileInfo(textBox1.Text);
            file_length = fi_image.Length;
            FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
            photo_byte = new byte[Convert.ToInt32(file_length)];
            fs.Read(photo_byte, 0, Convert.ToInt32(file_length));
            fs.Close();
        }
        

        //SAVE button
        private void button2_Click(object sender, EventArgs e)
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand cmd = new SqlCommand("INSERT INTO ImageTable VALUES ( @photo )", con);
            cmd.Parameters.Add("@photo", SqlDbType.Image);
            cmd.Parameters["@photo"].Value = photo_byte;
            cmd.ExecuteScalar();
            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("SUCCESS");
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
            combo_load();
        }

        //DISPLAY Button
        private void button3_Click(object sender, EventArgs e)
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }

            try
            {
                SqlCommand cmd1 = new SqlCommand("SELECT photo FROM ImageTable WHERE id = '" + comboBox1.Text + "'", con);
                byte[] disp_image = (byte[])cmd1.ExecuteScalar();
                string disp_image_name = Convert.ToString(DateTime.Now.ToFileTime());
                FileStream fs_disp = new FileStream(disp_image_name, FileMode.CreateNew, FileAccess.Write);
                fs_disp.Write(disp_image, 0, disp_image.Length);
                fs_disp.Flush();
                fs_disp.Close();
                if (pictureBox1.Image != null)
                {
                    pictureBox1.Image.Dispose();
                }
                pictureBox1.Image = Image.FromFile(disp_image_name);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
        }
C#
<pre lang="c#"><pre lang="c#"><pre lang="c#"><pre lang="c#">
 
Share this answer
 
Comments
Raktotpal 2-Sep-12 3:04am    
The Main bug was in Saving the Image not in retrieving the Image.

IMPORTANT:
Combo_load() function is to fill the COMBO BOX control with updated IMAGE_IDs every time you save a new Image in DB
Member 13369299 8-Nov-17 9:24am    
thanx man you solved my problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900