Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error on Sorting Datagrid

Status
Not open for further replies.

dzirkelb

Programmer
Jan 2, 2007
14
US
I have a datagrid which displays data which a user can select an item to display more data based upon selection. I try to add sorting, but come up with an error. Here is my code and below that is my error:



Sub BindStatesGrid(ByVal strSortField)
Dim cmdSelect As SqlCommand

strConn = ConfigurationManager.AppSettings("conn")
conn = New SqlConnection(strConn)

Dim strState As String
Dim ssql As String

strState = txtSearch.Text

ssql = "SELECT RecordID, RepCompany, Mfg,"
ssql = ssql & " Case IA When 1 Then 'X' Else null End AS IA,"
ssql = ssql & " Case NE When 1 Then 'X' Else null End AS NE,"
ssql = ssql & " Case MN When 1 Then 'X' Else null End AS MN,"
ssql = ssql & " Case WI When 1 Then 'X' Else null End AS WI,"
ssql = ssql & " Case IL When 1 Then 'X' Else null End AS IL,"
ssql = ssql & " Case SD When 1 Then 'X' Else null End AS SD,"
ssql = ssql & " Case ND When 1 Then 'X' Else null End AS ND,"
ssql = ssql & " Case MO When 1 Then 'X' Else null End AS MO,"
ssql = ssql & " Case KS When 1 Then 'X' Else null End AS KS,"
ssql = ssql & " Case AR When 1 Then 'X' Else null End AS AR,"
ssql = ssql & " IA as boolIA, NE as boolNE, MN as boolMN, WI as boolWI, IL as boolIL, SD as boolSD, ND as boolND, MO as boolMO, KS as boolKS, AR as boolAR, Contact"
ssql = ssql & " FROM MfgRepList WHERE (" & strState & " = 1) ORDER BY " & strSortField & ""

cmdSelect = New SqlCommand(ssql, conn)
conn.Open()
dgrdStates.DataSource = cmdSelect.ExecuteReader
dgrdStates.DataBind()
conn.Close()

BindDetailsGrid(0)
BindTargetsGrid(0)

Sub dgrdDetailsTargets_ItemCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
Dim intRecordID As Integer

If IsNothing(dgrdStates.DataKeys(e.Item.ItemIndex)) Then
intRecordID = 0
Else
intRecordID = dgrdStates.DataKeys(e.Item.ItemIndex)
End If


dgrdStates.SelectedIndex = e.Item.ItemIndex
BindDetailsGrid(intRecordID)
BindTargetsGrid(intRecordID)
End Sub

<asp:DataGrid ID="dgrdStates" AllowSorting="true" OnSortCommand="dgrdStates_SortCommand" OnEditCommand="dgrdStates_EditCommand" OnUpdateCommand="dgrdStates_UpdateCommand" OnCancelCommand="dgrdStates_CancelCommand" BorderColor="black" DataKeyField="RecordID" OnItemCommand="dgrdDetailsTargets_ItemCommand" AutoGenerateColumns="false" CellPadding="5" ItemStyle-BorderColor="black" SelectedItemStyle-BackColor="White" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="RecordID" HeaderStyle-Font-Size="8pt" HeaderStyle-Font-Names="Verdana" HeaderStyle-BorderColor="black" ItemStyle-BorderColor="black">
<ItemTemplate>
<asp:LinkButton Text='<%#Container.DataItem("RecordID") %>' Font-Names="verdana" Font-Size="8pt" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="Rep Company" DataField="RepCompany" SortExpression="RepCompany" ItemStyle-Font-Names="verdana" ItemStyle-Font-Size="8pt" HeaderStyle-Font-Names="verdana" HeaderStyle-Font-Size="8pt" HeaderStyle-BorderColor="black" ItemStyle-BorderColor="black" />
</Columns>
</asp:DataGrid>




Server Error in '/' Application.
--------------------------------------------------------------------------------

Index was out of range. Must be non-negative and less than the size of the collection.
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: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:

Line 108:
Line 109: 'Response.Write(dgrdStates.DataKeys(e.Item.ItemIndex))
Line 110: If IsNothing(dgrdStates.DataKeys(e.Item.ItemIndex)) Then
Line 111: intRecordID = 0
Line 112: Else

Source File: D:\inetpub\WebECIS\MfgRepList-test.aspx Line: 110

Stack Trace:

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
System.Collections.ArrayList.get_Item(Int32 index) +2776637
System.Web.UI.WebControls.DataKeyCollection.get_Item(Int32 index) +9
ASP.mfgreplist_test_aspx.dgrdDetailsTargets_ItemCommand(Object s, DataGridCommandEventArgs e) in D:\inetpub\WebECIS\MfgRepList-test.aspx:110
System.Web.UI.WebControls.DataGrid.OnItemCommand(DataGridCommandEventArgs e) +105
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +77
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +117
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +115
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +163
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +174
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102





--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210
 
You probably want to attach a debugger here and see what e.Item.ItemIndex thinks it is and how many rows your grid has when it fails.

You probably also want to add some error handling to your application instead of just letting the whole mess go to the screen.

Brian Begy
BugSentry - Automatic error reporting
 
The error happens when I select the column name to sort. I can select the line and the data displays correctly, it breaks when I try to sort the column.

How do I go about attaching a debugger?
 
I don't know how to do that, add breakpoints. I think it is failing because I have two functions thinking e is something. It appears to be two different things. The sort column, and the item index selected...so, it will be an integer, and a string.

Any ideas on how I can do this debugger thing? I never have used it, I just write the page and go off whatever error .net provides to me.
 
You can set break points a few ways.. the easiest open your code behind page and click to the left (left margin) of the line of code you want to stop on. A red circle will show that you added a break point.
 
I believe it has e.item.itemindex set at -1 while checking the debugger functions...why is this?
 
Check the control lifecycle. Is it firing once when you activate the control and once on the column sort? You might want to handle the error so it doesn't barf and see if the event fires twice.

Just guessing here, since I don't really use datagrids much.

Brian Begy
BugSentry - Automatic error reporting
 
I'm not sure how to check the control lifecycle to see if it fires twice or once. As far as I know, it shouldn't be firing when I click the sort column, but it is firing off. It should only fire when I select an item in my collection to display more data, but it appears it is firing off when I try to sort also.
 
I would suggest reading some documentation on the data grid events then.

I'm not a big data grid user, so I don't know the details, but if it fires the event twice, once on sort and once on command, then you will need to detect this and back out of your processing routine in the former case.

Brian Begy
BugSentry - Automatic error reporting
 
I'm pretty sure when I fire the sort column command, it fires the item command as thats where it errors out at, which sucks. I've tried workarounds but don't really know of any off hand.
 
That's why I thought I'd have my little workaround, but it doesn't work. Sub dgrdDetailsTargets_ItemCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
Dim intRecordID As Integer

If dgrdStates.DataKeys(e.Item.ItemIndex) = -1 Then
intRecordID = 0
Else
intRecordID = dgrdStates.DataKeys(e.Item.ItemIndex)
End If


dgrdStates.SelectedIndex = e.Item.ItemIndex
BindDetailsGrid(intRecordID)
BindTargetsGrid(intRecordID)
End Sub

It breaks on If dgrdStates.DataKeys(e.Item.ItemIndex) = -1 Then

It sees the e.item.itemindex adn immediately breaks...It doesn't care if I am looking to see if it equals -1.
 
If the index is -1 why continue processing the code at all? Return and get out of there.

It will fire again in a second, right?



Brian Begy
BugSentry - Automatic error reporting
 
If dgrdStates.DataKeys(e.Item.ItemIndex) = -1 Then
Return
Else
intRecordID = dgrdStates.DataKeys(e.Item.ItemIndex)
dgrdStates.SelectedIndex = e.Item.ItemIndex
BindDetailsGrid(intRecordID)
BindTargetsGrid(intRecordID)
End If

Still breaks...is that what you were suggestign though?
 
I think he means:
Code:
  If [b]e.Item.ItemIndex = -1[/b] Then
            Return
        Else
            intRecordID = dgrdStates.DataKeys(e.Item.ItemIndex)
            dgrdStates.SelectedIndex = e.Item.ItemIndex
            BindDetailsGrid(intRecordID)
            BindTargetsGrid(intRecordID)
        End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top