title: How to display video in webpage using c# .net 3.5 and sqlserver 2008
body: <p>Dear Friends we have code for upload video file into database and working fine .</p>
  
  <p>We also have code to display video online on webpage but when we click on video file it does not display.when we click on video file downloading start automatically in IDM(internet download manager).</p>
  
  <p>I unable to understand please anyone can suggest us how we can display my video file smoothly please</p>
  
  <p>Below my code for uploading video. </p>
  
  <p>Database table</p>
  
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE [dbo].[tblVideos](
      [id] [int] IDENTITY(1,1) NOT NULL,
      [Video_Name] [varchar](50) NULL,
      [Name] [varchar](50) NULL,
      [ContentType] [varchar](50) NULL,
      [Data] [varbinary](max) NULL
  ) ON [PRIMARY]
  </code></pre>
  
  <p>Aspx code to upload video</p>
  
  <pre><code>&lt;table style="margin-left:150px; height:546px"&gt;
    &lt;tr class="tr"&gt;
        &lt;td colspan="3"&gt;
            &lt;asp:Label ID="Label1" runat="server" 
                Font-Size="15pt" ForeColor="#0099FF" Text="Add New Video"&gt;&lt;/asp:Label&gt;
  
  
  
            &amp;nbsp;&lt;br /&gt;
            &lt;asp:Label ID="Label2" runat="server" Font-Names="MS Reference Sans Serif" 
              Font-Size="7pt" Text="Make Sure Video in MP4 format"&gt;&lt;/asp:Label&gt;
        &lt;/td&gt;
  
    &lt;/tr&gt;
    &lt;tr class="tr"&gt;
     &lt;td class="style1"&gt; Video Name&amp;nbsp; :&lt;/td&gt;
     &lt;td&gt;&lt;asp:TextBox ID="txtideoname"  Height="24px" Width="270px" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;
      &lt;td&gt;
        &amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="tr"&gt;
     &lt;td class="style1"&gt;Video type :&lt;/td&gt;
     &lt;td&gt;&lt;asp:TextBox ID="txtvideotype"  Height="24px" Width="270px" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;
      &lt;td&gt;
        &amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
  
    &lt;tr class="tr"&gt;
     &lt;td  class="style1"&gt;video:&lt;/td&gt;
     &lt;td&gt;
       &lt;asp:FileUpload ID="FileUpload1" runat="server" /&gt;&lt;/td&gt;
      &lt;td&gt;
        &amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="tr"&gt;
     &lt;td&gt;&amp;nbsp;&lt;/td&gt;
     &lt;td&gt;&lt;asp:Button ID="btnupload" runat="server" Text="Upload" Width="132px" Height="34px" BackColor="Red" BorderStyle="None" Font-Bold="True" ForeColor="White" onclick="btnupload_Click"/&gt;&lt;br /&gt;
         &lt;asp:Label ID="lblMsg" runat="server"/&gt;&lt;/td&gt;
      &lt;td&gt;
        &amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="tr"&gt;
        &lt;td&gt;
            &amp;nbsp;&lt;/td&gt;
        &lt;td&gt;
            &amp;nbsp;&lt;/td&gt;
        &lt;td&gt;
          &amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code> 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["con1"].ConnectionString;
              using (SqlConnection con = new SqlConnection(strConnString))
              {
                  using (SqlCommand cmd = new SqlCommand())
                  {
                      cmd.CommandText = "insert into tblVideos(Video_Name,Name, ContentType, Data) values (@Video_Name,@Name, @ContentType, @Data)";
                      cmd.Parameters.AddWithValue("@Video_Name", txtideoname.Text);
  
                      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);
     }
  </code></pre>
  
  <p>aspx code for display video</p>
  
  <pre><code> &lt;asp:FileUpload ID="FileUpload1" runat="server" /&gt;
  &lt;asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /&gt;
  &lt;hr /&gt;
  &lt;asp:DataList ID="DataList1" Visible="true" runat="server" AutoGenerateColumns="false"
      RepeatColumns="2" CellSpacing="5"&gt;
      &lt;ItemTemplate&gt;
          &lt;u&gt;
              &lt;%# Eval("Name") %&gt;&lt;/u&gt;
          &lt;hr /&gt;
          &lt;a class="player" style="height: 300px; width: 300px; display: block" href='&lt;%# Eval("Id", "FileCS.ashx?Id={0}") %&gt;'&gt;
          &lt;/a&gt;
      &lt;/ItemTemplate&gt;
  &lt;/asp:DataList&gt;
  &lt;script src="FlowPlayer/flowplayer-3.2.12.min.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      flowplayer("a.player", "FlowPlayer/flowplayer-3.2.16.swf", {
          plugins: {
              pseudo: { url: "FlowPlayer/flowplayer.pseudostreaming-3.2.12.swf" }
          },
          clip: { provider: 'pseudo', autoPlay: false},
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>cs code for display video</p>
  
  <pre><code>    private void BindGrid()
         {
          string strConnString =        ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
          using (SqlConnection con = new SqlConnection(strConnString))
          {
              using (SqlCommand cmd = new SqlCommand())
              {
                  cmd.CommandText = "select Id, Name from tblVideos";
                  cmd.Connection = con;
                  con.Open();
                  ddlvideogallery.DataSource = cmd.ExecuteReader();
                  ddlvideogallery.DataBind();
                  con.Close();
              }
          }
      }
  </code></pre>
  
  <p>Generic Handler code for display video</p>
  
  <pre><code>    using System;
      using System.Collections;
      using System.Data;
      using System.Linq;
      using System.Web;
      sing System.Web.Services;
      using System.Web.Services.Protocols;
      using System.Xml.Linq;
      using System.Data.SqlClient;
      using System.Configuration;
  
    namespace Mindpower
     {
     /// &lt;summary&gt;
     /// Summary description for $codebehindclassname$
  /// &lt;/summary&gt;
  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  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["con1"].ConnectionString;
          string name;
          using (SqlConnection con = new SqlConnection(strConnString))
          {
              using (SqlCommand cmd = new SqlCommand())
              {
                  cmd.CommandText = "select Name, Data, ContentType from tblVideos 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;
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Please any one can give some idea or solution</p>
  
  <p>Thanks</p>