ASp.net

Saturday, September 12, 2015

How Image Upload and save in C# Part 2

1 comment:

  1. private void button2_Click(object sender, EventArgs e)
    {
    try
    {
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=I:\C3 soft\WindowsFormsApplication2\WindowsFormsApplication2\MTechSqlDatabase.mdf;Integrated Security=True;Connect Timeout=30");
    con.Open();
    string cb = string.Format("Insert into Customer(CustomerName, CustomerPicture) Values ('{0}',@image)", textBox1.Text);
    SqlCommand cmd = new SqlCommand(cb, con);

    MemoryStream ms = new MemoryStream(); //for image
    Bitmap bmpImage = new Bitmap(pictureBox1.Image); //for image
    bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //for image
    byte[] data = ms.GetBuffer(); //for image
    SqlParameter p = new SqlParameter("@image", SqlDbType.VarBinary) { Value = data }; //for image
    cmd.Parameters.Add(p); //for image


    cmd.ExecuteReader();
    con.Close();
    MessageBox.Show("Successfully saved");
    Getdata();

    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }

    ReplyDelete

MM Online Express | Best product Review and Online Marketing site

MM Online Express | Best product Review and Online Marketing site