Click here to Skip to main content
15,913,263 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there

I develop a code to show the image from my database when i click button(btnImgFrmDB)
it fail and i tried a lot from internet but still the same problem.

Error: Parameter is not valid.error showing where i wrote the line in bold and underline

Code:
private void btnImgFrmDB_Click_1(object sender, EventArgs e)
        {
            
           if (textImg.Text != "")
            {
                string sqlstr = "Select Image from SqlToImage where ImgName='" + textImg.Text + "' ";
                SqlConnection conn = new SqlConnection(constring);
                conn.Open();
                SqlCommand cmd = new SqlCommand(sqlstr, conn);
                object img = cmd.ExecuteScalar();
                byte[] MyImg = (byte[])img;
                Stream s = null;
                MemoryStream ms = new MemoryStream(MyImg);
                ms.Read(MyImg, 0, MyImg.Length);
                s = ms;                
                pictureBox1.Image = Image.FromStream(ms);
               //Also tried the below Code and having same problem

                byte[] MyData = new byte[2048];
                MemoryStream ms=null;
                while (dr.Read())
                {
                    ms = new MemoryStream((byte[])dr[0]);
                }
                    Stream s = ms;
                    s.Read(MyData, 0, 2048);
                    pictureBox1.Image=Image.FromStream(s);
                    myPictureBox.Image = Image.FromStream(ms);
                    ms.Close();

            }
Posted
Updated 18-Jul-11 23:00pm
v3

1 solution

You try this code:-
private void button3_Click(object sender, EventArgs e)
        {
            if (textImg.Text != "")
            {
                string sql = "Select Image from SqlToImage where ImgName='" + textImg.Text + "' ";
                SqlConnection connection = new SqlConnection();
                connection.ConnectionString = connectionString;
                connection.Open();
                FileStream file;
                BinaryWriter bw;
                int bufferSize = 100;
                byte[] outbyte = new byte[bufferSize];
                long retval;
                long startIndex = 0;
                savedImageName = textImg.Text;
                SqlCommand command = new SqlCommand(sql, connection);
                SqlDataReader myReader = command.ExecuteReader(CommandBehavior.SequentialAccess);
                while (myReader.Read())
                {
                    //string fileName = @"c:\" +textImg.Text+ ".jpeg";
                    /*file = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                    bw = new BinaryWriter(file);*/
                    file = new FileStream(savedImageName, FileMode.OpenOrCreate, FileAccess.Write);
                    bw = new BinaryWriter(file);
                    startIndex = 0;
                    retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
                    while (retval == bufferSize)
                    {
                        bw.Write(outbyte);
                        bw.Flush();
                        startIndex += bufferSize;
                        retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
                    }
                    bw.Write(outbyte, 0, (int)retval - 1);
                    bw.Flush();
                    bw.Close();
                    file.Close();
                }
                connection.Close();
                curImage = Image.FromFile(savedImageName);
                pictureBox1.Image = curImage;
                pictureBox1.Invalidate();
                connection.Close();
            }
            
        }
 
Share this answer
 

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



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