We have a webgrid that contains a date column. In some of the rows, the date is empty (DBNull). When we sort this column, we get the following error:
Object cannot be cast from DBNull to other types.
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.InvalidCastException: Object cannot be cast from DBNull to other types.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:
[InvalidCastException: Object cannot be cast from DBNull to other types.]
System.DBNull.System.IConvertible.ToDateTime(IFormatProvider provider) +54
System.Convert.ToDateTime(Object value, IFormatProvider provider) +31
Infragistics.WebUI.UltraWebGrid.UltraGridRow.MultiColCompare(UltraGridRow a, UltraGridRow b, ArrayList sortedCol, Int32 level) +4660
Infragistics.WebUI.UltraWebGrid.UltraGridRow.CompareTo(Object row) +313
Infragistics.WebUI.UltraWebGrid.RowsCollection.quickSort(Int32[ array, Int32 left, Int32 right) +91
Infragistics.WebUI.UltraWebGrid.RowsCollection.Sort(Boolean caseSensitive) +730
Infragistics.WebUI.UltraWebGrid.UltraWebGrid.SortGroupRows(RowsCollection rows) +150
Infragistics.WebUI.UltraWebGrid.UltraWebGrid.PerformGroupRows() +213
Infragistics.WebUI.UltraWebGrid.StateChangeProcessor.ProcessSortedColumns(StateChange stateChange, UltraGridColumn column) +3881
Infragistics.WebUI.UltraWebGrid.StateChangeProcessor.ProcessChanges() +2875
Infragistics.WebUI.UltraWebGrid.UltraWebGrid.ProcessChanges(StateChanges stateChanges, Boolean fireEvents) +247
Infragistics.WebUI.UltraWebGrid.UltraWebGrid.LoadPostData(String postDataKey, NameValueCollection values) +5648
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +718
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3776
I searched the old forum and found that someone else had a similar issue. We tried using a null instead of a DBNull and it did not work in our case. We also submitted a question to Infragistics support and have not received an answer yet.
Would someone have any other suggestion?
Thanks,
Annie
You can try something like this:
protected void uwgEmployee_InitializeLayout(object sender, Infragistics.WebUI.UltraWebGrid.LayoutEventArgs e){ e.Layout.Bands[0].Columns.FromKey("StartDate").DataType = "String"; }
protected void uwgEmployee_InitializeRow(object sender, Infragistics.WebUI.UltraWebGrid.RowEventArgs e){ if(e.Row.Cells.FromKey("StartDate").Value!=null) e.Row.Cells.FromKey("StartDate").Text = Convert.ToDateTime(e.Row.Cells.FromKey("StartDate").Value).ToShortDateString(); else e.Row.Cells.FromKey("StartDate").Text = "-";
}
Or you can use System.DateTime.MinDate instead of DBNull.
Hope it helps
Johni Ecco
Yotsuba said: You can try something like this: ... Or you can use System.DateTime.MinDate instead of DBNull.
...
That sort of thing shouldn't be necessary. I've seen grids that display dbNulls as empty cells. I'm pretty sure it has something to do with the WebGrid's property settings, but without the code...