This is a migrated thread and some comments may be shown as answers.

Stack overflow exception on DataBind()

3 Answers 599 Views
DataServiceDataSource
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 04 Jul 2016, 10:29 PM

We currently have an admin page that allows us to search for customers by email. My task is to also implement functionality that allows us to see all of a particular customer's orders for the past two weeks when we make that search. I modeled this data binding after the current data binding that exists, but it just causes a stack overflow exception, and I'm not sure what I'm doing wrong. Here's the markup:

 

<telerik:RadGrid ID="MailingsGrid" runat="server" AllowSorting="True" CellSpacing="0" GridLines="None" PageSize="50" OnItemDataBound="MailingsGrid_ItemDataBound">
    <MasterTableView Width="100%" NoMasterRecordsText="No users found" AutoGenerateColumns="false">
        <Columns>
            <telerik:GridBoundColumn DataField="Sub_Order_ID" HeaderText="Sub_Order_ID" UniqueName="Sub_Order_ID" ReadOnly="true" DataType="System.Int32" FilterControlAltText="Filter Sub_Order_ID column" SortExpression="Sub_Order_ID" />
                <telerik:GridBoundColumn DataField="Num_Pages" HeaderText="Num_Pages" UniqueName="Num_Pages" ReadOnly="true" DataType="System.Int32" FilterControlAltText="Filter Num_Pages column" SortExpression="Num_Pages" />
                    <telerik:GridTemplateColumn SortExpression="Address_1" HeaderText="Recipient" HeaderButtonType="TextButton" GroupByExpression="Address_1 Group by Address_1" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" ReadOnly="true" ItemStyle-Wrap="false">
                        <ItemTemplate><%#MailALetter.Common.FormatAddressTo(DataBinder.Eval(Container, "DataItem.Title"), DataBinder.Eval(Container, "DataItem.Name"), DataBinder.Eval(Container, "DataItem.JobTitle"), DataBinder.Eval(Container, "DataItem.Company"), DataBinder.Eval(Container, "DataItem.Address_1"), DataBinder.Eval(Container, "DataItem.Address_2"), DataBinder.Eval(Container, "DataItem.Address_3"),  DataBinder.Eval(Container, "DataItem.City"), DataBinder.Eval(Container, "DataItem.State"),  DataBinder.Eval(Container, "DataItem.Zip_Code_5"), DataBinder.Eval(Container, "DataItem.Zip_Code_4"),  DataBinder.Eval(Container, "DataItem.Country"))%>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn DataField="Date_Created" DataType="System.DateTime" FilterControlAltText="Filter Date_Created column" HeaderText="Date_Created" ReadOnly="True" SortExpression="Date_Created" UniqueName="Date_Created">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Date_Fullfilled" DataType="System.DateTime" FilterControlAltText="Filter Date_Fullfilled column" HeaderText="Date_Fullfilled" ReadOnly="True" SortExpression="Date_Fullfilled" UniqueName="Date_Fullfilled">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

And here's the code behind:

protected void SearchButton_Click(object sender, EventArgs e)
    {       
        string EmailSearchTerm = SearchText.Text;
 
        UserResults = DBContext.Users.Where(x => SqlMethods.Like(x.Email, "%" + EmailSearchTerm + "%")).ToList();
        MembershipResults = DBContext.Memberships.Where(x => UserResults.Select(y => y.UserId).Contains(x.UserId)).ToList();
        SearchResults.DataSource = UserResults;
        // This line is causing the overflow exception
        MailingsGrid.DataSource = DBContext.LetterReports.Where(x => x.UserID.Equals(MembershipResults[0].UserId)).ToList();
        SearchResults.Rebind();
        MailingsGrid.DataBind();
    }

protected void MailingsGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
 
            string SubOrderId = item.GetDataKeyValue("Sub_Order_ID").ToString();
            string DateCreated = item.GetDataKeyValue("Date_Created").ToString();
            string DateFullfilled = item.GetDataKeyValue("Date_Fulfilled").ToString();
            string Pages = item.GetDataKeyValue("Num_Pages").ToString();
        }
    }

Any help would be greatly appreciated, this is the latest of many iterations, and I'm kind of at a loss at this point.

3 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 07 Jul 2016, 10:24 AM
Hi Paul,

Please make sure that you are not using the DataBind() method to bind the grid. Performing complex grid operations such as Inserting, Deleting, Updating, Hierarchy relations, Grouping, Exporting, Paging, Sorting, Filtering, etc. require accommodating appropriate database operations.  Therefore, we suggest you to avoid Simple Databinding and strongly recommend the use of more advanced databinding methods, which automatically handle the aforementioned functions:

Declarative DataSource (DataSourceID property)
Programmatic Data Binding (NeedDataSource event, + DetailTableDataBind for hierarchy). You should set the DataSource property ONLY within these event handler.

For applying filter programmatically, please use the FireCommandEvent method as suggested in the following post:
http://www.telerik.com/forums/radgrid-enable-filter-code-behind#cel1slcp-U-pmV28CfXTrg

I hope this will prove helpful.


Regards,
Eyup
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Paul
Top achievements
Rank 1
answered on 07 Jul 2016, 11:56 PM

Here's my implementation of your recommendation, using the DataSourceID method. Now, the page won't even load without triggering a stack overflow exception. This is the table that works, the one that loads the user:

    <telerik:RadGrid ID="SearchResults" runat="server" PageSize="5" AllowSorting="True"
            AllowPaging="True" GridLines="Both" OnItemDataBound="SearchResults_ItemDataBound">
            <MasterTableView Width="100%" NoMasterRecordsText="No users found" DataKeyNames="UserId,Email"
                AutoGenerateColumns="false">
                <Columns>
                    <telerik:GridBoundColumn DataField="Email" HeaderText="User Email" UniqueName="Email"
                        ShowFilterIcon="true" HeaderStyle-Width="75px" ItemStyle-Width="155px" FilterControlWidth="150px"
                        ReadOnly="true" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
                    <telerik:GridTemplateColumn SortExpression="UserAddress_1" HeaderText="From Address"
                        HeaderButtonType="TextButton" GroupByExpression="UserAddress_1 Group by UserAddress_1"
                        HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" ReadOnly="true"
                        ItemStyle-Wrap="false">
                        <ItemTemplate>
                            <%#MailALetter.Common.FormatAddressFrom(DataBinder.Eval(Container, "DataItem.DisplayName"),DataBinder.Eval(Container, "DataItem.Company"),DataBinder.Eval(Container, "DataItem.Address1"), DataBinder.Eval(Container, "DataItem.Address2"), "",  DataBinder.Eval(Container, "DataItem.City"),  DataBinder.Eval(Container, "DataItem.State"),  DataBinder.Eval(Container, "DataItem.ZipCode"), DataBinder.Eval(Container, "DataItem.ZipCode4"),  DataBinder.Eval(Container, "DataItem.Country"))%>
                        </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Left" />
                    </telerik:GridTemplateColumn>
                    <telerik:GridHyperLinkColumn HeaderText="" UniqueName="Loginas" DataNavigateUrlFields="UserId"
                        Text="Log in as" DataNavigateUrlFormatString="/Login.aspx?UserId={0}" Target="_blank"
                        AllowFiltering="false" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
                    <telerik:GridNumericColumn HeaderText="MailBank Amount" DataField="CreditRemaining"
                        DataFormatString="{0:C2}" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
                    <telerik:GridBoundColumn DataField="StateofMailing" HeaderText="State of Mailing"
                        EmptyDataText="" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
                    <telerik:GridHyperLinkColumn HeaderText="Custom Envelope" DataNavigateUrlFields="CustomEnvelopePath"
                        DataNavigateUrlFormatString="/Envelope.aspx?UserId={0}" Target="_blank" HeaderStyle-HorizontalAlign="Left"
                        ItemStyle-HorizontalAlign="Left" />
                    <telerik:GridBoundColumn DataField="UserNotes" HeaderText="User Notes" HeaderStyle-HorizontalAlign="Left"
                        ItemStyle-HorizontalAlign="Left" />
                    <telerik:GridTemplateColumn>
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" Text="Unlock" NavigateUrl='<%# "/accountsadmin.aspx?command=unlock&UserId=" + DataBinder.Eval(Container, "DataItem.UserID").ToString() %>' Visible='<%#  DisplayLockout(DataBinder.Eval(Container, "DataItem.UserID"),"unlock") %>' />
                            <asp:HyperLink ID="HyperLink2" runat="server" Text="Lock" NavigateUrl='<%# "/accountsadmin.aspx?command=lock&UserId=" + DataBinder.Eval(Container, "DataItem.UserID").ToString() %>' Visible='<%#  DisplayLockout(DataBinder.Eval(Container, "DataItem.UserID"),"lock") %>' />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>                    <telerik:GridHyperLinkColumn DataNavigateUrlFields="UserID" DataNavigateUrlFormatString="/accountsadmin.aspx?command=details&UserId={0}"
                        HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" Text="Details" />
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

Here's the table that needs to take that user's email and show their orders. This table is part of a tab strip further down the page:

    <telerik:RadPageView ID="pvMailings" runat="server" TabIndex="3">
                <telerik:RadGrid ID="MailingsGrid" runat="server" AllowSorting="True" CellSpacing="0" DataSourceID="LinqDataSource1" GridLines="None">
                    <MasterTableView Width="100%" NoMasterRecordsText="No users found" AutoGenerateColumns="false" DataKeyNames="UserId" DataSourceID="LinqDataSource1">
                        <Columns>
                            <telerik:GridBoundColumn DataField="Sub_Order_ID" HeaderText="Sub_Order_ID" UniqueName="Sub_Order_ID" ReadOnly="true" />
                            <telerik:GridBoundColumn DataField="Num_Pages" HeaderText="Num_Pages" UniqueName="Num_Pages" ReadOnly="true" />
                            <telerik:GridTemplateColumn HeaderText="Recipient" HeaderButtonType="TextButton" GroupByExpression="Address_1 Group by Address_1" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" ReadOnly="true" ItemStyle-Wrap="false">
                                <ItemTemplate>
                                    <%#MailALetter.Common.FormatAddressTo(DataBinder.Eval(Container, "DataItem.Title"), DataBinder.Eval(Container, "DataItem.Name"), DataBinder.Eval(Container, "DataItem.JobTitle"), DataBinder.Eval(Container, "DataItem.Company"), DataBinder.Eval(Container, "DataItem.Address_1"), DataBinder.Eval(Container, "DataItem.Address_2"), DataBinder.Eval(Container, "DataItem.Address_3"),  DataBinder.Eval(Container, "DataItem.City"), DataBinder.Eval(Container, "DataItem.State"),  DataBinder.Eval(Container, "DataItem.Zip_Code_5"), DataBinder.Eval(Container, "DataItem.Zip_Code_4"),  DataBinder.Eval(Container, "DataItem.Country"))%>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridBoundColumn DataField="Date_Created" HeaderText="Date_Created" ReadOnly="True" SortExpression="Date_Created" UniqueName="Date_Created">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Date_Fullfilled" HeaderText="Date_Fullfilled" ReadOnly="True" UniqueName="Date_Fullfilled">
                            </telerik:GridBoundColumn>
                        </Columns>
                    </MasterTableView>
                </telerik:RadGrid> 
                <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="MailALetter.LINQDAL.LINQDALDataContext" EntityTypeName="" OrderBy="Date_Created desc" TableName="LetterReports">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="SearchResults" Name="Email" PropertyName="SelectedValue" Type="String"></asp:ControlParameter>
                    </SelectParameters>
                </asp:LinqDataSource>
            </telerik:RadPageView> 

The code behind:

    protected void SearchButton_Click(object sender, EventArgs e)
    {        
        string EmailSearchTerm = SearchText.Text;

        UserResults = DBContext.Users.Where(x => SqlMethods.Like(x.Email, "%" + EmailSearchTerm + "%")).ToList();
        MembershipResults = DBContext.Memberships.Where(x => UserResults.Select(y => y.UserId).Contains(x.UserId)).ToList();
        SearchResults.DataSource = UserResults;
        SearchResults.Rebind();
    }

    ...

    protected void SearchResults_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;

            LinkButton linkButton = item.FindControl("LockoutLink") as LinkButton;
            if (linkButton != null)
            {
                string email = item.GetDataKeyValue("Email").ToString().Trim().ToLowerInvariant();
                if (MembershipResults.First(x => x.LoweredEmail == email).IsLockedOut)
                {
                    linkButton.Text = "Unlock";
                    linkButton.CommandName = "unlock";
                    linkButton.CommandArgument = item.GetDataKeyValue("userid").ToString();
                    linkButton.Visible = false;
                    //   linkButton.OnClientClick = string.Format("if(!confirmLockout('{0}')) return false;", email);
                    //   linkButton.CommandArgument = email;
                }
            }
        }
    }

0
Eyup
Telerik team
answered on 12 Jul 2016, 07:43 AM
Hi Paul,

Does the grid load as expected if you remove the template columns? You'd let the grid handle the filtering and try replacing the logic within the SearchButton_Click event handler with FireCommandEvent as suggested in my previous reply. If the issue remains, please open a formal support thread and send us a sample runnable web site sample to demonstrate the problematic behavior for further investigation.

Regards,
Eyup
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
DataServiceDataSource
Asked by
Paul
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Paul
Top achievements
Rank 1
Share this question
or