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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
AhelblingAuthor Commented:
The grid is populated from the DB yes
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.