 |
| ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics 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
|
|
|
|

August 15th, 2007, 10:18 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
DetailsView "Out of range" error when updating
Any clue on why I'm getting this error? I have a detailsview (picture attached). When I try editing any of the first 8 elements, I'm fine. When I try editing the 9th element on, I get the following error:
(Note: the DetailsView obiviously has more than 8 records. At the bottom, it has them indexed by 1 2 3 4 5 6 7 8 9 10 ...
I did debug and put a line: dim x as integer = e.newvalues.count and it was recording 7 (or 8--i forget). Why would the count only be 7 or 8 when there are obviously more than 10 records?)
Specified argument was out of the range of valid values.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index
Source Error:
Line 32: Dim myDropDownList As DropDownListLine 33: myDropDownList = CType(DetailsView1.FindControl("DropDownList1"), DropDownList)Line 34: e.NewValues.Insert(e.CommandArgument, "ptype", myDropDownList.SelectedValue)Line 35: Line 36: Dim myFileUpload As FileUpload
Source File: d:hostingsunsartAdmineditpictures.aspx. vb Line: 34
******
Here is my method that is being referenced in the error:
Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventAr gs) Handles DetailsView1.ItemUpdating
Dim myDropDownList As DropDownList
myDropDownList = CType(DetailsView1.FindControl("DropDownList1"), DropDownList)
e.NewValues.Insert(e.CommandArgument, "ptype", myDropDownList.SelectedValue)
Dim myFileUpload As FileUpload
Dim oldFileName As String = CType(DetailsView1.FindControl("Label3"), Label).Text
myFileUpload = CType(DetailsView1.FindControl("FileUpload1"), FileUpload)
If myFileUpload.HasFile Then
Try
File.Delete(MapPath("~/Paintings/") & oldFileName)
myFileUpload.SaveAs(Server.MapPath("~/Paintings/" & myFileUpload.FileName))
Dim url As String = myFileUpload.FileName
If (url = String.Empty) Then
url = oldFileName
End If
e.NewValues.Item("url") = myFileUpload.FileName
Catch ex As Exception
FileUploadReport.Text = "Failed because: <br/>" & ex.Message
End Try
End If
End Sub
Thanks,
Rob
|
|

August 15th, 2007, 10:48 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Not sure if it helps in resolving the error, but look at this:
dim x as integer = e.newvalues.count
The Count property of the NewValues collection has nothing to do with the number of records that the DetailsView is displaying. NewValues.Count refers to the number of fields / columns that your object has. So, a simple object with just an ID and a Description would give a Count of 2.
HtH,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|

August 15th, 2007, 10:59 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Ok...so...ultimately, what I am tryign to do is take the current item being edited and put the value of a selected dropdown into the value "ptype" for the currently selected record in the detailsview. I tried doing this via the DetailsView_ItemUpdating event as:
e.NewValues.Insert(e.CommandArgument, "ptype", myDropDownList.SelectedValue)
I thought that this would put the value into Ptype for the currently selected item. What's odd--is this works for the first 8 records--this line. Ironically enough..there are 8 fields in the DetailsView that are "editable" with the ptype being a dropdown in there--the 4th control in the edit view.
My logic was obviously wrong for editing...what would I need to change it to so that the current item being "updated" will have the selected value from the dropdown input into "ptype"--and..why did it work for the first 8 items the way I have it?
Thanks SO much!
Rob
|
|

August 15th, 2007, 11:05 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I am not sure, as I don't fully understand what your problem is. However, let's say you haver the following parameters in your datasource:
<UpdateParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Summary" Type="String" />
</UpdateParameters>
Then when the updating event fires, you can do something like this:
Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventAr gs) _
Handles DetailsView1.ItemUpdating
e.NewValues("Title") = "New Title Here"
End Sub
This sets or overwrites the value of the parameter called Title.
Does this help?
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|

August 15th, 2007, 11:09 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
I think so..I will try later and post results. I think my biggest problem was that I thought that e.Newvalues represented each record that was returned--not the fields of a record that is being updated. So, I thought that if there were 12 records in my detailsview, there would be 12 "newvalues"..so, I thought
e.NewValues.Insert(e.CommandArgument, "ptype", myDropDownList.SelectedValue)
Where e.commandArgument was the "selected record" (ie the 9th record) and that e.NewValues.Insert would insert the value of the selected dropdown and put it into "ptype" for the "9th" record)
Obviously, my logic was skewed...but not sure why this actually worked for the first 8 records (first 8 paintings) that were returned.
Weird.
Thanks SO much for the help!
Kind Regards,
Rob
|
|

August 15th, 2007, 11:15 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I don't know that either as I don't know what you did.
However, NewValues contains the new values for the current record being updated, giving you a chance to modify the current data before it gets sent to the data source. Remember, the DetailsView, while capable of displaying multiple, pageable records, only works with one record at the time.
IntelliSense for NewValues might have revealed that:
"Gets a dictionary that contains the new field name/value pairs for the record to update"
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|

August 15th, 2007, 11:23 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
While reading your original post again, I see what the problem is:
e.NewValues.Insert(e.CommandArgument, "ptype", myDropDownList.SelectedValue)
You were not assigning the existing parameters a new value, but you were creating a *new* parameter using the Insert method. Insert takes as its first argument a zero-based index of the collection. So:
e.NewValues.Insert(0, "ptype", "SomeValue")
would insert a new value at the very first position.
Once you got passed record 8, you tried to insert an item beyond the length of the collection (e.CommandArgument contains the zero-based index of the *record* in the DetailsView).
So all in all, it was bad luck it worked before. You used the record index to determine a property index. It worked while your record index was less than the number of properties / columns in your data source.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|

August 15th, 2007, 12:33 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Makes Sense...thank you VERY much!
Last question--so if it was creating a new parameter--would it matter what the size of the e.newvalues was? Or, does it replace the existing parameter with the "new" one.
For example. Let's say my parameters were:
fname = Rob
mname = John
lname = Searing
state = KS
and I do e.newvalues.insert(0, "lname", "Spaanjaars")
will that replace the "fname" parameter with "lname" (because fname is the 0-placed parameter--and I am performing an insert into the "0" position with a different parameter name "lname"
The reason I ask is because "ptype" was not the "0" position element in the record...the filename was...yet the filename didn't change with e.newvalues.insert(e.commandargment, "ptype", dropdownlist.selectedvalue)..in fact everything worked liked I wanted it to...even when I try editing the "second" record-which would have resulted in e.newvalues.insert(1, "ptype", value) or the third, which would be e.newvalues.insert(2, "ptype", value)...it always updated perfectly...not sure why.
Kind Regards,
Rob
|
|

August 15th, 2007, 05:06 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Ok..it worked. I am still curious about the behavior and why, what I describe above, worked. I actually debugged and even though I had
e.newvalues(e.commandargument, "ptype", dropdownlist1.selectedvalue) which translated to (1, "ptype", 20) where 20 is and id to a table value and 1 is the second field (0-based array) of the record which is not ptype---it still passed the right parameters into my object.
I used the same design as Imar did on the 3-layer article and so I was able to see what values were passed into my "paintings" object and each one was passed correctly. So regardless as to whether I picked (1, "ptype", 20) or (3, "ptype", 20) or any other number in the first parameter below 8 -- it worked fine.
I must assume that by "insert" all it did was reposition the "ptype" field in the parameters..once it got about the amount of parameters being passed--it errored.
Kind Regards,
Rob
|
|

August 15th, 2007, 05:18 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Nope, you can't reposition it. The NewValues collection is an OrderedDictionary that only allows unique keys. Once you add the same key again using Insert, you get an error.
I haven't seen the rest of your page so I can only guess, but my take is you're not supplying a value for ptype in the code before (e.g. through an editable control for example), so when ItemUpdating fires, the parameter is not listed in the NewValues collection yet, allowing your code to actually add it.
Just for fun, try setting one of the other parameters (if you have any) that are already set by the UI. For example:
e.NewValues.Insert(e.CommandArgument, "Title", "NewValue")
You'll see it crashes with an error stating that Title (or whatever column name you have) already exists.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|
 |