Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I keep getting this error: parameter not valid while retreiving the image from the database.I am using the following code.Any help would really be appreciated.
C#
try
           {
               if (con.State != ConnectionState.Open)
               {
                   con.Open();
               }
               SqlCommand cmd1 = new SqlCommand("select * from hbet_info where id='"+textBox1.Text+"'", con);
               SqlDataReader dr = cmd1.ExecuteReader();
               if (dr.Read())
               {
                   textBox5.Text = dr["NAME"].ToString();
                   //textBox1.Text = dr["id"].ToString();
                   byte[] b = new byte[0];
                   b = (Byte[])(dr["img"]);
                  // ms.Seek(0, SeekOrigin.Begin);
                   MemoryStream ms = new MemoryStream(b);

                   pictureBox1.Image = Image.FromStream(ms);

               }
               dr.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
       }
Posted
Updated 12-Dec-12 23:30pm
v2
Comments
Killzone DeathMan 13-Dec-12 5:02am    
I am not sure but:
textBox5.Text = dr.GetString("NAME");

instead of:
textBox5.Text = dr["NAME"].ToString();

Right now, I have nothing to try the code... sorry!

C#
public class Document1
        {        
            public byte[] DocContent { get; set; }
        }

public byte[] Attachmnt;
Document1 objDoc = new Document1();
objDoc.DocContent = Attachmnt;//here u will get ur date into byte[] format


File.WriteAllBytes(FileName, objDoc.DocContent);
                writeStream = new FileStream(FileName, FileMode.Create);
                BinaryWriter writeBinay = new BinaryWriter(writeStream);
                writeBinay.Write(objDoc.DocContent);
                writeBinay.Close();
                writeStream.Close();


By this ur data will be created into file, now u can open this file select all data from this file and display them into textbox,after than u can also delete this file.
 
Share this answer
 
SqlCommand cmd1 = new SqlCommand("select * from hbet_info where id='"+textBox1.Text+"'", con);

=> Is the id field an integer one ?
If so, you should NOT put its value between quotes.
Moreover, it is a very very bad habit to construct sql requests with string concatenations.

Better :
SqlCommand cmd1 = new SqlCommand("select * from hbet_info where id=@id", con);
cmd1.Parameters.AddWithValue("id", int.Parse(textBox1.Text));
 
Share this answer
 
Hi,
Insted of this line b = (Byte[])(dr["img"]);
use
b = (Byte[])dr["img"];
 
Share this answer
 

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