Search code examples
asp.netvb.netformview

FormView ItemUpdating e.oldValues and e.NewValues seems null


I have a FormView on a page in my asp.net 4.5 VB application. I have a FileUpload in EditItemTemplate and InsertItemTemplate of my FormView. InsertItemTemplate works fine as expected. I want my FormView to:

  1. Insert values into SQL DB, and save the file into the file system and save its name into DB (Which it does as expected).
  2. Update DB and the file system by replacing the old values and with new ones (this is OK too.) and previous file of the row with the new file (This is OK too) and Delete the old file... (NOT OK!)
  3. Keep the previous file name in DB if FileUpload has no file.(NOT OK!) records NULL if is empty.
  4. Delete record from db along with the file associated (This is OK too.)

FormView:

<asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="KurumId" DataSourceID="KurumlarDS" Width="500px">
<EditItemTemplate>
    KurumId:
    <asp:Label ID="KurumIdLabel1" runat="server" Text='<%# Eval("KurumId") %>' />
    <br />
    KurumAdi:
    <asp:TextBox ID="KurumAdiTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumAdi") %>' />
    <br />
    KurumLogoPath:
    <%--<img runat="server" ID="imgKurumLogo" src='<%# Eval("KurumLogoPath", "../images/kurum/{0}?w=80")%>' alt="Kurum Logosu" />
     <br />--%>
    <asp:FileUpload ID="KurumLogoPathFUU" runat="server" />
    <br />
    KurumTelefon:
    <asp:TextBox ID="KurumTelefonTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumTelefon") %>' />
    <br />
    KurumBelgeGecer:
    <asp:TextBox ID="KurumBelgeGecerTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumBelgeGecer") %>' />
    <br />
    KurumWebAdresi:
    <asp:TextBox ID="KurumWebAdresiTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumWebAdresi") %>' />
    <br />
    KurumEMail:
    <asp:TextBox ID="KurumEMailTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumEMail") %>' />
    <br />
    KurumTarihce:
    <asp:TextBox ID="KurumTarihceTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumTarihce") %>' />
    <br />
    KurumRengi:
    <asp:TextBox ID="KurumRengiTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumRengi") %>' />
    <br />
    KurumAdres:
    <asp:TextBox ID="KurumAdresTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumAdres") %>' TextMode="MultiLine" />
    <br />
    KurumSortExpression:
    <asp:TextBox ID="KurumSortExpressionTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumSortExpression") %>' />
    <br />
    KurumLat:
    <asp:TextBox ID="KurumLatTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumLat") %>' />
    <br />
    KurumLon:
    <asp:TextBox ID="KurumLonTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumLon") %>' />
    <br />
    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Güncelle" />
    &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Vazgeç" />
</EditItemTemplate>
<InsertItemTemplate>
</InsertItemTemplate>
<ItemTemplate>
</ItemTemplate>
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />

Related CodeBehind:

Private Sub FormView1_ItemUpdating(sender As Object, e As FormViewUpdateEventArgs) Handles FormView1.ItemUpdating
    Dim fu As FileUpload = CType(FormView1.FindControl("KurumLogoPathFUU"), FileUpload)
    Dim MyPath As String = Server.MapPath("~/images/kurum/")
    Dim fName As String = fu.FileName
    Dim FullPathedFile As String = MyPath & fName
    Try
        If fu.HasFile = True Then
            Dim oldName As String = e.OldValues("KurumLogoPath")
            Dim newName As String = e.NewValues("KurumLogoPath")
            If File.Exists(MyPath & oldName) Then
                File.Delete(MyPath & oldName)
            End If
            e.NewValues("KurumLogoPath") = fName
            fu.SaveAs(FullPathedFile)
        Else
            e.NewValues("KurumLogoPath") = e.OldValues("KurumLogoPath")
        End If
    Catch ex As Exception
        Response.Write("Cannot Update!" & ex.Message())
        e.Cancel = True
    End Try
End Sub

I can see MyPath but not e.NewValues("KurumLogoPath") or e.OldValues("KurumLogoPath") I also tried e.NewValues.Item("KurumLogoPath") and e.NewValues.Item("KurumLogoPath") with no success. So I think that all e.OldValues and e.NewValues are null.. Thus file.delete tries to delete C:\inetpub\wwwroot\WebApp1\images\kurum instead of C:\inetpub\wwwroot\WebApp1\images\kurum\oldfile.jpg as a result deletes nothing.

I have checked permissions for the folder and Application, they are OK. I have googled, binged, yahooed, trialed & errored with no success. It's been about two days now that I am on this :(

What am I doing wrong? Any ideas?

Thanks in advance.


Solution

  • After causita's comment I used index for the values. It worked for all columns except for KurumLogoPath column. While trying to find that columns index I realized that no index number was related to KurumLogoPath column. Then I realized indices are coming from <%# Bind("ColumName") %> statements.. But FileUpload control doesn't have databinding in it's nature just like a textbox or a hiddenfield did. So I added a hiddenfield under it:

    <asp:FileUpload ID="KurumLogoPathFUU" runat="server" /> <asp:HiddenField ID="KurumLogoPathHF" Value='<%# Bind("KurumLogoPath") %>' runat="server" />

    and then both e.OldValues("KurumLogoPath") and e.OldValues(2) and file deleting process worked very well.