1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
   |  protected void BoutonAjouterfichier_Click(object sender, ImageClickEventArgs e)
        {
            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
            {
                if (FileUpload1.HasFile)
                {
                    if (FileUpload1.PostedFile.ContentLength < 17179869)
                    {
                        try
                        {
                            connection.Open();
                            using (var command = connection.CreateCommand())
                            {
                                command.CommandText = "INSERT INTO TableImage(Image,Chemin,Taille,DateCreation) values (@file_bytes,@Chemin,@Taille,@DateCreation)";
                                command.Parameters.Add("file_bytes", SqlDbType.Image).Value = FileUpload1.FileBytes;
                                command.Parameters.Add("Chemin", SqlDbType.VarChar, 50).Value = FileUpload1.FileName;
                                command.Parameters.Add("Taille", SqlDbType.VarChar, 50).Value = FileUpload1.PostedFile.ContentLength +" "+ "kb";
                                command.Parameters.Add("DateCreation", SqlDbType.Date).Value = DateTime.Now.ToShortDateString();
 
 
                                command.ExecuteNonQuery();
                            }
 
                        }
                        catch (Exception err)
                        {
                            Label1.Text = "ERREUR: " + err.Message.ToString();
                        }
 
                        finally
                        {
                            try
                            {
                                //Mise à jour du tableau
                                GridViewListeFichier.DataBind();
 
                                //Fermeture de la connexion
                                connection.Close();
                            }
                            catch (Exception err2)
                            {
                                Trace.Write(err2.Message);
                            }
                        }
                    }
                    else
                    {
                        Label1.Text = "Fichier trop volumineux! ";
                    }
                }
                else
                {
                    Label1.Text = "Vous n'avez pas spécifié de fichier! ";
                }
 
            }
        } | 
Partager