CodeSmith Community
Your Code. Your Way. Faster!

gridview from custom store procedure breaks after postback

Latest post 04-28-2008 9:48 AM by vjerko. 5 replies.
  • 04-27-2008 1:53 PM

    • vjerko
    • Top 500 Contributor
    • Joined on 04-27-2008
    • Posts 10
    • Points 80

    gridview from custom store procedure breaks after postback

    Hi everyone,

    I got a problem, finally after all other problem i had get my grid and code to somehow support paging from custom store procedure. Now it looks that grid works with paging but after i do any postback it breaks Actually, it looks that it doesn't connect or fetch data - it is empty. Now my GridView1_SelectedIndexChanged fires, but GridView1.DataKeys.Count is 0? After i change pageindex it is also empty?

    could anyone help?

    Vjeran

    Custom method - works:

    public override TList<Cmstabs> GetByCategoryGroupPaged(TransactionManager transactionManager, int start, int pageLength , System.Int32? categoryId, System.Int32? groupId, System.Int32? pageIndex, System.Int32? pageSize, ref System.Int32? totalRowCount)
            {
                SqlDatabase database = new SqlDatabase(this._connectionString);
                DbCommand commandWrapper = database.GetStoredProcCommand("dbo.cust_cmstabs_GetByCategoryGroupPaged");
                
                database.AddInParameter(commandWrapper, "@CategoryId", DbType.Int32,  categoryId );
                database.AddInParameter(commandWrapper, "@GroupId", DbType.Int32,  groupId );
                database.AddInParameter(commandWrapper, "@PageIndex", DbType.Int32,  pageIndex );
                database.AddInParameter(commandWrapper, "@PageSize", DbType.Int32,  pageSize );
        
                database.AddParameter(commandWrapper, "@TotalRowCount", DbType.Int32, 4, ParameterDirection.InputOutput, true, 10, 0, string.Empty, DataRowVersion.Current, totalRowCount);
                
                IDataReader reader = null;
                
                //Create Collection
                    adriatica.Entities.TList<Cmstabs> rows = new adriatica.Entities.TList<Cmstabs>();
                    //Provider Data Requesting Command Event
                    OnDataRequesting(new CommandEventArgs(commandWrapper, "GetByCategoryGroupPaged", rows));
        
                    if (transactionManager != null)
                    {    
                        reader = Utility.ExecuteReader(transactionManager, commandWrapper);
                    }
                    else
                    {
                        reader = Utility.ExecuteReader(database, commandWrapper);
                    }    
                    
                    try
                    {    
                        Fill(reader, rows, start, pageLength);
                    }
                    finally
                    {
                        if (reader != null)
                            reader.Close();
                    }
                    
                    //Provider Data Requested Command Event
                    OnDataRequested(new CommandEventArgs(commandWrapper, "GetByCategoryGroupPaged", rows));

                totalRowCount =  Utility.GetParameterValue<System.Int32?>(commandWrapper.Parameters["@TotalRowCount"]);

                    return rows;
            }

     

    <asp:GridView ID="GridView1" runat="server"
    AutoGenerateColumns="false"
    OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
    DataSourceID="CmsTabsDataSource"
    AllowPaging="true"
    EnableSortingAndPagingCallbacks="false"
    EmptyDataText="No records"
    AllowSorting="false"
    PageSize="10"
    PageIndex="0"
    DataKeyNames="Id">
    <Columns>
        <asp:CommandField ShowSelectButton="True" ShowEditButton="True" />                
        <asp:BoundField DataField="Id" HeaderText="Redosljed" SortExpression="tabsorder" />
        <asp:BoundField DataField="Name" HeaderText="Naziv" SortExpression="name"  />
        <data:HyperLinkField HeaderText="Tip stanice"
        DataNavigateUrlFormatString="CmsTabtypesEdit.aspx?Id={0}"
        DataNavigateUrlFields="Id" DataContainer="TabtypeSource" DataTextField="Name" />
        <asp:BoundField DataField="Link" HeaderText="Link" SortExpression="link"  />
        </Columns>
    </asp:GridView>
            <br />
            <asp:Button runat="server" ID="btnCmsTabs" OnClientClick="BLOCKED SCRIPTlocation.href='CmsTabsEdit.aspx'; return false;" Text="Add New"></asp:Button>
            <data:CmsTabsDataSource ID="CmsTabsDataSource" runat="server"
                SelectMethod="GetByCategoryGroupPaged"
                EnablePaging="True"
                EnableSorting="True"
                EnableDeepLoad="True"
                CustomMethodRecordCountParamName="TotalRowCount"
                >
                <DeepLoadProperties Method="IncludeChildren" Recursive="False">
                    <Types>
                        <data:CmsTabsProperty Name="CmsTabtypes" />
                    </Types>
                </DeepLoadProperties>
                <Parameters>
                    <asp:ControlParameter Name="CategoryId" ControlID="kategorije" PropertyName="SelectedValue" Type="Int32" />
                    <asp:ControlParameter Name="GroupId" ControlID="ddlgrupe" PropertyName="SelectedValue" Type="Int32" />
                    <asp:ControlParameter Name="PageIndex" ControlID="GridView1" PropertyName="PageIndex" Type="Int32" />
                    <asp:ControlParameter Name="PageSize" ControlID="GridView1" PropertyName="PageSize" Type="Int32" />
                    <data:CustomParameter Name="RecordCount" Value="0" Type="Int32" />
                </Parameters>
            </data:CmsTabsDataSource>

    • Post Points: 5
  • 04-28-2008 6:48 AM In reply to

    • vjerko
    • Top 500 Contributor
    • Joined on 04-27-2008
    • Posts 10
    • Points 80

    Re: gridview from custom store procedure breaks after postback

     Just one note.. select is working.. only paging doesn't. I had checked. ObjectDatasource is correctly calling sp, which returns ok data. I don't understand why can't i bind grid to this data.

    • Post Points: 5
  • 04-28-2008 7:57 AM In reply to

    • vjerko
    • Top 500 Contributor
    • Joined on 04-27-2008
    • Posts 10
    • Points 80

    Re: gridview from custom store procedure breaks after postback

     It looks that this method is causing problem:

            public static adriatica.Entities.TList<Cmstabs> Fill(IDataReader reader, adriatica.Entities.TList<Cmstabs> rows, int start, int pageLength)
            {
            // advance to the starting row
                for (int i = 0; i < start; i++)
                {
                    if (!reader.Read())
                        return rows; // not enough rows, just return
                }
            for (int i = 0; i < pageLength; i++)
            {
                if (!reader.Read())
                break; // we are done
                string key = null;
               
                adriatica.Entities.Cmstabs c = null;
                if (DataRepository.Provider.UseEntityFactory)
                {
                key = new System.Text.StringBuilder("Cmstabs")
                .Append("|").Append((reader.IsDBNull(((int)adriatica.Entities.CmstabsColumn.Id - 1))?(int)0:(System.Int32)reader[((int)adriatica.Entities.CmstabsColumn.Id - 1)]).ToString()).ToString();
                c = EntityManager.LocateOrCreate<Cmstabs>(
                key.ToString(), // EntityTrackingKey
                "Cmstabs",  //Creational Type
                DataRepository.Provider.EntityCreationalFactoryType,  //Factory used to create entity
                DataRepository.Provider.EnableEntityTracking); // Track this entity?
                }
                else
                {
                c = new adriatica.Entities.Cmstabs();
                }
               
                if (!DataRepository.Provider.EnableEntityTracking ||
                c.EntityState == EntityState.Added ||
                (DataRepository.Provider.EnableEntityTracking &&
                ((DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
                (DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.DiscardChanges && (c.EntityState == EntityState.Unchanged ||
                                    c.EntityState == EntityState.Changed)))))
                            {
                c.SuppressEntityEvents = true;
                c.Id = (System.Int32)reader["id"];
                c.Tabsorder = (System.Int32)reader["tabsorder"];
                c.Name = (System.String)reader["name"];
                c.Tabtype = (System.Int32)reader["tabtype"];
                c.Link = reader.IsDBNull(reader.GetOrdinal("link")) ? null : (System.String)reader["link"];
                c.Commentsclosed = (System.Boolean)reader["commentsclosed"];
                c.Foldername = reader.IsDBNull(reader.GetOrdinal("foldername")) ? null : (System.String)reader["foldername"];
                c.EntityTrackingKey = key;
                c.AcceptChanges();
                c.SuppressEntityEvents = false;
                }
                rows.Add(c);
            }
            return rows;
            }        

    So if i got 2 records it will go to return empty tabs collection - so this is the reason why i don't have grid full with data. 

    • Post Points: 5
  • 04-28-2008 8:05 AM In reply to

    • vjerko
    • Top 500 Contributor
    • Joined on 04-27-2008
    • Posts 10
    • Points 80

    Re: gridview from custom store procedure breaks after postback

     I think logic here should be to support paging. Now we only have skipping to target page and then fetching data. Is there any suggestions how i could use this or i msut build my own datasource control and override existing methods for this cases?

     

    • Post Points: 5
  • 04-28-2008 8:08 AM In reply to

    • vjerko
    • Top 500 Contributor
    • Joined on 04-27-2008
    • Posts 10
    • Points 80

    Re: gridview from custom store procedure breaks after postback

    AHa.. all we need to do is to change parameter of starting index in paging event of grid, right? :)

    Filed under: ,
    • Post Points: 5
  • 04-28-2008 9:48 AM In reply to

    • vjerko
    • Top 500 Contributor
    • Joined on 04-27-2008
    • Posts 10
    • Points 80

    Re: gridview from custom store procedure breaks after postback

    Reply |Contact |Answer

     OK my workaround is to put 0 in the method in custom store procedure provider base, in my case public override TList<Cmstabs> GetByCategoryGroupPaged(TransactionManager transactionManager, int start, int pageLength , System.Int32? categoryId, System.Int32? groupId, System.Int32? pageIndex, System.Int32? pageSize, ref System.Int32? totalRowCount)

    in call: Fill(reader, rows, 0, pageLength); and custom store proc paging works without effect on other code. Party!!!

    • Post Points: 5
Page 1 of 1 (6 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems