Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am unable to load a webgrid dynamically as the source bound to it is null. the datasource gets loaded only after selecting the dropdownlist item, then on selection change it calls an ActionResult through an Ajax call, then binds the html to the Webgrid. But before it runs the project, it throws an exception like "Object reference not set to an instance of an object." . because the datasource I have assigned is null and loads only after the selection of dropdownlist.


I have tried by putting the If statement for checking null, for the webgrid Div, but after loading the values also, its not showing the grid.If checking is not working.

Note: I am binding to the grid by Ajax call when the item gets selected from the dropdown by Ajax call as

success: function (result) {
$("#GridTable").html(result);
},


Any Solution for this?
Posted
Comments
F-ES Sitecore 26-Nov-15 5:09am    
The solution is to not reference properties of null variables, so check they are not null before accessing properties. Without seeing the code it's hard to give more specific advice.
Sireesha E 26-Nov-15 5:27am    
@Html.DropDownListFor(r => r.servers, new SelectList(Enumerable.Empty<SelectListItem>(), "ServerID", "ServerName", Model.SelectedServerID), "Select", new { @id = "ServersList", @onchange = "GetServices()" })

@{
var grid = new WebGrid(Model.SelectedServer.Services, rowsPerPage: 5, ajaxUpdateContainerId: "gridcontent");
}
<div id="gridcontent">

@grid.GetHtml(tableStyle: "webGrid",
headerStyle: "header",
alternatingRowStyle: "alt",
selectedRowStyle: "select",
columns: grid.Columns(grid.Column("ServiceName", "DisplayName"),
grid.Column("Status", "DisplayName"),


Here, in Webgrid Source is null when the page loads, After selecting a value from Dropdownlist, Model.SelectedServer.Services loads.
So how to get rid of this null exception while page loads
F-ES Sitecore 26-Nov-15 5:44am    
You need to work out what is null, what is causing the null reference. Is it Model.SelectedServer? If so wrap the "var grid=" code and the @grid.getHtml code in an if statement;

@if (Model.SelectedServer != null)
{
.... your code here
}
Sireesha E 26-Nov-15 6:27am    
Model.SelectedServer.Services is null, It will load only after the selecting the item from dropdown.

I have tried with If statement like @If(Model.SelectedServer.Services!=null){}

But after selecting dropdown, I am calling a jQuery function (from which onSuccess, It will get result from an action method), its not applied to the Webgrid

success: function (result) {
$("#GridTable").html(result);
},

result is in the form of list (returning JsonResult from controller).
F-ES Sitecore 26-Nov-15 6:41am    
You might have to set the webgrid to a dummy data source, or a null source, so that the table is still rendered as html so that your jquery function can then update it. I've never used this webgrid so I'm not familiar with its ins and outs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900