1

I have DetailsView with users Info (Name, Email, Picture). That DetailsView control can be edited. Values are from DataBase

 protected void DVUserInfoShow_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{


    FileUpload EditAvatar = (FileUpload)DVUserInfoShow.FindControl("EditAvatar");
    if (EditAvatar.HasFile)
    {
        string image_path = "~/images/user_images/" + EditAvatar.FileName;
        EditAvatar.SaveAs(Server.MapPath(image_path));
        e.NewValues["Avatar"] = EditAvatar.FileName;

    }

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



}

The problem is with e.NewValues["Avatar"] = e.OldValues["Avatar"];, when user updates his name and email, the picture value is set to null. And that code doesn't work. What I'm doing wrong?

RaShe
  • 1,851
  • 3
  • 28
  • 42

1 Answers1

1

OldValues collection is only available when DetailView is bounded with a declarative DataSource, otherwise OldValues collection will contain null in ItemUpadting event.

What you can do is inside DVUserInfoShow_ItemUpdating event, get current data from database and save in some local variables.

Waqas
  • 6,812
  • 2
  • 33
  • 50
  • Now i know the answer, i used declarative DataSource, but it didn't work anyway.. The problem was that i didn't set DataKeyNames.. – RaShe Feb 08 '12 at 01:59