I am getting the error while inserting the image from win-form.Can anyone please tell me the in the code.I am using Visual Studio 2013 and MS Sql 2008 R2.
Thanks .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        SqlConnection cn = new SqlConnection("Data Source=(local);Initial Catalog=maria;Persist Security Info=True;User ID=sa;Password=123456");
        SqlCommand command;
        string imgLoc = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*";
                dlg.Title = "Select Employee Picture";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    imgLoc = dlg.FileName.ToString();
                    pictureBox1.ImageLocation = imgLoc;

                }

            }
            catch (Exception)
            {
                MessageBox.Show("Please enter the valid image.");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] img = null;
                FileStream fs = new FileStream(imgLoc, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                img = br.ReadBytes((int)fs.Length);

                string sql = "INSERT INTO STU(EID,FIRST_NAME,LAST_NAME,IMAGE)VALUES(" + textBox1.Text + "," + textBox2.Text + "," + textBox3.Text + ",@img)";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                command = new SqlCommand(sql, cn);
                command.Parameters.Add(new SqlParameter("@img", img));
                command.ExecuteNonQuery();
                cn.Close();
                MessageBox.Show(ToString() + "Record(s) Saved.");

            }
            catch (Exception)
            {
                cn.Close();
                MessageBox.Show("Insertion Failed");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "SELECT FIRST_NAME,LAST_NAME,IMAGE FROM STU WHERE Id=" + textBox1.Text + "";
                cn.Open();
                command = new SqlCommand(sql, cn);
                SqlDataReader reader = command.ExecuteReader();
                reader.Read();
                if (reader.HasRows)
                {
                    textBox2.Text = reader[0].ToString();
                    textBox3.Text = reader[1].ToString();
                    byte[] img = (byte[])(reader[2]);
                    if (img == null)
                        pictureBox1.Image = null;
                    else
                    {
                        MemoryStream ms=new MemoryStream(img);
                        pictureBox1.Image=Image.FromStream(ms);
                    }
                }
                else
                {
                    MessageBox.Show("Id Doesnot exists");
                }
                cn.Close();
            }
            catch(Exception)
            {
                cn.Close();
                MessageBox.Show("Id Does not Exists.");
            }
        }
    }
}

What is the error you are getting?

Image is not stored in database and showing the message insertion failed

string sql = "INSERT INTO STU(EID,FIRST_NAME,LAST_NAME,IMAGE)VALUES(" + textBox1.Text + "," + textBox2.Text + "," + textBox3.Text + ",@img)";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                command = new SqlCommand(sql, cn);
                command.Parameters.Add(new SqlParameter("@img", img));

On line 5 remove the monkey sign(@), on line 2 (line one but second row) look where you comma is ",@img", remove the comma concatenate it separetly, try then and tell if errors appear. Try not to use the concatention it's messy and not a good practice since it's vulnerable once your database is ready to use...

This thing still doesn't work for me my connection string is okk. The problem is when i save image & text it show insertion failed but when i manually entered in database query it saved when i search for id it display the message id doesn't exists but my details are displayed but image is not displayed... :(

Hmmm... I really don t know i m looking at the code and everything seems ok but maybe i missed something, anyways since your code is not long press f11 and go through every line and see what happens that is the only thing that i can say. Provide more information in your next post if possible and tomorrow if you don't solve the problem i ll rewrite your code a bit a post it here...

I changed my connection String and modify the code and changed column name as Picture from Imagein database.
Change code is:

string sql = "INSERT INTO STU(EID,FIRST_NAME,LAST_NAME,IMAGE)VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "',@img)";

string sql = "SELECT FIRST_NAME,LAST_NAME,IMAGE FROM STU WHERE Id='" + textBox1.Text + "'";

And it work for me thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.