Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
i am trying to Upload an image into sqlserver2008 using Winforms(C#)
 
// sourcecode

string strFileFullPath="this is my image location";
 
in Database i took Varbinary(max) as DataType
 
FileStream stream = new FileStream(strFileFullPath, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
 
byte[] photo = reader.ReadBytes((int)stream.Length);
// sourcecode
Error : Failed to convert parameter value from a String to a Byte[].
can you please help me how can i solve this issue
 
Thanks in advance
Posted 22-Apr-13 21:10pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello try this code. If you find any easy solution than of mine then please reply me. Thanks...
 
private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
            if (open.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = open.FileName;
            }
            cn.Open();
            string image = textBox1.Text;
            Bitmap bmp = new Bitmap(image);
            FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
            byte[] bimage = new byte[fs.Length];
            fs.Read(bimage, 0, Convert.ToInt32(fs.Length));
            fs.Close();
            SqlCommand cmd = new SqlCommand("insert into ImageUpload(imgdata) values(@imgdata)",cn);
            cmd.Parameters.AddWithValue("@imgdata",SqlDbType.Image).Value=bimage;
            cmd.ExecuteNonQuery();
            cn.Close();
        }
  Permalink  
v2
Comments
leaderbalaji at 23-Apr-13 3:37am
   
it's fine but i am getting error like
Index was outside the bounds of the array.
i am trying to save 4kb image into varbinary(max) in sqlserver2008 is it problem? is it required to compress? if yes then how can i?
Chiklu.Soumya at 23-Apr-13 3:45am
   
Hello... I have used this code with Image datatype in Sql server 2008,2012. Varbinary was working only in Sql 2005. Don't think it's problem using varbinary. Now i"ll try and will respond u if i succeed.
Chiklu.Soumya at 23-Apr-13 3:48am
   
Yeah...It's working. In my code change SqlDbType.Image to SqlDbType.VarBinary. It's working. By the way Thanks for your question.
leaderbalaji at 23-Apr-13 4:14am
   
Thank you soo may be in passing parameters it's a problem i think
 
once again thank you dude......
Chiklu.Soumya at 23-Apr-13 4:20am
   
Welcome..........

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
Web02 | 2.8.1411019.1 | Last Updated 23 Apr 2013
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