Click here to Skip to main content
15,886,071 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
in my c# application have store image into database and retrieve images from database to picture box.
image insertion is successful in to the data base but image read not possible.one error occurs "parameter is not valid"
// my c# code is here
// the code for insert image in to the database is below

C#
FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
byte[] imgbnry = new byte[fs.Length];

fs.Read(imgbnry, 0,imgbnry.Length);
fs.Close();
string str = "insert into tbl_img (id,photo)values('" + textBox3.Text + "','@img')";
SqlConnection con = new SqlConnection(@"DataSource=.\SQLEXPRESS;AttachDbFilename=C:\Users\Keltron_Project\Documents\sampl_pi.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;");
con.Open();
SqlCommand cmd = new SqlCommand(str, con);

cmd.Parameters.AddWithValue("@img", imgbnry);
int n = cmd.ExecuteNonQuery();
if (n > 0)
{
    MessageBox.Show("Successfully inserted");
}
else
{
    MessageBox.Show("Not inserted");
}



// the code for read image from database is below

C#
string str = "select photo from tbl_img where id='"+textBox2.Text+"'";
SqlConnection con = new SqlConnection(@"DataSource=.\SQLEXPRESS;AttachDbFilename=C:\Users\Keltron_Project\Documents\sampl_pi.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;");
 con.Open();
 SqlDataAdapter da = new SqlDataAdapter(str,con);
 DataTable dt = new DataTable();
        
 da.Fill(dt);
 if (dt.Rows.Count > 0)
 {
 byte[] imgdata = new byte[0];
 imgdata = (byte[])dt.Rows[0][0];
  MemoryStream ms = new MemoryStream(imgdata);
 pictureBox1.Image = Image.FromStream(ms);      //error occurs here   "prameter is not valid"
 }
 else
{
 MessageBox.Show("No images in a table");
 }




please help...
thanks in advance
Posted
Updated 8-Apr-18 19:50pm
v4
Comments
george4986 19-Sep-14 3:19am    
specify the buffer length
"byte[] imgdata = new byte[0];" doesn't work
George Jonsson 19-Sep-14 3:29am    
Don't use all capital letters in your text.
it is considered to be SHOUTING, and it is not very polite.
Member 10984793 19-Sep-14 4:12am    
sory sir,,,
George Jonsson 19-Sep-14 4:34am    
Don't be sorry, change it.

FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
byte[] imgbnry = new byte[fs.Length];

fs.Read(imgbnry, 0, imgbnry.Length);
fs.Close();
string str = "insert into tbl_img (id,photo)values('" + textBox3.Text + "',@img)";

string ConStr = @"Server=COMP3\SQLEXPRESS;Database=TestDb;User Id=sa;Password=cos123;";

SqlConnection con = new SqlConnection(ConStr); //@"DataSource=.\SQLEXPRESS;AttachDbFilename=C:\Users\Keltron_Project\Documents\sampl_pi.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;");
con.Open();
SqlCommand cmd = new SqlCommand(str, con);

cmd.Parameters.AddWithValue("@img", imgbnry);
int n = cmd.ExecuteNonQuery();
if (n > 0)
{
MessageBox.Show("Successfully inserted");
}
else
{
MessageBox.Show("Not inserted");
}
 
Share this answer
 
Comments
Member 10984793 20-Sep-14 1:59am    
got it...
thanks
replace code
C#
FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
byte[] imgbnry = new byte[fs.Length];
fs.Read(imgbnry, 0,imgbnry.Length);
 fs.Close();


on insert section with below code

C#
Image image = 
////Image.FromFile(textBox1.Text);
Image.FromFile(@"C:\Users\ku102\Pictures\bitmap\usertile36.bmp");
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Bmp);
byte[] imgbnry = new byte[ms.Length];
ms.Position = 0;
ms.Read(imgbnry, 0, imgbnry.Length);


good luck ;-)
 
Share this answer
 
v3
Comments
Member 10984793 19-Sep-14 4:14am    
i tried above code but not work...
again same error is occur
george4986 19-Sep-14 4:21am    
can u post value of dt.Rows[0][0] here
Member 10984793 19-Sep-14 5:08am    
how to post
george4986 19-Sep-14 5:17am    
copy pic data from database table field and post here as reply.
keltron ? student there?
Member 10984793 19-Sep-14 5:24am    
the database photo column contain data is binary data
what happens if you replace this

C#
byte[] imgdata = new byte[0];
imgdata = (byte[])dt.Rows[0][0];
MemoryStream ms = new MemoryStream(imgdata);


with this

C#
MemoryStream ms = new MemoryStream(byte[])dt.Rows[0][0]);


Other things you could try are to write

C#
(byte[])dt.Rows[0][0]


to a file on disk and see if its actually valid image, AND ONLY ONE image in the stream.

Do you keep the size of the original in a column in the database when its stored ? If so I'd compare that to the MemoryStream ms # of bytes

This construct :-

C#
Image.FromStream()


can also be an 'issue' if the Image class cannot determine the type of the image - png or bmp for example
 
Share this answer
 
Comments
Member 10984793 19-Sep-14 4:37am    
how to write to file
Member 10984793 19-Sep-14 5:50am    
pls help....
C#
private void button1_Click(object sender, EventArgs e)
{
    Image tempImage;
    using (var conn = new SqlConnection("myconnectionstring"))
    {
        conn.Open();
        using (
            var cmd = new SqlCommand("SprocToGetImageBytes", conn) {CommandType = CommandType.StoredProcedure})
        {
            using (var rdr = cmd.ExecuteReader())
            {
                var buffer = (byte[])rdr[0];
                using (var ms = new MemoryStream(buffer))
                {
                    tempImage = Image.FromStream(ms); //theres your image.
                    pictureBox1.Image = tempImage;
                    pictureBox1.Refresh();
                }
            }
        }
    }
}
 
Share this answer
 
Comments
Member 10984793 19-Sep-14 4:38am    
again same problem is occur
Member 10984793 19-Sep-14 5:58am    
anyone helpe me.......
Member 10911231 20-Sep-14 1:51am    
You should use sqltype with the parameter at inserting and retrive time....
Member 10911231 20-Sep-14 1:55am    
typeof(SqlBinary)
Member 10984793 20-Sep-14 2:00am    
got it....
SqlDataAdapter da = new SqlDataAdapter(str,con);


 byte[] picbyte = da["Your image column name"] as byte[] ?? null;
                        if (picbyte != null)
                        {
                            MemoryStream mstream = new MemoryStream(picbyte);
                            pictureBox1.Image = System.Drawing.Image.FromStream(mstream);
                            {
                                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(mstream);
                            }

                        }
 
Share this answer
 
Comments
Maciej Los 9-Apr-18 1:55am    
4 years too late ;(

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