locked
Update a video inside gridview C# RRS feed

  • Question

  • User-2094959909 posted

    hello, 

    i had added so many operations (name of operation, number of operations, line(list), machine(list),..., video) so for the add is all good i have a fileupload for video with this code (i don(t have database for videos i uploaded them directly) 

    string filename;
    string video;
    filename = FileUpload1.FileName;
    video = Server.MapPath("~/VideosData/") + filename;
    FileUpload1.SaveAs(video);

    so in the show of this operations i used a gridview it showed all the operations with in the video area (ex: video.mp4) so when i want to update an operation the fields filled with list it comes with list to  update but the video it still textbox but i need a fileupload to change the current video 

    so if any one can help please 

    Thanks in advance

    Saturday, August 11, 2018 7:42 PM

All replies

  • User409696431 posted

    " when i want to update an operation the fields filled "

    You use the term "operations" many times in your post, but we have no idea what you mean by that.

    Please clarify your question.   This: " i used a gridview it showed all the operations with in the video area " is very unclear.  How would a GridView be "in a video area"?

    From your post title, I think you mean you are showing a video inside a GridView?  Or a video name?  Are you trying to update a video inside a GridView?  If so, you need to put a file upload control in the EditTemplate of that field, and handle the file upload in the RowUpdating event of the GridView.

    The following link shows an example using an image, but the same applies for a video.  (Just look at the code for the employee image column.)

    https://www.c-sharpcorner.com/UploadFile/261736/gridview-with-image-and-fileupload-controls-in-Asp-Net/ 

    Sunday, August 12, 2018 2:04 AM
  • User-2094959909 posted

    Hi KathyW,

    so first of all i have to add an operation (clothes making operation) to database, this operation is made of (opeation number, operation name, line(list), model(list), ... , video(fileupload)) so when i add an operation i have another page to set those operations visible by a gridview, and i have update link to edit the operation i want so when i click on the link it gave me the fields to change whoever i want so like the model and line who are filled by a list i have lists to update them upon the content of lists but when i come to the video column it gave me the name of the video inside a textbox (exemple : video.mp4) and that's an issue because i need a fileupload instead when i have to change the video by a new one, my question is how can i have a fileupload and edit that video if you can help please and of course if you want any other explaination just ask.

    ps : i don't use a database for videos !!!

    Thanks in advance 

    Sunday, August 12, 2018 4:45 PM
  • User475983607 posted
    Your question does not make sense. Post code that reproduces the issue, explain how you expect the feature to work, and include any errors.
    Sunday, August 12, 2018 5:32 PM
  • User409696431 posted

    Read the link I gave you in my earlier post if you want to have a file upload inside a GridView edit field.  If that is not your question, you will need to try again to explain it.

    The rest of your post makes no sense to anyone but you.  We have no idea what you are calling an "operation", or what that has to do with a GridView and a file upload.

    Keep your posts simple: only include the part that you are having trouble with, and if you have code you have tried and doesn't work, post it.  Again: simplify it to only the part you are having trouble with.

    Sunday, August 12, 2018 9:45 PM
  • User-2094959909 posted

    thank you anyway KathyW

    Monday, August 13, 2018 3:43 PM
  • User-2094959909 posted

    hello, 

    i have a gridview and i show on it name of the video and the video so when i click on it a new window opens with the video so i can watch it, i have the ability to update and delete 

    so when i click on update it gives me a field to edit the name and a field to update the video and that's the issue it shouldn't give me textbox in the video place i need a fileupload 

    ( " !!!!!!! i don't use a database for the videos !!!!!!! " )

    so if anyone can help please, i tried so many solutions and it didn't worked for me!

    Thanks in advance.

    Sunday, September 9, 2018 9:49 PM
  • User409696431 posted

    I already gave you a link illustrating how to put a fileupload in a GridView edit template, in one of your earlier threads.

    https://www.c-sharpcorner.com/UploadFile/261736/gridview-with-image-and-fileupload-controls-in-Asp-Net/

    The answer hasn't changed.

    Monday, September 10, 2018 1:35 AM
  • User-2094959909 posted

    KathyW tnaks for your respond, 

    i need one more explanation from you in the code behind you have a function 

     protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)   
        {  

    can i put the code to update just for the video, because the other rows they can be updated normaly i have just a problem in the video row ... so can i take the part just for the video ??

    Monday, September 10, 2018 2:45 PM
  • User-893317190 posted

    Hi Omar27,

    You could combine your sqldatasource  with KathyW's sample. In this way , you only need to write your additional code in  gridview's OnRowUpdated event and RowUpdating

    Please pay attention that you should write your update logic in OnRowUpdated event not  RowUpdating(which is usually used) and only use RowUpdating to record the rowIndex.

    Because if you write in RowUpdating , the sqldatasource will cover the result of your of sql.

    Below is my code.

    <form id="form1" runat="server">
              <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  Width="400px" Height="80px"   DataKeyNames="id" DataSourceID="SqlDataSource1"  OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating">
                  <Columns>
                      <asp:BoundField DataField="id" ReadOnly="true" />
                      <asp:BoundField DataField="name" />
                      <asp:TemplateField>
                          <ItemTemplate>
    <video controls="controls"  src="<%# Eval("videoPath") %>">
                              
                              </video>
                          </ItemTemplate>
                      
                          <EditItemTemplate>
                                  <asp:FileUpload runat="server" ID="upload"></asp:FileUpload>
                          </EditItemTemplate>
                      </asp:TemplateField>
                      <asp:CommandField ShowEditButton="true" ShowCancelButton="true"  />
                  </Columns>
    
              </asp:GridView>
              
              <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:bjhksjConnectionString %>" DeleteCommand="DELETE FROM [Video] WHERE [Id] = @Id" InsertCommand="INSERT INTO [Video] ([videoPath], [Name]) VALUES (@videoPath, @Name)" SelectCommand="SELECT [Id], [videoPath], [Name] FROM [Video]" UpdateCommand="UPDATE [Video] SET [videoPath] = @videoPath, [Name] = @Name WHERE [Id] = @Id">
                  <DeleteParameters>
                      <asp:Parameter Name="Id" Type="Int32" />
                  </DeleteParameters>
                  <InsertParameters>
                      <asp:Parameter Name="videoPath" Type="String" />
                      <asp:Parameter Name="Name" Type="String" />
                  </InsertParameters>
                  <UpdateParameters>
                      <asp:Parameter Name="videoPath" Type="String" />
                      <asp:Parameter Name="Name" Type="String" />
                      <asp:Parameter Name="Id" Type="Int32" />
                  </UpdateParameters>
              </asp:SqlDataSource>
              
        </form>
     

    Code behind.

     public int index { get; set; }
            protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                // in RowUpdating only record the RowIndex to use in RowUpdated
                index = e.RowIndex;
              
            }
    
            protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
            {
                GridViewRow row = GridView1.Rows[index];
                Response.Write(index);
                Response.Write(row);
                FileUpload upload = row.FindControl("upload") as FileUpload;
    
                string path = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName);
                upload.PostedFile.SaveAs(Server.MapPath("/upload/") + path);
                int id = Convert.ToInt32(e.Keys["id"]);
             
                string sql = "update Video set videoPath=@videoPath where id=@id";
              
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand com = new SqlCommand(sql, con))
                    {
                        con.Open();
                        com.Parameters.AddWithValue("id", id);
                        com.Parameters.AddWithValue("videoPath", "/upload/" + path);
    
                        com.ExecuteNonQuery();
                    }
                }
            }

    Best regards,

    Ackerly Xu

    Tuesday, September 11, 2018 8:31 AM