How to check for Null?

Hello,

Can someone show me the proper syntax to check for null using VB.net?

GridView1.Rows(i).Cells(k).Text


TIA,
Andrew
AhelblingAsked:
Who is Participating?
 
CodeCruiserCommented:
There is a space in cell which gets encoded to html so you would want to try

If String.IsNullOrWhiteSpace(GridView1.Rows(i).Cells(k).Text) Or GridView1.Rows(i).Cells(k).Text = " " Then
0
 
santhimurthydCommented:
Try this

If String.IsNullOrEmpty(GridView1.Rows(i).Cells(k).Text) Then

        End If
0
 
CodeCruiserCommented:
If the grid is populated from a database then I would use

If IsDBNull(...)
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
AhelblingAuthor Commented:
The grid is populated from the DB yes
0
 
AhelblingAuthor Commented:
   Protected Sub btnExportCSV_Click(ByVal sender As Object, ByVal e As EventArgs)
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv")
        Response.Charset = ""
        Response.ContentType = "application/text"

        GridView1.AllowPaging = False
        GridView1.DataBind()

        Dim sb As New StringBuilder()
        For k As Integer = 0 To GridView1.Columns.Count - 1
            'add separator
            sb.Append(GridView1.Columns(k).HeaderText + ","c)
        Next
        'append new line
        sb.Append(vbCr & vbLf)
        For i As Integer = 0 To GridView1.Rows.Count - 1
            For k As Integer = 0 To GridView1.Columns.Count - 1

                If String.IsNullOrEmpty(GridView1.Rows(i).Cells(k).Text) Then
                   'convert null to " "
                Else
                End If

                'add separator
                sb.Append(GridView1.Rows(i).Cells(k).Text + ","c)
            Next
            'append new line
            sb.Append(vbCr & vbLf)
        Next
        Response.Output.Write(sb.ToString())
        Response.Flush()
        Response.End()
    End Sub
0
 
AhelblingAuthor Commented:
I am trying to eliminate the   that show up in the csv I am exporting.
Capture.JPG
0
 
AhelblingAuthor Commented:
It seems that if the field is Null in the DB then it exports it as a nbsp; and thats not goin to work.


Capture.JPG
0
 
santhimurthydCommented:
whether the syntax given is not working

  If String.IsNullOrEmpty(GridView1.Rows(i).Cells(k).Text) Then
                   'convert null to " "
                Else
                End If
0
 
santhimurthydCommented:

Try with this

  If String.IsNullOrWhiteSpace(GridView1.Rows(i).Cells(k).Text) Then
                   'convert null to " "
                Else
                End If
 
0
 
AhelblingAuthor Commented:
               If String.IsNullOrEmpty(GridView1.Rows(i).Cells(k).Text) Then
                    GridView1.Rows(i).Cells(k).Text = "TEST"
                Else
                End If
0
 
AhelblingAuthor Commented:
               If String.IsNullOrWhiteSpace(GridView1.Rows(i).Cells(k).Text) Then
                    GridView1.Rows(i).Cells(k).Text = "TEST"
                Else
                End If

results in the attached ScreenShot SS
0
 
AhelblingAuthor Commented:
               If String.IsNullOrEmpty(GridView1.Rows(i).Cells(k).Text) Then
                    GridView1.Rows(i).Cells(k).Text = "TEST"
                Else
                End If

results in: SS
0
 
santhimurthydCommented:
Could you update the Null field from SQL with is null option

Make it ('')  empty string use IsNull(field, '')

this will help to solve the issue
0
 
AhelblingAuthor Commented:
@ santhimurthyd

I cannot change the null data due to multiple cascading drop down filters based on the data.  Thanks for your input though.


@ CodeCruiser

that seems to work. thank you!


    Protected Sub btnExportCSV_Click(ByVal sender As Object, ByVal e As EventArgs)
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv")
        Response.Charset = ""
        Response.ContentType = "application/text"

        GridView1.AllowPaging = False
        GridView1.DataBind()

        Dim sb As New StringBuilder()
        For k As Integer = 3 To GridView1.Columns.Count - 1
            'add separator
            sb.Append(GridView1.Columns(k).HeaderText + ","c)
        Next
        'append new line
        sb.Append(vbCr & vbLf)
        For i As Integer = 3 To GridView1.Rows.Count - 1
            For k As Integer = 3 To GridView1.Columns.Count - 1

                If String.IsNullOrWhiteSpace(GridView1.Rows(i).Cells(k).Text) Or GridView1.Rows(i).Cells(k).Text = " " Then
                    GridView1.Rows(i).Cells(k).Text = "TEST"
                Else
                End If

                'add separator
                sb.Append(GridView1.Rows(i).Cells(k).Text + ","c)
            Next
            'append new line
            sb.Append(vbCr & vbLf)
        Next
        Response.Output.Write(sb.ToString())
        Response.Flush()
        Response.End()
    End Sub SS
0
 
nepaluzCommented:
You have got something wrong, try
If String.IsNullOrWhiteSpace(GridView1.Rows(i).Cells(k).Value)...

Open in new window

ratherthan
If String.IsNullOrWhiteSpace(GridView1.Rows(i).Cells(k).Text)...

Open in new window

0
 
santhimurthydCommented:
thank Ahelb

As you able to solve the issue
I feel as instead of gettign the value from Grid view cell on each processing, it will degrade the performance on handling large data, So declare an variable and use the same as in code snippet

 
Protected Sub btnExportCSV_Click(ByVal sender As Object, ByVal e As EventArgs)
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv")
        Response.Charset = ""
        Response.ContentType = "application/text"

        GridView1.AllowPaging = False
        GridView1.DataBind()

        Dim sb As New StringBuilder()
        Dim val As String
        For k As Integer = 3 To GridView1.Columns.Count - 1
            'add separator
            sb.Append(GridView1.Columns(k).HeaderText + ","c)
        Next
        'append new line
        sb.Append(vbCr & vbLf)
        For i As Integer = 3 To GridView1.Rows.Count - 1
            For k As Integer = 3 To GridView1.Columns.Count - 1
                val = GridView1.Rows(i).Cells(k).Text
                If String.IsNullOrWhiteSpace(val) Or String.Equals(val, " ") Then
                    GridView1.Rows(i).Cells(k).Text = "TEST"
                Else
                End If

                'add separator
                sb.Append(val + ","c)
            Next
            'append new line
            sb.Append(vbCr & vbLf)
        Next
        Response.Output.Write(sb.ToString())
        Response.Flush()
        Response.End()
    End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.