Isiah Jones
Isiah Jones

Reputation: 104

Trying to use File Upload in Listview? Update not working

Here is my ListView. I'm using the OnItemCommand to reference the code in code-behind. Here I'm trying to Update the database with the new FileUpload control.

 <%--Listview--%>
<asp:ListView runat="server" ID="livLocation" class="container"
    DataKeyNames="LocationID"
    DataSourceID="sdsListViewLocation"
    EmptyDataText="No data to display"
    InsertItemPosition="FirstItem"
    OnItemInserted="livLocation_ItemInserted"
    OnItemUpdated="livLocation_ItemUpdated"
    OnItemDeleted="livLocation_ItemDeleted"
    OnItemCanceling="livLocation_ItemCanceling"
    OnItemCommand="livLocation_ItemCommand">
</asp:ListView>

The Insert works perfectly fine. When an Update is executed a Null error is thrown for all FindControls. I believe for some reason the FindControls are not working for the Update. I have tried giving each control their own id's but that still didn't fix the problem. I have been referencing this post but nothing has helped so far: Upload images with fileupload within a Listview asp.net

 protected void livLocation_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        //
        if (e.CommandName == "Insert")
        {
            // Find controls on insert.
            TextBox txtLocation = (TextBox)livLocation.InsertItem.FindControl("txtLocation");
            TextBox txtImage = (TextBox)livLocation.InsertItem.FindControl("txtImage");
            FileUpload fuiImage = (FileUpload)livLocation.InsertItem.FindControl("fuiImage");

            // Get today's date
            String strDate = DateTime.Now.ToString("MM-dd-yyyy-h-m-stt");

            // If file is there to upload.
            if (fuiImage.HasFile)
            {
                // Set path.
                String strFileName = txtLocation.Text + "-" + strDate + ".jpg";
                String strPath = Request.PhysicalApplicationPath + "Image\\Location\\" + strFileName;

                // Save file.
                fuiImage.SaveAs(strPath);

                // Fill Image textbox
                txtImage.Text = strFileName;
            }
            else
            {
                // Do nothing
            }
        }
        else if (e.CommandName == "Update")
        {
            // Find controls on insert.
            TextBox txtLocation = (TextBox)livLocation.InsertItem.FindControl("txtLocation");
            TextBox txtImage = (TextBox)livLocation.InsertItem.FindControl("txtImage");
            FileUpload fuiImage = (FileUpload)livLocation.InsertItem.FindControl("fuiImage");

            // Get today's date
            String strDate = DateTime.Now.ToString("MM-dd-yyyy-h-m-stt");

            // If file is there to upload.
            if (fuiImage.HasFile)
            {
                // Set path.
                String strFileName = txtLocation.Text + "-" + strDate + ".jpg";
                String strPath = Request.PhysicalApplicationPath + "Image\\Location\\" + strFileName;

                // Save file.
                fuiImage.SaveAs(strPath);

                // Fill Image textbox
                txtImage.Text = strFileName;
            }
            else
            {
                // Do nothing
            }
        }
        else if (e.CommandName == "Delete")
        {
            // Delete file.
            FileUpload fuiImage = (FileUpload)livLocation.InsertItem.FindControl("fuiImage");
            String strPath = Request.PhysicalApplicationPath + "Image\\Location\\" + fuiImage.FileName;

            System.IO.File.Delete("strPath");

        }
        else
        {
            // Do nothing
        }
    }
}

Upvotes: 0

Views: 285

Answers (1)

Hany Habib
Hany Habib

Reputation: 1405

As discussed its always nice to use the sender object for better maintainability later. for your issue kindly visit the following link to get the edited item as your syntax is not correct: https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listview.itemediting?view=netframework-4.8

it shows more details about item editing and how to fetch the edited item with the new edit index.

the
(TextBox)livLocation.InsertItem is incorrect it must be EditItem so it will be (FileUpload)(sender as ListView).EditItem.FindControl.....

Upvotes: 1

Related Questions