Jump to content

ASP.NET Razor WebGrid - Column Links


kevin.upshaw

Recommended Posts

Hi, I wonder if someone can help. I am trying within a WebGrid to create a link on the (using the HTML anchor tag) Asset Tag column - so then when the user clicks on an asset tag it directs them to a Display Report page - where it displays a transaction report on that particular asset. I have searched and searched the internet, and found the solution below. However it displays the following error message: Server Error in '/' Application. Object reference not set to an instance of an object.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.NullReferenceException: Object reference not set to an instance of an object. 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. Here is my code: var db = Database.Open("assetManagement"); var sqlExtractEventDescription = "SELECT * FROM [event]";}<h2>Devices</h2>@{if(addNewOutput.IsEmpty() == false){<p class="informationMessage">@addNewOutput</p><br />}}<a href="addNew.cshtml?type=device" class="button">Add New</a><a href="editExisting.cshtml?type=device" class="button">Edit Existing</a>@{var sql = "SELECT assetTag, imei, serialNumber, eventId FROM [device] ORDER BY assetTag";var data = db.Query(sql);var grid = new WebGrid(data, canPage: true, rowsPerPage: 10);grid.Pager(WebGridPagerModes.NextPrevious);}<div id="webGridDiv">@grid.GetHtml( tableStyle: "userWebGrid", htmlAttributes: new { id = "deviceDataTable" }, headerStyle: "header", alternatingRowStyle: "alt", columns: grid.Columns( grid.Column("assetTag", "Asset Tag", format: @<text><a class="button" href="@Href("~/reports/displayReport.cshtml?type=assetTag&assetTag=" + assetTag")">@assetTag</a></text>), grid.Column("imei", "IMEI"), grid.Column("serialNumber", "Serial Number"), grid.Column("eventId", "Event ID"), grid.Column("", "Edit"))) </div> If someone could perhaps point me in the right direction as to where I am going wrong that would be great. Thanks. Kevin

  • Like 1
Link to comment
Share on other sites

One of the variables you're trying to use as an object is undefined or not set to an object. That would include things like addNewOutput, db, grid, etc. You'll need to check each of those to make sure they're defined correctly.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...