Upload multiple files and save their path to database using Fileupload control in asp.net | ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial

Upload multiple files and save their path to database using Fileupload control in asp.net

Introduction: In this article I will explain how to Upload multiple files and save their path to database using Fileupload control in asp.net 4.5

Description:


Html Markup Of page:
<table>
            <tr><td>Upload Image:</td><td> <asp:fileupload ID="Fileupload1" runat="server" AllowMultiple="true"></asp:fileupload></td></tr>
            <tr><td></td><td><asp:Button ID="Button1" runat="server" Text="Upload File" OnClick="Button1_Click" /></td></tr>
        </table>

C# Code:
using System.IO;
using System.Data.SqlClient;
using System.Configuration;

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (Fileupload1.HasFiles)
            {
                foreach (HttpPostedFile postedfile in Fileupload1.PostedFiles)
                {
                    string fileName = Path.GetFileName(postedfile.FileName);
                    postedfile.SaveAs(Server.MapPath("~/ImageUploads/") + fileName);
                    string filepath = "~/ImageUploads/" + fileName;
                    string query = "insert into File_Uploads values(@FileName,@Filepath)";
                    SqlCommand cmd = new SqlCommand(query, con);
                    con.Open();
                    cmd.Parameters.AddWithValue("@FileName", fileName);
                    cmd.Parameters.AddWithValue("@Filepath", filepath);
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                Messagebox(string.Format("{0} files have been uploaded successfully.", Fileupload1.PostedFiles.Count));
            }
        }
        catch (Exception ex)
        {
        }     
    }
    private void Messagebox(string Message)
    {
        Label lblMessageBox = new Label();
        lblMessageBox.Text =
            "<script language='javascript'>" + Environment.NewLine +
            "window.alert('" + Message + "')</script>";
        Page.Controls.Add(lblMessageBox);
    }

VB Code:
Imports System.IO
Imports System.Data.SqlClient
Imports System.Configuration

Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ToString())

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            If Fileupload1.HasFiles Then
                For Each postedfile As HttpPostedFile In Fileupload1.PostedFiles
                    Dim fileName As String = Path.GetFileName(postedfile.FileName)
                    postedfile.SaveAs(Server.MapPath("~/ImageUploads/") & fileName)
                    Dim filepath As String = Convert.ToString("~/ImageUploads/") & fileName
                    Dim query As String = "insert into File_Uploads values(@FileName,@Filepath)"
                    Dim cmd As New SqlCommand(query, con)
                    con.Open()
                    cmd.Parameters.AddWithValue("@FileName", fileName)
                    cmd.Parameters.AddWithValue("@Filepath", filepath)
                    cmd.ExecuteNonQuery()
                    con.Close()
                Next
              Messagebox(String.Format("{0} files have been uploaded successfully.", Fileupload1.PostedFiles.Count))
            End If
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Messagebox(ByVal Message As String)
        Dim lblMessageBox As New Label()
        lblMessageBox.Text = "<script language='javascript'>" + Environment.NewLine & "window.alert('" & Message & "')</script>"
        Page.Controls.Add(lblMessageBox)

    End Sub

 Is this article helpful for you?

If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

Post a Comment

Its Good Understanding.
but the database connection its not properly understand.

i have set the connection in web.config




and create object of connection :
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget