How To Upload Audio In Database And Play In GridView Using ASP.NET

In this article, we are going to discuss how to upload an audio file in our project folder and save its path in SQL server database table.

Introduction

In this article, we are going to discuss how to upload an audio file in our project folder and save its path in SQL server database table. We will dynamically bind SQL table with the gridview control and display all uploaded audio files. In the gridview control, we have HTML5 audio element with controls play, volume and download options. We will have jQuery data table applied in gridview control for search, sort and paging.

Step 1

Create a database in the SQL server of your choice.

  1. CREATE TABLE [dbo].[AudioList](  
  2.     [ID] [int] IDENTITY(1,1) NOT NULL,  
  3.     [Name] [nvarchar](50) NULL,  
  4.     [Audio_Path] [nvarchar](maxNULL,  
  5.  CONSTRAINT [PK_AudioList] PRIMARY KEY CLUSTERED   
  6. (  
  7.     [ID] ASC  
  8. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  9. ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  
  10.   
  11. GO  
  12.   
  13. CREATE procedure [dbo].[spInsertAudio]  
  14. (  
  15. @Name nvarchar(50),  
  16. @Audio_Path nvarchar(50)  
  17. )  
  18. as  
  19. begin  
  20. insert into AudioList(Name,Audio_Path)  
  21. values(@Name,@Audio_Path)  
  22. end  
  23.   
  24. CREATE procedure [dbo].[spGetAllAudio]  
  25. as  
  26. begin  
  27. select ID,Name,Audio_Path from AudioList  
  28. end   

Step 2

Create an empty web project in the visual studio of your choice. Give a meaningful name. Double click on web config file and add a database connection.

  1. <connectionStrings>  
  2.   <add name="DBCS" connectionString="data source=FARHAN\SQLEXPRESS; database=simpleDB; integrated security=true;"/>  
  3. </connectionStrings>  

Add the below line of code to allow up to 50MB file upload:

  1. <httpRuntime executionTimeout="3600" maxRequestLength="51200" enable="true"/>  
  2. <system.webServer>  
  3.     <security>  
  4.       <requestFiltering>  
  5.         <requestLimits maxAllowedContentLength="104857600" />  
  6.       </requestFiltering>  
  7.     </security>  
  8.   </system.webServer>  

Step 3

Add web form, right click on project, add new item, choose web form, give name. Add script and style cdn link in head section of web form.

  1. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">  
  2. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">  
  3. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  4. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>  
  5. <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap4.min.css" />  
  6. <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js" type="text/javascript"></script>  
  7. <script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js" type="text/javascript"></script>  

Write script to apply jQuery data table with gridview:

  1. <script type="text/javascript">  
  2.     $(document).ready(function () {  
  3.         $("#GridView1").prepend($("<thead></thead>").append($(this).find("tr:first"))).dataTable();  
  4.     });  
  5. </script>  

Step 4

Design web form by dragging and dropping textbox control, button control, gridview control, control and label control. Apply Bootstrap 4 class for respective control. Right click on the project and create a folder with name UploadAudio to upload audio files.

  1. <body>  
  2.     <form id="form1" runat="server">  
  3.         <div class="container py-3">  
  4.             <h2 class="text-center text-uppercase">How to upload audio in database and play using asp.net</h2>  
  5.             <div class="card" style="margin-bottom: 10px;">  
  6.                 <div class="card-header bg-primary text-white">  
  7.                     <h5>Upload Audio</h5>  
  8.                 </div>  
  9.                 <div class="card-body">  
  10.                     <button type="button" class="btn btn-primary btn-sm rounded-0" data-toggle="modal" data-target="#myAudio">  
  11.                         <i class="fa fa-plus-circle"></i>Add New  
  12.                     </button>  
  13.                     <div class="modal fade" id="myAudio">  
  14.                         <div class="modal-dialog">  
  15.                             <div class="modal-content">  
  16.                                 <div class="modal-header">  
  17.                                     <h4 class="modal-title">Add New Audio</h4>  
  18.                                     <button type="button" class="close" data-dismiss="modal">×</button>  
  19.                                 </div>  
  20.                                 <div class="modal-body">  
  21.                                     <div class="row">  
  22.                                         <div class="col-md-12">  
  23.                                             <div class="form-group">  
  24.                                                 <label>Choose Audio:</label>  
  25.                                                 <div class="input-group">  
  26.                                                     <div class="custom-file">  
  27.                                                         <asp:FileUpload ID="FileUpload1" CssClass="custom-file-input" runat="server" />  
  28.                                                         <label class="custom-file-label"></label>  
  29.                                                     </div>  
  30.                                                     <div class="input-group-append">  
  31.                                                         <asp:Button ID="btnUpload" CssClass="btn btn-outline-secondary" runat="server" Text="Upload" OnClick="btnUpload_Click" />  
  32.                                                     </div>  
  33.                                                 </div>  
  34.                                                 <asp:Label ID="lblMessage" runat="server"></asp:Label>  
  35.                                             </div>  
  36.                                         </div>  
  37.                                     </div>  
  38.                                 </div>  
  39.                                 <div class="modal-footer">  
  40.                                     <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>  
  41.                                 </div>  
  42.                             </div>  
  43.                         </div>  
  44.                     </div>  
  45.                 </div>  
  46.                 <asp:GridView ID="GridView1" ShowHeaderWhenEmpty="true" HeaderStyle-CssClass="bg-primary text-white" runat="server" AutoGenerateColumns="false" CssClass="table table-bordered">  
  47.                     <EmptyDataTemplate>  
  48.                         <div class="text-center">No Data Found <strong>Upload New Audio</strong></div>  
  49.                     </EmptyDataTemplate>  
  50.                     <Columns>  
  51.                         <asp:BoundField HeaderText="ID" DataField="ID" />  
  52.                         <asp:BoundField HeaderText="Name" DataField="Name" />  
  53.                         <asp:BoundField HeaderText="Audio" DataField="Audio_Path" />  
  54.                         <asp:TemplateField HeaderText="Play">  
  55.                             <ItemTemplate>  
  56.                                 <audio controls>  
  57.                                     <source src='<%#Eval("Audio_Path")%>' type="audio/ogg">  
  58.                                 </audio>  
  59.                             </ItemTemplate>  
  60.                         </asp:TemplateField>  
  61.                     </Columns>  
  62.                 </asp:GridView>  
  63.             </div>  
  64.         </div>  
  65.     </form>  
  66. </body>  

COMPLETE HTML CODE FOR WEB FORM

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4. <head runat="server">  
  5.     <title>Upload Audio</title>  
  6.     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">  
  7.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">  
  8.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  9.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>  
  10.     <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap4.min.css" />  
  11.     <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js" type="text/javascript"></script>  
  12.     <script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js" type="text/javascript"></script>  
  13.     <script type="text/javascript">  
  14.         $(document).ready(function () {  
  15.             $("#GridView1").prepend($("<thead></thead>").append($(this).find("tr:first"))).dataTable();  
  16.         });  
  17.     </script>  
  18. </head>  
  19. <body>  
  20.     <form id="form1" runat="server">  
  21.         <div class="container py-3">  
  22.             <h2 class="text-center text-uppercase">How to upload audio in database and play using asp.net</h2>  
  23.             <div class="card" style="margin-bottom: 10px;">  
  24.                 <div class="card-header bg-primary text-white">  
  25.                     <h5>Upload Audio</h5>  
  26.                 </div>  
  27.                 <div class="card-body">  
  28.                     <button type="button" class="btn btn-primary btn-sm rounded-0" data-toggle="modal" data-target="#myAudio">  
  29.                         <i class="fa fa-plus-circle"></i>Add New  
  30.                     </button>  
  31.                     <div class="modal fade" id="myAudio">  
  32.                         <div class="modal-dialog">  
  33.                             <div class="modal-content">  
  34.                                 <div class="modal-header">  
  35.                                     <h4 class="modal-title">Add New Audio</h4>  
  36.                                     <button type="button" class="close" data-dismiss="modal">×</button>  
  37.                                 </div>  
  38.                                 <div class="modal-body">  
  39.                                     <div class="row">  
  40.                                         <div class="col-md-12">  
  41.                                             <div class="form-group">  
  42.                                                 <label>Choose Audio:</label>  
  43.                                                 <div class="input-group">  
  44.                                                     <div class="custom-file">  
  45.                                                         <asp:FileUpload ID="FileUpload1" CssClass="custom-file-input" runat="server" />  
  46.                                                         <label class="custom-file-label"></label>  
  47.                                                     </div>  
  48.                                                     <div class="input-group-append">  
  49.                                                         <asp:Button ID="btnUpload" CssClass="btn btn-outline-secondary" runat="server" Text="Upload" OnClick="btnUpload_Click" />  
  50.                                                     </div>  
  51.                                                 </div>  
  52.                                                 <asp:Label ID="lblMessage" runat="server"></asp:Label>  
  53.                                             </div>  
  54.                                         </div>  
  55.                                     </div>  
  56.                                 </div>  
  57.                                 <div class="modal-footer">  
  58.                                     <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>  
  59.                                 </div>  
  60.                             </div>  
  61.                         </div>  
  62.                     </div>  
  63.                 </div>  
  64.                 <asp:GridView ID="GridView1" ShowHeaderWhenEmpty="true" HeaderStyle-CssClass="bg-primary text-white" runat="server" AutoGenerateColumns="false" CssClass="table table-bordered">  
  65.                     <EmptyDataTemplate>  
  66.                         <div class="text-center">No Data Found <strong>Upload New Audio</strong></div>  
  67.                     </EmptyDataTemplate>  
  68.                     <Columns>  
  69.                         <asp:BoundField HeaderText="ID" DataField="ID" />  
  70.                         <asp:BoundField HeaderText="Name" DataField="Name" />  
  71.                         <asp:BoundField HeaderText="Audio" DataField="Audio_Path" />  
  72.                         <asp:TemplateField HeaderText="Play">  
  73.                             <ItemTemplate>  
  74.                                 <audio controls>  
  75.                                     <source src='<%#Eval("Audio_Path")%>' type="audio/ogg">  
  76.                                 </audio>  
  77.                             </ItemTemplate>  
  78.                         </asp:TemplateField>  
  79.                     </Columns>  
  80.                 </asp:GridView>  
  81.             </div>  
  82.         </div>  
  83.     </form>  
  84. </body>  
  85. </html>  

Step 5

Double click on upload button and write the following code.

  1. using System;  
  2. using System.IO;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Configuration;  
  6.   
  7. namespace UploadAudio_Demo  
  8. {  
  9.     public partial class UploadAudioFile : System.Web.UI.Page  
  10.     {  
  11.         protected void Page_Load(object sender, EventArgs e)  
  12.         {  
  13.             if (!IsPostBack)  
  14.             {  
  15.                 BindGrid();  
  16.             }  
  17.         }  
  18.         private void BindGrid()  
  19.         {  
  20.             string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
  21.             using (SqlConnection con = new SqlConnection(CS))  
  22.             {  
  23.                 SqlCommand cmd = new SqlCommand("spGetAllAudio", con);  
  24.                 cmd.CommandType = CommandType.StoredProcedure;  
  25.                 con.Open();  
  26.                 GridView1.DataSource = cmd.ExecuteReader();  
  27.                 GridView1.DataBind();  
  28.             }  
  29.         }  
  30.         protected void btnUpload_Click(object sender, EventArgs e)  
  31.         {  
  32.             if (FileUpload1.PostedFile != null)  
  33.             {  
  34.                 try  
  35.                 {  
  36.                     string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);  
  37.                     FileUpload1.PostedFile.SaveAs(Server.MapPath("UploadAudio/" + FileName));  
  38.   
  39.                     string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
  40.                     using (SqlConnection con = new SqlConnection(CS))  
  41.                     {  
  42.                         SqlCommand cmd = new SqlCommand("spInsertAudio", con);  
  43.                         cmd.CommandType = CommandType.StoredProcedure;  
  44.                         con.Open();  
  45.                         cmd.Parameters.AddWithValue("@Name", FileName);  
  46.                         cmd.Parameters.AddWithValue("@Audio_Path""UploadAudio/" + FileName);  
  47.                         cmd.ExecuteNonQuery();  
  48.                         BindGrid();  
  49.                         lblMessage.Text = "Your file uploaded successfully";  
  50.                         lblMessage.ForeColor = System.Drawing.Color.Green;  
  51.                     }  
  52.                 }  
  53.                 catch (Exception)  
  54.                 {  
  55.                     lblMessage.Text = "Your file not uploaded";  
  56.                     lblMessage.ForeColor = System.Drawing.Color.Red;  
  57.                 }  
  58.             }  
  59.         }  
  60.     }  
  61. }  

Step 6

Run project ctr+F5

Final output:

Screenshot-1

ASP.NET

 

Screenshot-2

ASP.NET

Screenshot-3

ASP.NET

Screenshot-4

ASP.NET

 

Summary

In this article, we have learned how to upload an audio file in the folder and save path in SQL database. We have displayed all uploaded files in ASP.net gridview control. We have also applied jQuery datatable functionality in gridview like search, sort and paging. We have used the HMTL5 audio element to play the audio file.