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
 
I loose my selected item now when I do the sort, but that is perfectly fine with me. It is working good enough for the user that needs it, thanks guys!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top