Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 16th, 2008, 09:40 PM
Authorized User
 
Join Date: Apr 2008
Posts: 25
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Imar,

Sorry for not being detailed.

I have a MS-SQL database table called Employees which keeps information like name, address, etc, and the URL of the file being uploaded. The EmployeeID is integer, name, address are declared as VARCHAR(20) and the photoURL is declared VARCHAR(200).

I also have the DetailsView1(in addedit.apsx) which is described above and also a GridView1 which is on another page(DataDisplay.aspx). When I click on the HyperLink field of the GridView1, it brings me to the DetailsView(addedit.aspx) of that record. I implemented this using DataNavigateURLFormatString=addedit.aspx?EmployeeI D={0} in GridView1. In other words, DetailsView1 displays and edits data of the record that I clicked in GridView1.

Now, the problem is: Whatever I change in DetailsView, the data doesn't get updated in GridView. Neither is it updated in the SQL database. For example, if I change the name from John to Tom in DetailsView, GridView stills display John as the name. Same goes for the photoURL. I clicked on 'Browse' to select the file and then 'Update', the file gets copied to the '~/images/' directory but the data field of the PhotoURL in the SQL database is still null.

How do I get around this problem?

Thanks.
 
Old June 16th, 2008, 11:58 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

But where is the code that is supposed to update the database after you store the image.

If it's an Update statement, are you sure it gets the right ID so it knows what record to update?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old June 17th, 2008, 01:47 AM
Authorized User
 
Join Date: Apr 2008
Posts: 25
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Imar!

I thought the 'SqlDataSource' is suppose to provide the code to update the database. When I click on 'Update' of the DetailsView, doesn't it update the database? I'm scratching my head.

If I am wrong, could you kindly give me a pointer on how the code should be.

Thanks for helping out.

 
Old June 17th, 2008, 03:33 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, it's supposed to do that. However, it doesn't know about your action in the code behind right? How does it know you are changing the URL of the uploaded image?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old June 17th, 2008, 03:46 AM
Authorized User
 
Join Date: Apr 2008
Posts: 25
Thanks: 0
Thanked 1 Time in 1 Post
Default

So how should I go about putting the action of updating the database in code behind?

Is it in DetailsView1_ItemUpdated() function? What shall I put inside?

Thanks

 
Old June 17th, 2008, 04:10 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I may start to sound like a broken record, but that's pretty hard to tell without seeing your code. Why don't you post the *relevant* pieces of your code? (that is, don't post the entire page but just the parts that we're dealing with here).

Take a look at e.NewValues which is available in the DetailsView's ItemUpdating event.

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old June 17th, 2008, 04:15 AM
Authorized User
 
Join Date: Apr 2008
Posts: 25
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Imar, this is the code-behind for addedit.aspx

Code:
Partial Class Management_addedit
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.QueryString.Get("EmployeeID") IsNot Nothing Then
            DetailsView1.DefaultMode = DetailsViewMode.Edit
        End If
    End Sub

    Protected Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted
        EndEditing()
    End Sub

    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting
        'Dim myPicture As Picture = CType(e.NewObject, Picture)
        Dim FileUpload1 As FileUpload = CType(DetailsView1.FindControl("FileUpload1"), FileUpload)
        Dim virtualFolder As String = "~/images/"
        Dim physicalFolder As String = Server.MapPath(virtualFolder)
        Dim fileName As String = Guid.NewGuid().ToString()
        Dim extension As String = System.IO.Path.GetExtension(FileUpload1.FileName)

        FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension))

        'myPicture.ImageUrl = virtualFolder + fileName + extension
    End Sub

    Protected Sub DetailsView1_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles DetailsView1.ItemUpdated
        EndEditing()
    End Sub

    Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
        Dim FileUpload1 As FileUpload = CType(DetailsView1.FindControl("FileUpload1"), FileUpload)
        Dim virtualFolder As String = "~/images/"
        Dim physicalFolder As String = Server.MapPath(virtualFolder)
        Dim fileName As String = Guid.NewGuid().ToString()
        Dim extension As String = System.IO.Path.GetExtension(FileUpload1.FileName)

        FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension))

    End Sub

    Private Sub EndEditing()

        Response.Redirect("DataDisplay.aspx")
    End Sub

End Class
 
Old June 17th, 2008, 04:28 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

And did you look at e.NewValues in the Inserting or Updating event?

For an example and an explanation, take a look at page 419. Basically, you need to provide the control with a new value for the PictureUrl column or whatever you have called it.

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old June 17th, 2008, 04:38 AM
Authorized User
 
Join Date: Apr 2008
Posts: 25
Thanks: 0
Thanked 1 Time in 1 Post
Default

Am I right to say that I have to do:

Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventAr gs) _
Handles DetailsView1.ItemUpdating

e.NewValues["LastName"] = DetailsView1.LastName
e.NewValues["FirstName"] = DetailsView1.FirstName
e.NewValues["PhotoURL"] = DetailsView1.PhotoURL...
...
etc?

I am confused.

 
Old June 17th, 2008, 04:45 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Take another look at the page / chapter I referred to. It's all in there.

The DetailsView and SqlDataSource can figure most of the stuff out, so you don't need to do anything for most fields. However, you want to change the value for the PictureUrl column in a custom event handler. That's the column you need to feed to NewValues so it understands you have a URL that it needs to feed to the SqlDataSource.

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter File Types in ASP.NET File Upload ramuis78 ASP.NET 2.0 Basics 2 May 31st, 2007 10:50 AM
Whole Folder upload(Multi file Upload) ramasamy_rams XML 1 September 9th, 2005 12:43 PM
JSP file upload and delete file pandjie JSP Basics 0 January 29th, 2005 10:49 PM
Upload file hbcontract XML 2 November 6th, 2003 07:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.