Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone,

On my website I am successfully able to upload images to a folder location in my project folder and copy the file path to the database so that the images can be displayed and viewed in a data grid view. What I want to be able to do now is upload videos in the same way and then when they are displayed in the data grid view, play them in a pop up player or new window? How would I go about this as I have no idea?

Here is my code for uploading the files and displaying them so far

C#
protected void UploadFile(object sender, EventArgs e)
        {
            if (btnBrowse.PostedFile != null && btnBrowse.PostedFile.ContentLength > 0)
            {

                
                try
                {
                    string fileName = Path.GetFileName(btnBrowse.PostedFile.FileName);
                    string folder = Server.MapPath("~/BusinessProfilesContent/" + Session["BusinessName"]);
                    Directory.CreateDirectory(folder);
                    btnBrowse.PostedFile.SaveAs(Path.Combine(folder, fileName));

                    //add data to the database and the file path
                    SqlConnection conn;
                    SqlCommand comm;
                    String connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
                    conn = new SqlConnection(connectionString);
                    comm = new SqlCommand("exec UploadContent @Description,@DateTime,@FilePath,@FileName,@BusinessName,@Username,@Incident_Name", conn);
                    comm.Parameters.Add("@Username", System.Data.SqlDbType.VarChar).Value = Session["User"];
                    comm.Parameters.Add("@Description", System.Data.SqlDbType.VarChar).Value = txtAddComments.Text.Trim();
                    comm.Parameters.Add("@DateTime", System.Data.SqlDbType.DateTime).Value = System.DateTime.Now;
                    comm.Parameters.AddWithValue("@FilePath", "~/BusinessProfilesContent/" + Session["BusinessName"] + "/" + fileName);
                    comm.Parameters.AddWithValue("@FileName", fileName);
                    comm.Parameters.Add("@BusinessName", System.Data.SqlDbType.VarChar).Value = Session["BusinessName"];
                    comm.Parameters.Add("@Incident_Name", System.Data.SqlDbType.VarChar).Value = DDLIncidentType.Text;

                    conn.Open();
                    comm.ExecuteNonQuery();

                    Response.Write("Uploaded: " + fileName);


                }
                catch
                {
                    Response.Write("Operation Failed!!!");
                }
            }


           
        }
 protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "Welcome " + Session["User"];
            Label2.Text = "Business: " + Session["BusinessName"];
           
            SqlConnection conn;
            SqlCommand comm;
            String connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

            conn = new SqlConnection(connectionString);
            comm = new SqlCommand("SELECT * from Business a, User_Acc c Where c.Username = @Username AND a.Business_ID = c.Business_ID", conn);
            comm.Parameters.Add("@Username", System.Data.SqlDbType.VarChar).Value = Session["User"];
            conn.Open();
            SqlDataReader reader = comm.ExecuteReader();
            while (reader.Read())
            {
                lblOwner.Text += reader["Owner_Name"];
                lblBusiness.Text += reader["Business_Name"];
                lblAddress.Text += reader["Address_Line_1"] + " - " + reader["Address_Line_2"] + " - " + reader["Address_Line_3"];
                lblProvince.Text += reader["Provence"];
                lblCounty.Text += reader["County"];
                lblVATNo.Text += reader["Vat_No"];
                lblEmail.Text += reader["Email"];
                lblTelephone.Text += reader["Telephone"];

            }
                SqlConnection connWrite;
                connWrite = new SqlConnection(connectionString);
                DataTable dt = new DataTable();
                SqlDataAdapter sda = new SqlDataAdapter();

                SqlCommand comm1 = new SqlCommand("exec DisplayContent @BusinessName", connWrite);
                    comm1.Parameters.Add("@BusinessName", System.Data.SqlDbType.VarChar).Value = Session["BusinessName"];
                    try
                    {
                        connWrite.Open();
                        sda.SelectCommand = comm1;
                        sda.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                    finally
                    {
                        connWrite.Close();
                        sda.Dispose();
                        connWrite.Dispose();
                    }



            
            reader.Close();
            conn.Close();

        }

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "view":
                        string filename = e.CommandArgument.ToString();
                        string path = Server.MapPath("~/BusinessProfilesContent/" + Session["BusinessName"] + "/" + filename);
                        byte[] bts = System.IO.File.ReadAllBytes(path);
                        Response.Clear();
                        Response.ClearHeaders();
                        Response.ContentType = "image";
                        Response.WriteFile(path);
                        Response.Flush();
                        Response.End();
                        break;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);

            }
        }


ASP.NET
 <asp:TableRow ID="TableRow9" runat="server">
                <asp:TableCell ID="TableCell17" runat="server" HorizontalAlign="Center" VerticalAlign="Top">Upload New Content :</asp:TableCell>
                <asp:TableCell ID="TableCell18" runat="server" HorizontalAlign="Left" VerticalAlign="Top"><asp:FileUpload ID="btnBrowse" runat="server" />
</asp:TableCell>
            </asp:TableRow>

<asp:TableRow ID="TableRow12" runat="server">
                <asp:TableCell ID="TableCell23" runat="server" HorizontalAlign="Center"><asp:Button ID="btnUpload" runat="server" Height="22px" Text="Upload" 
            Width="73px" OnClick="UploadFile"/>
</asp:TableCell>
            </asp:TableRow>

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
                        EmptyDataText = "No files uploaded" Width="834px" onrowcommand="GridView1_RowCommand">
    <Columns>
       <asp:BoundField DataField = "Incident_ID" HeaderText = "Incident ID" />
       <asp:BoundField DataField = "Incident_Type_Name" HeaderText = "Incident Name" />
       <asp:BoundField DataField = "Description" HeaderText = "Description" />
       <asp:BoundField DataField = "Date_Time" HeaderText = "Date and time of Upload" />
       <asp:BoundField DataField = "Business_Name" HeaderText = "Business Name" />
       <asp:BoundField DataField = "Provence" HeaderText = "Provence" />
       <asp:BoundField DataField = "County" HeaderText = "County" />
       <asp:BoundField DataField = "Username" HeaderText = "Username" />
       <asp:ImageField DataImageUrlField="File_Path" ControlStyle-Width="100"
        ControlStyle-Height = "100" HeaderText = "Preview Image"/>
         <asp:TemplateField ItemStyle-HorizontalAlign = "Center">
            <ItemTemplate>
               <asp:LinkButton ID="LinkButton1"  runat="server" CausesValidation="false" CommandName="view" CommandArgument = '<%#Eval("FileName")%>' >ViewFile</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


My Incident table is like so:

Incident_ID int
Business_ID int
User_ID int
Incident_Type_ID int
Description varchar(50)
Date_Time datetime
File_Path varchar(200)
FileName varchar(200)
Posted
Updated 25-Feb-14 6:16am
v2
Comments
Your code is about GridView, but you are talking about DataGridView.
Member 10609511 25-Feb-14 12:34pm    
sorry for the confusion - I just want to implement uploading and viewing of videos in what I already have, thanks
So, what is the problem while showing video?
Member 10609511 25-Feb-14 12:44pm    
I can't even upload video - when I insert a video file in the FileUpload control and hit the upload button I get a 'Page cannot be loaded' error
ravikhoda 25-Feb-14 23:47pm    
try to check size of video file. file upload control default file size value is 4 MB so you need to add limit of file size. you can increase that from web config like maxrequestlength property.

1 solution

Quote:
I can't even upload video - when I insert a video file in the FileUpload control and hit the upload button I get a 'Page cannot be loaded' error
Please go through some articles to know how exactly video is uploaded.

1. Upload Save Retrieve and Play MP4 Video files with live streaming from Database in ASP.Net using C# and VB.Net[^]
2. video Upload and view in asp.net c#[^]
 
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