Click here to Skip to main content
13,097,884 members (56,897 online)
Rate this:
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 20:10pm

1 solution

Rate this: bad
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;
            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));
            SqlCommand cmd = new SqlCommand("insert into ImageUpload(imgdata) values(@imgdata)",cn);
leaderbalaji 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 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 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 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 23-Apr-13 4:20am

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

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2017
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