0
namespace dota2
{
  public partial class Form1 : Form
  {
    private SqlConnection cn = new SqlConnection();
    private SqlCommand cmd = new SqlCommand();
    private SqlDataReader dr;
    private SqlParameter picture;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection(global::dota2.Properties.Settings.Default.Database1ConnectionString);
        cmd.Connection = cn;
        picture = new SqlParameter("@picture", SqlDbType.Image);

    }

    private void button1_Click(object sender, EventArgs e)
    {
        open();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        savepicture();

    }
    private void savepicture()
    {

        if (pictureBox1.Image != null)
        {
            MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
            byte[] a = ms.GetBuffer();
            ms.Close();
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@picture", a);
            cmd.CommandText = "insert into pictures (name,picture) values ('" + textBox1.Text.ToString() + "',@picture)";
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            textBox1.Text = "";
            pictureBox1.Image = null;
            MessageBox.Show("Image Saved", "Programming At Kstark");
        }
    }
    private void open()
    {
        try
        {
            OpenFileDialog f = new OpenFileDialog();
            f.InitialDirectory = "C:/Picture/";
            f.Filter = "All Files|*.*|JPEGs|*.jpg|Bitmaps|*.bmp|GIFs|*.gif";
            f.FilterIndex = 0;
            if (f.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(f.FileName);
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.BorderStyle = BorderStyle.Fixed3D;
                textBox1.Text = f.SafeFileName.ToString();
            }
        }
        catch { }
    }
  }
}

My form has a picturebox and Open and Save buttons. I'm trying to insert the picture from my picturebox into database using the code below. Picture opens as it should and shows up in the picturebox, but pressing the save button isn't working. It says either cn. Open is an error or the executenonquery.

Jawa
  • 2,336
  • 6
  • 34
  • 39
noob
  • 1
  • 1
  • Please post the exact error message, also please note you're using classes that implement `IDisposable`, that means you should prefer to put those items in a `using` statement. (eg: `using (var ms = new MemoryStream){ // do stuff with ms; } – Destrictor Apr 04 '15 at 15:47
  • possible duplicate of [C# Resize jpg image, convert to byte and save into database using varbinary](http://stackoverflow.com/questions/17333488/c-sharp-resize-jpg-image-convert-to-byte-and-save-into-database-using-varbinary) – Ňɏssa Pøngjǣrdenlarp Apr 04 '15 at 15:48
  • The error is cmd.ExecuteNonQuery(); – noob Apr 04 '15 at 15:49
  • That is not the exception, that is probably where it throws.... – rene Apr 04 '15 at 16:24
  • 1
    Can you post the exception message? What you gave is the line that the exception happens on. – Moby Disk Apr 07 '15 at 18:49
  • How exactly does the duplicate link posted **days ago** not help? It shows precisely how to save to a DB **plus** how to optionally encode and resize the image! it is a great answer. – Ňɏssa Pøngjǣrdenlarp Apr 07 '15 at 18:55

0 Answers0