Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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.
 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 12-Dec-12 23:51pm
Edited 13-Dec-12 0:30am
v2
Comments
Killzone DeathMan at 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!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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));
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
Insted of this line b = (Byte[])(dr["img"]);
use
b = (Byte[])dr["img"];
  Permalink  

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

  Print Answers RSS
0 Mathew Soji 274
1 BillWoodruff 270
2 Afzaal Ahmad Zeeshan 244
3 Sergey Alexandrovich Kryukov 240
4 Raul Iloc 160
0 OriginalGriff 6,219
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,103
3 Manas Bhardwaj 4,549
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100