By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,417 Members | 1,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,417 IT Pros & Developers. It's quick & easy.

DetailsView image update trouble.(Urgent help needed)

P: n/a

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

Mar 30 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

"jens Jensen" wrote:

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

Mar 30 '06 #2

P: n/a
<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.Configurat ion.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
Mar 30 '06 #3

P: n/a
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"];
}

"jens Jensen" wrote:
<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.Configurat ion.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

Mar 30 '06 #4

P: 1
Many thanks, that was useful for me too. There's not another place in wich this issue is explained better.

Soz for my English
Apr 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.