Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
int i=0;

            byte[] img_bt=null;
            FileStream fs=new FileStream(this.textBox4.Text,FileMode.Open,FileAccess.Read);
            BinaryReader br=new BinaryReader(fs);
            img_bt=br.ReadBytes((int)fs.Length);


            SqlCommand cmd = new SqlCommand("insert into prac_imag values(@sno,@name,@city,@image", con);
            
            cmd.Parameters.AddWithValue("@sno", Convert.ToInt32(textBox1.Text));
            cmd.Parameters.AddWithValue("@name", textBox3.Text);
            cmd.Parameters.AddWithValue("@city", textBox2.Text);
            cmd.Parameters.AddWithValue("@image",img_bt);
            con.Open();
            i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                MessageBox.Show("Data has been saved");

            }
            else
            {
                MessageBox.Show("Data has not been saved");
            }

         con.Close();
Posted
Updated 28-May-14 3:27am
v2
Comments
PIEBALDconsult 28-May-14 9:52am    
That looks OK. What is the result?

Specify the type
SQL
cmd.Parameters.AddWithValue("@image", img_bt, SqlDbType.VarBinary)
 
Share this answer
 
for image do as below
C#
SqlParameter image = new SqlParameter("@image", SqlDbType.VarBinary, img_bt.Length);
image.Value = img_bt;
cmd.Parameters.Add(image);
 
Share this answer
 
v2
Comments
JITHU.P 29-May-14 11:14am    
still facing a sql expection in cmd.executenonquery saying (some string or binary truncated)
what this exception means can't understand

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