0

I have a grid being populated by SQL data. In my dataBound method I have a numerical condition that evaluates the text of particular cells, for example: if myCell.Text <90

It is possible for the cell to have NULL values, so I want to check before running these conditions to ensure that the cell is not null. However, the actual text in the cell is evaluated as "nbsp;" (removed the & so it will appear in this post). I don't want to hardcode something like NOT myCell.Text.Contains("nbsp") because that just feels sloppy and problematic.

If TypeOf e.Item Is GridDataItem Then
    Dim dataItem As GridDataItem = e.Item
    Dim myCell As TableCell = dataItem("Status")
    If Not String.IsNullOrWhiteSpace(myCell.Text) Then

This is my current code and obviously the string " " passes this condition.

How do I properly check this value? Is there a different property I can evaluate?

Dale K
  • 25,246
  • 15
  • 42
  • 71
SethD02
  • 5
  • 4
  • I'm confused: is the value `null` or `" "` or the single NBSP character `" "`? Or is it actually `DBNull.Value` which is what you would get if using a `DataTable` or `DataReader`? – Charlieface Aug 06 '23 at 23:28
  • @Charlieface the value of myCell.Text is literally " " – SethD02 Aug 06 '23 at 23:32
  • Well how did that value get there? Maybe you should be correcting your data at source, in your database? Or in the `SELECT` statement that pulls out the data? Otherwise what's wrong with just checking for that string? How is "obviously the string " " passes this condition" relevant to the question? – Charlieface Aug 06 '23 at 23:36
  • @Charlieface Sorry I guess I'm just looking to understand this issue so I can implement this in a better way? The source data is correct and shows NULL in sql. Sorry if this is a waste of time I was hoping to gain some insight into why this would happen or if it's a known problem that can be worked around without "cheating" – SethD02 Aug 06 '23 at 23:39
  • 3
    Take a look at the couple of answers to this question, that talk about either setting the "Null value" text, or regarding HTML encoding/decoding. If you're not aware,   is a standard HTML output for rendering a "space" character in the browser. It seems your RadGrid is expecting that it should be outputting empty values (be that actually Null, or just an empty string/space character) - but, obviously you're not specifically wanting that. There should be some property (or properties) that you can set on the RadGrid to change this behaviour (sorry that I don't know specifically) – Craig Aug 06 '23 at 23:48
  • Why not put a breakpoint in that code and inspect the value of these "nulls". Although coming from a Sql DB would guess it's DBNull.Value you're looking for – Hursey Aug 07 '23 at 00:02
  • I don't use that control but my guess would be that you're using the `Text` property. The actual text displayed in a control and the value that that text represents are often not the same thing. If the display is HTML then the non-breaking space is probably required to make it look right. Is there another property, e.g. `Value`, that contains the actual data in that cell, rather than the text representing that data? Consider the fact that `Text` is always going to be a `String` but non-null data could be `Integer` or `Date` or any other type. – jmcilhinney Aug 07 '23 at 03:44
  • `if myCell.Text <90` is comparing a `String` to an `Integer` so that obviously makes no sense from the outset. You should turn `Option Strict On` in your project and start respecting data types. You'll write better code as a result. – jmcilhinney Aug 07 '23 at 03:46
  • The `Text` values have already been formatted by the grid for presentation purposes. If you need access to the raw values have you read through [Accessing Raw Field Data and Key Values](https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/accessing-values-and-controls/server-side/accessing-cells#accessing-raw-field-data-and-key-values) yet? – AlwaysLearning Aug 07 '23 at 08:31

0 Answers0