DetailsView image update trouble.(Urgent help needed)

J

jens Jensen

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
 
G

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.
 
J

jens Jensen

<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
 
G

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"];
}





jens Jensen said:
<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
 
Joined
Nov 11, 2006
Messages
1
Reaction score
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:
Joined
Jun 11, 2007
Messages
1
Reaction score
0
iam confused

iam using asp.net2 with c#
and i feel confused
plzzz can any one put
example
from a - z
plzzz
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top