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

how to dynamically sort a radgrid columl's data

1 Answer 98 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mohamed
Top achievements
Rank 1
Mohamed asked on 09 Feb 2013, 04:47 AM
i need to know how to dynamically sort the column's data.i just placed a radgrid and dynamically binded through the following...


con = new SqlConnection("Data Source=ServerName;Initial Catalog=Databasename;User ID=sa;Password=*******");
        con.Open();
        cmd = new SqlCommand("select * from TableName where Department ='FieldName'", con);
        reader = cmd.ExecuteReader();
        RadGrid1.DataSource = reader;
        RadGrid1.DataBind();
        con.Close();

and i aslo enable the radgrid properties such as sorting , paging, auto generate columns, at the design time itself ...when i click on the column header at runtime .. it shows error like below..

Server Error in '/' Application.

Invalid attempt to call FieldCount when reader is closed.

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.InvalidOperationException: Invalid attempt to call FieldCount when reader is closed.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 

[InvalidOperationException: Invalid attempt to call FieldCount when reader is closed.]
   System.Data.SqlClient.SqlDataReader.get_FieldCount() +5033848
   System.Data.Common.DbEnumerator.BuildSchemaInfo() +19
   System.Data.Common.DbEnumerator.MoveNext() +156
   Telerik.Web.UI.GridResolveEnumerable.GetCollectionItemType(Boolean noItemsInEnumerator, Type& collectionItemType, Object& collectionFirstObject) +337
   Telerik.Web.UI.GridResolveEnumerable.ParseProperties() +90
   Telerik.Web.UI.GridResolveEnumerable.Initialize() +13
   Telerik.Web.UI.GridResolveEnumerable.EnsureInitialized() +24
   Telerik.Web.UI.GridEnumerableFromDataView..ctor(GridTableView owner, IEnumerable enumerable, Boolean CaseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields, Boolean enableSplitHeaderText) +208
   Telerik.Web.UI.GridDataSourceHelper.CreateGridEnumerable(GridTableView owner, IEnumerable enumerable, Boolean caseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields, Boolean enableSplitHeaderText) +129
   Telerik.Web.UI.GridDataSourceHelper.GetResolvedDataSource(GridTableView owner, Object dataSource, String dataMember, Boolean caseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields, Boolean enableSplitHeaderText) +404
   Telerik.Web.UI.GridTableView.get_ResolvedDataSource() +164
   Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +33
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +66
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +33
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
   Telerik.Web.UI.GridTableView.PerformSelect() +16
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
   Telerik.Web.UI.GridTableView.DataBind() +272
   Telerik.Web.UI.GridPageChangedEventArgs.ExecuteCommand(Object source) +1299
   Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +136
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +38
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +87
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

pls reply me quick...i i'm in urgent

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 11 Feb 2013, 10:56 AM
Hi,

The most appropriate place to close the DataReader and the connection is in the DataBound event handler of the grid, as shown below. Please take a look into the sample code snippet I treid.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowFilteringByColumn="true" AllowSorting="true"  CellSpacing="0"
    GridLines="None" PageSize="10">
    <MasterTableView AutoGenerateColumns="true" DataKeyNames="CustomerID">
    </MasterTableView>
</telerik:RadGrid>

C#:
SqlDataReader reader;
SqlConnection conn;
 
protected void RadGrid1_NeedDataSource1(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = ReadRecords("SELECT CustomerID, CompanyName, ContactName FROM Customers");
}
 
private SqlDataReader ReadRecords(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
    conn = new SqlConnection(ConnString);
    conn.Open();
 
    SqlCommand cmd = new SqlCommand(query, conn);
    reader = cmd.ExecuteReader();
 
    return reader;
}
 
protected void RadGrid1_DataBound(object sender, System.EventArgs e)
{
    reader.Close();
    conn.Close();
}

Please check this documentation for more details.

Thanks,
Shinu.
Tags
Grid
Asked by
Mohamed
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or