Solved: Telerik RadGrid doesn’t retain data after a postback

You may come across this issue when you try to sort, filter or group your table that is built with Telerik RadGrid component. Each of these sorting, filtering, and grouping features trigger a postback. You may not see the data in your table after the postback is completed.

Solution

Make sure you define OnNeedDataSource attribute for your RadGrid object and handle NeedDataSource event. Telerik uses NeedDataSource event each time RadGrid is need to be bound with a data source.

ASPX file:

<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" CellSpacing="0"
GridLines="None" OnNeedDataSource="RadGrid1_NeedDataSource1" AllowPaging="true" PageSize="10">
     <MasterTableView AutoGenerateColumns="true" DataKeyNames="CustomerID">
     </MasterTableView>
</telerik:RadGrid>

Code-behind file:

protected void RadGrid1_NeedDataSource1(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
      RadGrid1.DataSource = GetDataTable("SELECT CustomerID, CompanyName, ContactName FROM Customers");
}

References:

How to convert a long timestamp to a short date in Oracle?

Many developers agree on working with dates is more challenging than working with other data types. I recently had to convert some long timestamp strings to short date values. Here are the functions I used:

Original value: 09-MAY-16 10.11.50.596605 AM -04:00
Target value: 09/05/2016

Code:

TO_CHAR(TO_TIMESTAMP_TZ(V_DATE_TIME, 'DD-MON-RR HH:MI:SS.FF AM TZH:TZM'), 'DD/MM/YYYY')

Original value: 09-MAY-16 10.11.50.596605 AM -04:00
Target value: 09/05/2016 10:11 AM

Code:

TO_CHAR(TO_TIMESTAMP_TZ(V_DATE_TIME, 'DD-MON-RR HH:MI:SS.FF AM TZH:TZM'), 'DD/MM/YYYY HH:MI AM')

As you see in the examples above, I first convert the string value in V_DATE_TIME to timestamp data type by using TO_TIMESTAMP_TZ. Then I convert timestamp data type back to string in the new format by using TO_CHAR function.

Solved: “ORA-01830: date format picture ends before converting entire input string” and “ORA-01821: date format not recognized”

If you store timestamps as string values (varchar2) in your Oracle database, you will need convert them to date or timestamp values in order to use sorting based dates and times.

Let’s say, you have this value in your table: 27-AUG-17 08.27.57.113444 PM -04:00. Data type of the column is VARCHAR2. If you use this column in your ORDER BY statement, your query will return a table sorted based on string comparison. In this case, 27-AUG-17 will be bigger than 26-AUG-18 which is incorrect because the year 2018 is bigger than 2017.

While trying to convert these strings values to dates, you may come across these errors:

ORA-01830: date format picture ends before converting entire input string
ORA-01821: date format not recognized

Solution

Use the functions below to convert your strings to dates or timestamps.

Original value: 27-AUG-17 08.27.57.113444 PM -04:00

TO_DATE(SUBSTR(V_DATE_TIME,1,15), 'DD-MON-YYYY HH:MI')

Result: 27-AUG-17 08:27

TO_DATE(SUBSTR(V_DATE_TIME,1, 10), 'DD-MON-YYYY')

Result: 27-AUG-17 00:00

TO_TIMESTAMP_TZ(V_DATE_TIME, 'DD-MON-RR HH.MI.SS.FF AM TZH:TZM')

Result: 27-AUG-2017 08.27.57.113444000 PM -04:00