Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i've been trying to store a video to my sql database with no success.

here is my FileUpload

C#
<asp:FileUpload ID="FileUpload1" runat="server" />
   <asp:Button ID="btnUploadVideo" runat="server" Text="Upload"
     OnClick="btnUploadVideo_Click" />
     <br />


Here is my btnUploadVideo_Click

C#
protected void btnUploadVideo_Click(object sender, EventArgs e)
    {
        // Read the file and convert it to Byte Array
        string filePath = FileUpload1.PostedFile.FileName;
        string filename = Path.GetFileName(filePath);
        string ext = Path.GetExtension(filename);
        string contenttype = String.Empty;

        //Set the contenttype based on File Extension
        switch (ext)
        {

            case ".avi":
                contenttype = "video/avi";
                break;
            case ".mp4":
                contenttype = "video/mp4";
                break;
            case ".wmv":
                contenttype = "video/wmv";
                break;

        }
        if (contenttype != String.Empty)
        {

            Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);

            //insert the file into database

            string strQuery = "insert into VIDEO(Video)" +
               " values (@Video)";
            SqlCommand cmd = new SqlCommand(strQuery);
            //cmd.Parameters.Add("@Candidate_Image", SqlDbType.VarChar).Value = filename;
            //cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value
            //  = contenttype;
            cmd.Parameters.Add("@Video", SqlDbType.Binary).Value = bytes;
            InsertUpdateData(cmd);
            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Text = "Video is Uploaded Successfully";
        }
        else
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "File format not recognised." +
              " Upload Audio/avi/mp4/wmv formats";
        }
    }

on my Sql database, Video datatype is varbinary(MAX).

When i try to change the format from .avi to png or jpg it does store but video format it does not store. The error message is "The connection was reset"

The connection to the server was reset while the page was loading.     

  The site could be temporarily unavailable or too busy. Try again in a few moments.
  If you are unable to load any pages, check your computer's network connection.
  If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.
I've also tried other browser it keep on giving me the same error
Posted
Updated 10-Jun-13 16:35pm
v2
Comments
Ron Beyer 10-Jun-13 14:10pm    
You really should not store video files in a database, its better to upload them to a directory and store the directory in the database. This puts a huge load on your database server, I'm guessing your DB provider is something you purchased online, and they limit the load on the server before resetting the loaded connection. They do this to provide uptime and reliability to all customers so you don't hog connections from the pool.
[no name] 10-Jun-13 15:36pm    
Thanx for the advice... i will try to save a path and save the video in a folder if that is what u sugesting...
CHill60 10-Jun-13 16:49pm    
Worth posting as a solution for the googlers out there .. it would get my vote
ZurdoDev 10-Jun-13 14:15pm    
As Ron said, I would suggest storing a link to the file in the db and not the actual file. But it could also be the file is too big and your web request is timing out.
[no name] 10-Jun-13 15:37pm    
Thanx guys

1 solution

Suggestion as answer:

Do not store video files in a database. The best practice is to upload them to a directory in your server and then store the path to this file in the database.

A lot of online DB/web providers limit the amount of data you can run through the connection. This is to ensure uptime and reliability to all users, since your database doesn't have a dedicated server, it shares it with all the customers who are hosting on that machine.
 
Share this answer
 

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