DetailsView image update trouble.(Urgent help needed)

Discussion in 'ASP .Net' started by jens Jensen, Mar 30, 2006.

  1. jens Jensen

    jens Jensen Guest

    Hello,
    I have an image file name in a table that a let users modify via a
    GridView/DetailView pair.

    * The actual image file is saved in the image folder of the application.
    But the image is rendered in a templatefield 's <itemtemplate> on the
    detailsview.
    In the <edititemtemplate> i have a fileupload control . leting users upload
    a new picture and thus replace the old one.

    I use the new two ways databinding feature Bind(..... for the data update.

    the problem here here when a field other then the image is updated , then my
    image filename is set to its default value (NULL).
    I dont want that. When the picture is not updated on the detailsView, it
    should keep its default value.


    How can i achieve this. I have now spende more then 10 hours on trying to
    achieve this and i therefore need some fresh thought here.

    Many thanks in advance

    JJ
     
    jens Jensen, Mar 30, 2006
    #1
    1. Advertisements

  2. jens Jensen

    Guest Guest

    The field is probably not re-setting to it's default value it is being set
    to the value of the control, which if the user doesn't select a new file to
    upload, is presumably null.

    The answer is to populate the control that is bound to the image filename
    field, with the image filename automatically so that if the user does not
    select a new value for that control then it will update the field to the same
    value that is in it already.

    If you could post your code I might be able to give you more specific advice.
     
    Guest, Mar 30, 2006
    #2
    1. Advertisements

  3. jens Jensen

    jens Jensen Guest

    <asp:TemplateField HeaderText="Image">

    <itemtemplate>



    <asp:Image ImageUrl='<%# Eval("pic", "../admin/images/{0}" ) %>'
    runat="server" ID="image" />





    </itemtemplate>

    <EditItemTemplate>


    <asp:FileUpload ID="fileUpload1" runat="server" />

    </EditItemTemplate>

    </asp:TemplateField>


    </Fields>



    update event handler

    .........

    FileUpload fileUpload = ((DetailsView)sender).FindControl("FileUpload1") as
    FileUpload;

    if (fileUpload.HasFile)

    {

    fileUpload.SaveAs(Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["picPaths"].ToString())
    + fileUpload.FileName);





    SqlDataSource1.UpdateParameters["pic"].DefaultValue = fileUpload.FileName;

    }


    .........

    DetailsView datasource:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
    ConnectionStrings:prodDb %>"

    SelectCommand="SELECT * FROM [Prod] WHERE [Id] = @Id "



    WHERE [Id] = @Id "



    UpdateCommand=


    "

    UPDATE [Prod] SET

    [ProductName] = @ProductName,

    [pic]= @pic,

    WHERE [Id] = @Id




    "





    <<UpdateParameters>

    <asp:parameter Name="CompanyNumber" Type=int16 />

    <asp:parameter Name="pic" Type="String" />

    </UpdateParameters>


    <SelectParameters>

    <asp:ControlParameter ControlID="GridView1" Name="Id"

    PropertyName="SelectedValue" Type=int32 />

    </SelectParameters>





    </asp:SqlDataSource>



    Many thanks



    JJ
     
    jens Jensen, Mar 30, 2006
    #3
  4. jens Jensen

    Guest Guest

    The problem is that

    UPDATE [Prod] SET

    [ProductName] = @ProductName,

    [pic]= @pic,

    WHERE [Id] = @Id

    will update [pic] to the value of @pic everytime, if you don't provide a
    value for @pic then it will be set to null and so will your field.

    Is the update event handler the updated event or the updating event?

    Either way don't use

    SqlDataSource1.UpdateParameters["pic"].DefaultValue = fileUpload.FileName;

    use e.NewValues["pic"] = fileUpload.FileName;

    and make sure it is in the ItemUpdating event handler.

    Then you need to make sure that pic gets uopdated to it's existing value by
    having an else clause to your if (fileUpload.HasFile) that says something
    like

    else
    {
    e.NewValues["pic"] = e.OldValues["pic"];
    }





     
    Guest, Mar 30, 2006
    #4
  5. jens Jensen

    zawwar

    Joined:
    Nov 11, 2006
    Messages:
    1
    Likes Received:
    0
    I wanted to keep the Default Old values when FileUploade is not selected. But its taking null value. Can any one tell me the solution


    In DetailsView I have used
    <asp:TemplateField HeaderText="Image">
    <itemtemplate>
    <asp:Image ImageUrl='<%# Bind("Image") %>'
    runat="server" ID="image" />
    </itemtemplate>
    <EditItemTemplate>
    <asp:FileUpload ID="fileUpload1" runat="server" />
    </EditItemTemplate>
    </asp:TemplateField>

    In DetailsView ItemUpdating event i have used this

    Dim fileUpload As FileUpload = CType(CType(sender, DetailsView).FindControl("FileUpload1"), FileUpload)
    If fileUpload.HasFile Then
    Dim spath As String
    Dim path As String
    spath = "~\Upload\" & fileUpload.FileName()
    path = Server.MapPath(spath)
    fileUpload.SaveAs(path)
    ObjectDataSource1.UpdateParameters("Image").DefaultValue = fileUpload.FileName
    Else
    e.NewValues("Image") = e.OldValues("image")
    End If
     
    Last edited: Nov 11, 2006
    zawwar, Nov 11, 2006
    #5
  6. jens Jensen

    mezo

    Joined:
    Jun 11, 2007
    Messages:
    1
    Likes Received:
    0
    iam confused

    iam using asp.net2 with c#
    and i feel confused
    plzzz can any one put
    example
    from a - z
    plzzz
     
    mezo, Jun 11, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.