I have a web grid which gets its data source from a user generated sql query. When they sort their results in a datagrid, it causes an error when they try to export the grid. The error is "Object reference not set to an instance of an object" when the webgrid tries to databind(); Why does this err out at this point? Does the webgrid loses its datasource when it is sorted? The original data bind works fine, it only errs out after a user has sorted the webgrid. I don't understand what casues this error. Please let me know any information you have on this.
Actually, it kind of worked. But now if I sort on one column, then sort on a different one, the error recurs. Maybe I am missing something. What do you mean by "re-add the columns"?
OK, I figured it out. or at least a work aound. Before re-building and re-binding the grid, I have to clear the bands(0).sortedCols collection. then the exception is not thrown...
like this...
If Me.ultraDriverInfo.Columns.Count > 0 Then Me.ultraDriverInfo.Bands(0).SortedColumns.Clear() end if
then do the grid resetting, re-add the columns and re-bind...
food for thought... I don't think I should have to do this, seems like something the grid reset should do for me, but hey, that's just me
forgot to paste in the datasource assignment. The code works fine as long as I don't sort any of the columns... It's pretty simple. A couple of combo boxes for filter critieria and a button to do the search. the way I reproduce the problem is do a search that yields more than a few records, click on any one of the column headers to sort it, then click the show button again to re-buld the data set and the exception occurs on the databind statement. Very reproduceable.
I think you may need to assign a datasource to your grid before you try to .DataBind() it.
here is the ASP.net code...
Me.ultraDriverInfo.DataSource = _ From dlh In dc.DriverLicenseHistDates _ Join dr In dc.Drivers On dr.DriverID Equals dlh.DriverID _ Join dht In dc.DriverLicenseHistTypes On dlh.DriverLicenseHistTypeID Equals dht.DriverLicenseHistTypeID _ Where dlh.ProcessedOn > StartDate _ Order By dlh.ProcessedDate Descending, dr.DriverCode Ascending, _ dlh.DriverLicenseHistTypeID, dlh.SequenceNo Ascending _ Select dr.DriverCode, dlh.DriverLicenseHistID, _ dlh.DriverID, dlh.Message, _ dlh.Note, dlh.ProcessedDate, _ dlh.ProcessedOn, dht.Description, _ dlh.DispDate, dlh.DriverLicenseHistTypeID, _ dlh.SequenceNo, dlh.Action, _ dlh.ActionIndicator, dlh.ConvRec, _ dlh.TicketNo, dlh.ViolationCode, dlh.ViolationDate, _ dlh.ViolationPoints, dlh.ViolationText
Dim i As Int16 Me.ultraDriverInfo.ResetRows() Me.ultraDriverInfo.Clear() If Not Me.ultraDriverInfo Is Nothing Then If Not Me.ultraDriverInfo.Rows Is Nothing Then i = Me.ultraDriverInfo.Rows.Count If i > 0 Then Me.ultraDriverInfo.Rows.Clear() End If End If End If Me.ultraDriverInfo.ResetColumns() Me.ultraDriverInfo.DisplayLayout.AllowColSizingDefault = Infragistics.WebUI.UltraWebGrid.AllowSizing.Free Me.ultraDriverInfo.DisplayLayout.AllowColumnMovingDefault = Infragistics.WebUI.UltraWebGrid.AllowColumnMoving.OnClient ' Me.ultraDriverInfo.DisplayLayout.ReadOnly = Infragistics.WebUI.UltraWebGrid.ReadOnly.LevelTwo ' Me.ultraDriverInfo.DisplayLayout.ViewType = Infragistics.WebUI.UltraWebGrid.ViewType.Hierarchical Me.ultraDriverInfo.DisplayLayout.NoDataMessage = "Sorry, bro, got notin for ye; trah-a-gin" Me.ultraDriverInfo.DisplayLayout.AutoGenerateColumns = False Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("Driver", "DriverCode", "string", 60)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("Retrieval Date", "ProcessedOn", "datetime", 80)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("Note", "note", "string", 300)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("Record Type", "Description", "string", 100, Infragistics.WebUI.UltraWebGrid.CellMultiline.No)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("Message", "msg", "string", 200)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("Seq.", "SequenceNo", "int16", 50)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("DispDate", "DispDate", "datetime", 50)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("Action", "Action", "string", 50)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("A.I.", "ActionIndicator", "string", 50)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("Ticket", "TicketNo", "string", 100)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("V. Code", "ViolationCode", "string", 50)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("V. Date", "ViolationDate", "datetime", 50)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("Pts.", "ViolationPoints", "string", 50)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("V. Text", "ViolationText", "string", 200)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("DriverID", "DriverID", "int16", 50)) Me.ultraDriverInfo.Columns.Add(UltraCreateColumn("DLHID", "DriverLicenseHistID", "int16", 50)) Me.ultraDriverInfo.DataBind()
Private Function UltraCreateColumn( _ ByVal sCaption As String, ByVal sCol As String, ByVal sType As String, ByVal iWidth As Int16, Optional ByVal bMultiline As Infragistics.WebUI.UltraWebGrid.CellMultiline = Infragistics.WebUI.UltraWebGrid.CellMultiline.Yes) _ As Infragistics.WebUI.UltraWebGrid.UltraGridColumn Dim wc As Infragistics.WebUI.UltraWebGrid.UltraGridColumn wc = New Infragistics.WebUI.UltraWebGrid.UltraGridColumn wc.Header.Caption = sCaption wc.BaseColumnName = sCol wc.DataType = sType If sType = "datetime" Then wc.Format = "MM/dd/yyyy" End If wc.Width = iWidth wc.Header.ClickAction = Infragistics.WebUI.UltraWebGrid.HeaderClickAction.SortMulti ' wc.CellMultiline = bMultiline UltraCreateColumn = wc End Function
markup
<igtbl:UltraWebGrid ID="ultraDriverInfo" runat="server" Height="400px" Width="100%"> <bands> <igtbl:UltraGridBand> <addnewrow view="NotSet" visible="NotSet"> </addnewrow> </igtbl:UltraGridBand> </bands> <displaylayout allowcolsizingdefault="Free" allowcolumnmovingdefault="OnServer" allowsortingdefault="OnClient" autogeneratecolumns="False" bordercollapsedefault="Separate" headerclickactiondefault="SortMulti" name="UltraWebGrid1" rowheightdefault="20px" rowselectorsdefault="No" selecttyperowdefault="Extended" stationarymargins="Header" stationarymarginsoutlookgroupby="True" tablelayout="Fixed" version="4.00" viewtype="OutlookGroupBy"> <framestyle backcolor="Window" bordercolor="InactiveCaption" borderstyle="Solid" borderwidth="1px" font-names="Microsoft Sans Serif" font-size="8.25pt" height="400px" width="100%" wrap="True"> </framestyle> <pager minimumpagesfordisplay="2"> <PagerStyle BackColor="LightGray" BorderStyle="Solid" BorderWidth="1px"> <borderdetails colorleft="White" colortop="White" widthleft="1px" widthtop="1px" /> </PagerStyle> </pager> <editcellstyledefault borderstyle="None" borderwidth="0px"> </editcellstyledefault> <footerstyledefault backcolor="LightGray" borderstyle="Solid" borderwidth="1px"> <borderdetails colorleft="White" colortop="White" widthleft="1px" widthtop="1px" /> </footerstyledefault> <headerstyledefault backcolor="LightGray" borderstyle="Solid" horizontalalign="Left"> <borderdetails colorleft="White" colortop="White" widthleft="1px" widthtop="1px" /> </headerstyledefault> <rowstyledefault backcolor="Window" bordercolor="Silver" borderstyle="Solid" borderwidth="1px" font-names="Microsoft Sans Serif" font-size="8.25pt"> <padding left="3px" /> <borderdetails colorleft="Window" colortop="Window" /> </rowstyledefault> <groupbyrowstyledefault backcolor="Control" bordercolor="Window"> </groupbyrowstyledefault> <groupbybox> <boxstyle backcolor="ActiveBorder" bordercolor="Window"> </boxstyle> </groupbybox> <addnewbox hidden="False"> <boxstyle backcolor="Window" bordercolor="InactiveCaption" borderstyle="Solid" borderwidth="1px"> <borderdetails colorleft="White" colortop="White" widthleft="1px" widthtop="1px" /> </boxstyle> </addnewbox> <activationobject bordercolor="" borderwidth=""> </activationobject> <filteroptionsdefault> <filterdropdownstyle backcolor="White" bordercolor="Silver" borderstyle="Solid" borderwidth="1px" customrules="overflow:auto;" font-names="Verdana,Arial,Helvetica,sans-serif" font-size="11px" height="300px" width="200px"> <padding left="2px" /> </filterdropdownstyle> <filterhighlightrowstyle backcolor="#151C55" forecolor="White"> </filterhighlightrowstyle> <filteroperanddropdownstyle backcolor="White" bordercolor="Silver" borderstyle="Solid" borderwidth="1px" customrules="overflow:auto;" font-names="Verdana,Arial,Helvetica,sans-serif" font-size="11px"> <padding left="2px" /> </filteroperanddropdownstyle> </filteroptionsdefault> </displaylayout> </igtbl:UltraWebGrid>
the exception:
[NullReferenceException: Object reference not set to an instance of an object.] Infragistics.WebUI.UltraWebGrid.RowsCollection.Sort() +797 Infragistics.WebUI.UltraWebGrid.UltraWebGrid.SortGroupRows(RowsCollection rows) +149 Infragistics.WebUI.UltraWebGrid.UltraWebGrid.PerformGroupRows() +213 Infragistics.WebUI.UltraWebGrid.DBBinding.BindList(IEnumerable datasource) +2010 Infragistics.WebUI.UltraWebGrid.DBBinding.DataBind(Object dataSource, String dataMember) +1317 Infragistics.WebUI.UltraWebGrid.UltraWebGrid.DataBind() +826 Driver_License_Info.WebForm1.cmdShow_Click(Object sender, EventArgs e) in C:\Projects\PetroExpress\Driver License Info\Driver License Info\Displayinfo.aspx.vb:178