Thursday, March 29, 2012

Paging: Index was outside the bounds of the array

I enable paging for a datagrid in a popup window. The first page works but whenever I clicked on the next link, the following error occurs:

Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error:
Line 30: myConnection.Open()
Line 31: myDataSet = New Dataset()
Line 32: myDataAdapter.Fill(myDataSet, "Lead") *** Error
Line 33: myConnection.Close()
Line 34:

I did the same thing in the parent window and it work without any fuss

Below is my code. Sorry if it is messy. I still a newbie

<%@dotnet.itags.org. Page Language="VB" debug="true" %>
<%@dotnet.itags.org. Import Namespace = "System.Data" %>
<%@dotnet.itags.org. Import Namespace = "MySql.Data.MySqlClient" %>

<script language="VB" runat="server">

Dim connectString As String

Sub Page_Load(sender As Object, e As EventArgs)

If Not (IsPostBack)
connectString = CStr(session("findLead"))
END IF

MyDataGrid.PagerStyle.Mode = PagerMode.NextPrev
SearchLead()

End Sub


Function CreateSearchLead() As ICollection

Dim myConnection As MySqlConnection
Dim myDataAdapter As MySqlDataAdapter
Dim myDataSet As DataSet


myConnection = New MySqlConnection(";") 'I remove the string


myDataAdapter = New MySqlDataAdapter(connectString, myConnection)
myConnection.Open()
myDataSet = New Dataset()
myDataAdapter.Fill(myDataSet, "Lead")
myConnection.Close()

CreateSearchLead = myDataSet.Tables("Lead").DefaultView

End Function


Sub SearchLead()
MyDataGrid.DataSource = CreateSearchLead()
MyDataGrid.DataBind
End Sub


Sub MyDataGrid_Page(sender As Object, e As DataGridPageChangedEventArgs)
MyDataGrid.CurrentPageIndex = e.NewPageIndex
SearchLead()
End Sub


Sub MyDataGrid_ItemCommand(Sender As Object, E As DataGridCommandEventArgs)

'Dim Title As String = MyDataGrid.DataKeys(E.Item.ItemIndex)
Dim MyGridButton As LinkButton = E.CommandSource
Dim strURL As String

Select (MyGridButton.CommandName)

Case "Company" :
strURL = "CompanySummary.aspx?company=" & MyGridButton.Text

Case "Name" :
strURL = "LeadSummary.aspx?lead=" & MyGridButton.Text

End Select

session("findLead") = ""

Dim strj As String = "<script language=""javascript"">"
strj = strj & "opener.document.location = '" & strURL & "';window.close();"
strj = strj & "<" & "/script" & ">"
Literal1.Text = strj

' Place the script onto the page...

Page.ClientScript.RegisterStartupScript(GetType(Page),"RedirectScript", strj.ToString())

End Sub


Sub CloseBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

session("findLead") = ""
Response.Write("<script>self.close();" & "<" & "/script>")

End Sub

</script>


<html>
<head>
<title>Popup Lead Search</title>
</head>
<body>

<form id="searchResult" runat="server">

<table cellpadding=10 style="font: 10pt verdana">

<ASP:DataGrid id="MyDataGrid" OnItemCommand="MyDataGrid_ItemCommand" DataKeyField="Name" runat="server"
AllowPaging="True"
PageSize="3"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
PagerStyle-NextPageText="Next"
PagerStyle-PrevPageText="Prev"
OnPageIndexChanged="MyDataGrid_Page"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="false"
AlternatingItemStyle-BackColor="#eeeeee"
>

<Columns>

<asp:TemplateColumn HeaderText="Name">

<ItemTemplate>
<asp:linkbutton Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' CommandName="Name" style="color:darkred" runat="server"/>
</ItemTemplate>

</asp:TemplateColumn>


<asp:TemplateColumn HeaderText="Company">

<ItemTemplate>
<asp:linkbutton Text = '<%# DataBinder.Eval(Container.DataItem, "CompanyName")%>' CommandName="Company" runat="server"/>
</ItemTemplate>

</asp:TemplateColumn>


<asp:TemplateColumn HeaderText="Contact Number">

<ItemTemplate>
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "OfficePhone") %>' style="color:darkred" runat="server"/>
</ItemTemplate>

</asp:TemplateColumn>


<asp:TemplateColumn HeaderText="Date">

<ItemTemplate>
<asp:Label Text = '<%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "CreationDate")).ToString("dd/MM/yyyy") %>' runat="server"/>
</ItemTemplate>

</asp:TemplateColumn>

</Columns>

</ASP:DataGrid>


<p>
<asp:button OnClick="CloseBtn_Click" text="Close" runat=server/>
<p>
</form>
</body>
</html>

Any ideas what is wrong?

Thank you

Take a look at the following article:

http://gridviewguy.com/ArticleDetails.aspx?articleID=67

0 comments:

Post a Comment