In this article I will explain with an example, how to upload and save MP4 Video files in SQL Server Database and then playing them with Live Streaming in ASP.Net DataList.
 
 
Database
This article makes use of a table named tblFiles whose schema is defined as follows.
Upload Save Retrieve and Play MP4 Video files with live streaming from Database in ASP.Net using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
HTML Markup
The HTML Markup contains a FileUpload and Button to upload and save the MP4 Video files to database and an ASP.Net DataList control to display the uploaded video files and also allows the user to play the MP4 Video file.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
<hr />
<asp:DataList ID="DataList1" Visible="true" runat="server" AutoGenerateColumns="false"
    RepeatColumns="2" CellSpacing="5">
    <ItemTemplate>
        <u>
            <%# Eval("Name") %></u>
        <hr />
        <a class="player" style="height: 300px; width: 300px; display: block" href='<%# Eval("Id", "FileCS.ashx?Id={0}") %>'>
        </a>
    </ItemTemplate>
</asp:DataList>
<script src="FlowPlayer/flowplayer-3.2.12.min.js" type="text/javascript"></script>
<script type="text/javascript">
    flowplayer("a.player", "FlowPlayer/flowplayer-3.2.16.swf", {
        plugins: {
            pseudo: { url: "FlowPlayer/flowplayer.pseudostreaming-3.2.12.swf" }
        },
        clip: { provider: 'pseudo', autoPlay: false},
    });
</script>
 
Note: I making use of FlowPlayer (Flash Video Player) to play the videos online on the web page. You can also make use of other players, you just need to set the source as File.ashx for the player.
 
Note: The File.ashx is a Generic Handler (explained later) which is used to fetch the uploaded MP4 Video files from the SQL Server database for the purpose of Playing.
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
 
VB.Net
Imports System.IO
Imports System.Data.SqlClient
Imports System.Configuration
 
 
Uploading the MP4 Video files and saving in SQL Server Database
The below event handler gets executed when the Upload Button is clicked, it simply saves the video file as Binary data in the SQL Server Database.
C#
protected void btnUpload_Click(object sender, EventArgs e)
{
    using (BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream))
    {
        byte[] bytes = br.ReadBytes((int)FileUpload1.PostedFile.InputStream.Length);
        string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)";
                cmd.Parameters.AddWithValue("@Name", Path.GetFileName(FileUpload1.PostedFile.FileName));
                cmd.Parameters.AddWithValue("@ContentType", "video/mp4");
                cmd.Parameters.AddWithValue("@Data", bytes);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
    Response.Redirect(Request.Url.AbsoluteUri);
}
 
VB.Net
Protected Sub btnUpload_Click(sender As Object, e As EventArgs)
    Using br As New BinaryReader(FileUpload1.PostedFile.InputStream)
        Dim bytes As Byte() = br.ReadBytes(CInt(FileUpload1.PostedFile.InputStream.Length))
        Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New SqlConnection(strConnString)
            Using cmd As New SqlCommand()
                cmd.CommandText = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)"
                cmd.Parameters.AddWithValue("@Name", Path.GetFileName(FileUpload1.PostedFile.FileName))
                cmd.Parameters.AddWithValue("@ContentType", "video/mp4")
                cmd.Parameters.AddWithValue("@Data", bytes)
                cmd.Connection = con
                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()
            End Using
        End Using
    End Using
    Response.Redirect(Request.Url.AbsoluteUri)
End Sub
 
 
Displaying the uploaded MP4 Video files in ASP.Net DataList
Inside the Page Load event, the DataList is populated with uploaded videos from Table.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGrid();
    }
}
 
private void BindGrid()
{
    string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select Id, Name from tblFiles";
            cmd.Connection = con;
            con.Open();
            DataList1.DataSource = cmd.ExecuteReader();
            DataList1.DataBind();
            con.Close();
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        BindGrid()
    End If
End Sub
 
Private Sub BindGrid()
    Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(strConnString)
        Using cmd As New SqlCommand()
            cmd.CommandText = "select Id, Name from tblFiles"
            cmd.Connection = con
            con.Open()
            DataList1.DataSource = cmd.ExecuteReader()
            DataList1.DataBind()
            con.Close()
        End Using
    End Using
End Sub
 
 
Live Streaming MP4 Video files from Database in ASP.Net DataList
I have made use of a Generic Handler which will fetch the uploaded MP4 video files based on Id from the database and will act as a live stream source for the Flash Video Player.
C#
<%@ WebHandler Language="C#" Class="FileCS" %>
 
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
public class FileCS : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        int id = int.Parse(context.Request.QueryString["id"]);
        byte[] bytes;
        string contentType;
        string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        string name;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select Name, Data, ContentType from tblFiles where Id=@Id";
                cmd.Parameters.AddWithValue("@Id", id);
                cmd.Connection = con;
                con.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                bytes = (byte[])sdr["Data"];
                contentType = sdr["ContentType"].ToString();
                name = sdr["Name"].ToString();
                con.Close();
            }
        }
        context.Response.Clear();
        context.Response.Buffer = true;
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name);
        context.Response.ContentType = contentType;
        context.Response.BinaryWrite(bytes);
        context.Response.End();
    }
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
 
VB.Net
<%@ WebHandler Language="VB" Class="FileVB" %>
 
Imports System
Imports System.Web
Imports System.Data.SqlClient
Imports System.Configuration
 
Public Class FileVB : Implements IHttpHandler
   
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim id As Integer = Integer.Parse(context.Request.QueryString("id"))
        Dim bytes As Byte()
        Dim contentType As String
        Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim name As String
        Using con As New SqlConnection(strConnString)
            Using cmd As New SqlCommand()
                cmd.CommandText = "select Name, Data, ContentType from tblFiles where Id=@Id"
                cmd.Parameters.AddWithValue("@Id", id)
                cmd.Connection = con
                con.Open()
                Dim sdr As SqlDataReader = cmd.ExecuteReader()
                sdr.Read()
                bytes = DirectCast(sdr("Data"), Byte())
                contentType = sdr("ContentType").ToString()
                name = sdr("Name").ToString()
                con.Close()
            End Using
        End Using
        context.Response.Clear()
        context.Response.Buffer = True
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name)
        context.Response.ContentType = contentType
        context.Response.BinaryWrite(bytes)
        context.Response.[End]()
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
End Class
 
 
Screenshots
Upload Save Retrieve and Play MP4 Video files with live streaming from Database in ASP.Net using C# and VB.Net

Upload Save Retrieve and Play MP4 Video files with live streaming from Database in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads