I am Working on my fyp project, i have a program to upload videos to sql server then show on th web pages.
I am following this tutorial but the problem is it is storing the video in sql server but did not show on the web page, neither arises an error or exception.
Create Proc uspAddVideos
@video varbinary(MAX), @VideoName nvarchar(100), @VideoSize bigint, @success bit out
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
INSERT INTO TableVideos (Video, VideoName, VideoSize) VALUES (@video,@VideoName,@VideoSize)
SET @success = 1
END TRY
BEGIN CATCH
SET @success = 0
END CATCH
END
Create Proc uspGetVideo
@VideoId int
AS
BEGIN
SET NOCOUNT ON;
SELECT Video,ID FROM TableVideos WHERE ID = @VideoId
END
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="VideoTesting.aspx.cs" Inherits="VideoTesting" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Upload | Preview Vidoes</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Upload Video"></asp:Label>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /> <br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button2" runat="server" Text="Show Video" OnClick="ButtonShowVideo_Click"/><br />
<asp:Panel ID="Panel1" runat="server" Height="126px" Width="881px">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<object id="player" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" height="300" width="300">
<param name="url" value='<%# "VideoHandler.ashx?id=" + Eval("ID") %>'/>
<param name="showcontrols" value="true" />
<param name="autostart" value="true" />
</object>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
</div>
</form>
</body>
</html>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
public partial class VideoTesting : System.Web.UI.Page
{
Boolean success;
DBClass db1 = new DBClass();
protected void Page_Load(object sender, EventArgs e)
{
}
byte[] buffer;
protected void Button1_Click(object sender, EventArgs e)
{
int usercount;
if (FileUpload1.HasFile && FileUpload1.PostedFile != null
&& FileUpload1.PostedFile.FileName != "")
{
HttpPostedFile file = FileUpload1.PostedFile;
buffer = new byte[file.ContentLength];
int bytesReaded = file.InputStream.Read(buffer, 0, FileUpload1.PostedFile.ContentLength);
if (bytesReaded > 0)
{
try
{
db1.sqlcmd = new SqlCommand("uspAddVideos");
using (SqlDataAdapter sda = new SqlDataAdapter())
{
db1.sqlcmd.CommandType = CommandType.StoredProcedure;
db1.sqlcmd.Parameters.AddWithValue("@video", buffer);
db1.sqlcmd.Parameters.AddWithValue("@VideoName", FileUpload1.FileName);
db1.sqlcmd.Parameters.AddWithValue("@VideoSize", file.ContentLength);
db1.sqlcmd.Parameters.Add("@success", SqlDbType.Bit);
db1.sqlcmd.Parameters["@success"].Direction = ParameterDirection.Output;
db1.sqlcmd.Connection = db1.sqlcon;
db1.sqlcon.Open();
usercount = (Int32)db1.sqlcmd.ExecuteScalar();
success = Convert.ToBoolean(db1.sqlcmd.Parameters["@success"].Value);
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if (success == true)
{
Response.Write("Video Uploaded Successfully");
}
else
{
Response.Write("Unsuccessfull, Please Try Again");
}
}
}
}
else
{
Label1.Text = "Choose a valid video file";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
}
private DataTable GetSpecificVideo(object i)
{
SqlConnection con = new SqlConnection("Data Source=MUHAMMADTAQI;Initial Catalog=ECII(Old);Persist Security Info=True;User ID=sa;Password=finally");
SqlCommand cmd = new SqlCommand("uspGetVideo", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand.Parameters.Add("@VideoId", SqlDbType.Int).Value = (int)i;
DataTable Datatable = new DataTable();
da.Fill(Datatable);
return Datatable;
}
protected void ButtonShowVideo_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(TextBox1.Text);
Repeater1.DataSource = GetSpecificVideo(id);
Repeater1.DataBind();
}
}
<%@ WebHandler Language="C#" Class="VideoHandler" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public class VideoHandler : IHttpHandler {
DBClass db1 = new DBClass();
public void ProcessRequest (HttpContext context) {
try
{
db1.sqlcmd = new SqlCommand("uspGetVideo");
using (SqlDataAdapter sda = new SqlDataAdapter())
{
db1.sqlcmd.CommandType = CommandType.StoredProcedure;
db1.sqlcmd.Parameters.AddWithValue("@VideoId", context.Request.QueryString["id"]);
db1.sqlcmd.Connection = db1.sqlcon;
db1.sqlcon.Open();
SqlDataReader reader = db1.sqlcmd.ExecuteReader(CommandBehavior.Default);
if (reader.HasRows)
{
while (reader.Read())
{
context.Response.ContentType = reader["VideoName"].ToString();
context.Response.BinaryWrite((byte[])reader["Video"]);
}
}
}
}
finally
{
db1.sqlcon.Close();
}
}
public bool IsReusable {
get {
return false;
}
}
}