Click here to Skip to main content
15,995,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

SQL
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


ASP.NET
<%@ 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>


C#
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;
    //this is the array of bytes which will hold the data (file)
    protected void Button1_Click(object sender, EventArgs e)
    {
        int usercount;
        //check the file

        if (FileUpload1.HasFile && FileUpload1.PostedFile != null
            && FileUpload1.PostedFile.FileName != "")
        {
            HttpPostedFile file = FileUpload1.PostedFile;
            //retrieve the HttpPostedFile object

            buffer = new byte[file.ContentLength];
            int bytesReaded = file.InputStream.Read(buffer, 0, FileUpload1.PostedFile.ContentLength);
            //the HttpPostedFile has InputStream porperty (using System.IO;)
            //which can read the stream to the buffer object,
            //the first parameter is the array of bytes to store in,
            //the second parameter is the zero index (of specific byte)
            //where to start storing in the buffer,
            //the third parameter is the number of bytes 
            //you want to read (do u care about this?)

            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";
        }
    }

    //create a sqlcommand object passing the query and the sqlconnection object
    //when declaring the parameters you have to be sure 
    //you have set the type of video column to varbinary(MAX)

    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();
    }
}


C#
<%@ WebHandler Language="C#" Class="VideoHandler" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
//if your data is large in size then you need to increase  maxRequestLength in web.config under System.web node.
//<system.web>
//<httpRuntime maxRequestLength="2097151" />
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;
        }
    }
}
Posted
Comments
[no name] 17-May-14 17:36pm    
Learning how to use the debugger is a very fine skill to have.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900