Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
private void Upload_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            //img filter
            open.Filter = "ImageFiles(*.jpg; *.jpeg; *.gif; *.bmp;)|*.jpg; *.jpeg; *.gif; *.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                String photoFilePath = open.FileName.ToString();
                pic.ImageLocation = photoFilePath;
                textbox.Text = photoFilePath;
            }
        }
        private void Save_Click(object sender, EventArgs e)
        {

         try
         {
             
             byte[] imageBt = null;
             FileStream fs = new FileStream(this.textbox.Text, FileMode.Open, FileAccess.Read);
             BinaryReader br = new BinaryReader(fs);
             imageBt = br.ReadBytes((int)fs.Length);
             
                String connString = ConfigurationManager.ConnectionStrings["logindb"].ConnectionString;
                SqlConnection conn = new SqlConnection(connString);
                SqlCommand cmd = new SqlCommand("storeimg", conn);
                //parameters
                cmd.Parameters.Add("@Photo", SqlDbType.Image).Value = pic.Image;
                cmd.Parameters.Add("@ID", SqlDbType.Int).Value = IDtex.Text;
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Added");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }

        }
Posted
Updated 4-May-15 2:14am
v2
Comments
Sergey Alexandrovich Kryukov 4-May-15 11:06am    
Why?
—SA

 
Share this answer
 
Why using pic.image?
cmd.Parameters.Add("@Photo", SqlDbType.Image).Value = pic.Image;

Use the byte array imageBt instead.


Moreover Image data type will be deprecated, why not using varbynary?
 
Share this answer
 
Comments
Lions Heart 4-May-15 12:39pm    
Thanks have done :)

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