Introduction

In this article we are going to learn how to upload a video in a project folder and save its path in a SQL server database table. We will bind that table to gridview and play audio using html5 video element.

Step 1

Create database in the SQL server of your choice.

  1. CREATE TABLE [dbo].[Videos](
  2. [ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [nvarchar](50) NULL,
  4. [Video_Path] [nvarchar](max) NULL,
  5. CONSTRAINT [PK_Videos] 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 = ON) ON [PRIMARY]
  9. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  10. GO
  11. CREATE procedure [dbo].[spInsertVideo]
  12. (
  13. @Name nvarchar(50),
  14. @Video_Path nvarchar(50)
  15. )
  16. as
  17. begin
  18. insert into Videos(Name,Video_Path)
  19. values(@Name,@Video_Path)
  20. end

Step 2

Create an empty project in Visual Studio. Double click on webconfig file and add connect.

  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 by right clicking on project, adding new item, choosing web form and giving it a name. Add script and style cdn link in web from the head section.

  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 HTML web form, drag and drop file upload control, button control, and grid view control, and use Bootstrap 4 class.

  1. <body>
  2. <form id="form1" runat="server">
  3. <div class="container py-3">
  4. <h2 class="text-center text-uppercase">How to upload video in database and play using asp.net</h2>
  5. <div class="row">
  6. <div class="form-group">
  7. <label>Choose Video:</label>
  8. <div class="input-group">
  9. <div class="custom-file">
  10. <asp:FileUpload ID="FileUpload1" CssClass="custom-file-input" runat="server" />
  11. <label class="custom-file-label"></label>
  12. </div>
  13. <div class="input-group-append">
  14. <asp:Button ID="btnUpload" CssClass="btn btn-outline-secondary" runat="server" Text="Upload" OnClick="btnUpload_Click" />
  15. </div>
  16. </div>
  17. </div>
  18. </div>
  19. <asp:Label ID="lblMessage" runat="server"></asp:Label>
  20. <asp:GridView ID="GridView1" ShowHeaderWhenEmpty="true" HeaderStyle-CssClass="bg-primary text-white" runat="server" AutoGenerateColumns="false" CssClass="table table-bordered">
  21. <EmptyDataTemplate>
  22. <div class="text-center">No Data Found <strong>Upload New Video</strong></div>
  23. </EmptyDataTemplate>
  24. <Columns>
  25. <asp:BoundField HeaderText="ID" DataField="ID" />
  26. <asp:BoundField HeaderText="Name" DataField="Name" />
  27. <asp:TemplateField HeaderText="Videos">
  28. <ItemTemplate>
  29. <video width="130" height="130" controls>
  30. <source src='<%#Eval("Video_Path")%>' type="video/mp4">
  31. </video>
  32. </ItemTemplate>
  33. </asp:TemplateField>
  34. </Columns>
  35. </asp:GridView>
  36. </div>
  37. </form>
  38. </body>
  1. <script type="text/javascript">
  2. $(document).ready(function () {
  3. $("#GridView1").prepend($("<thead></thead>").append($(this).find("tr:first"))).dataTable();
  4. });
  5. </script>

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

Step 6

Run project ctr+F5

Final output

Screenshot 1

ASP.NETScreenshot 2

ASP.NETScreenshot 3

ASP.NET