DataGird replace <NULL> value

Hi, I have datagrid that is bound to a SQL datasource. The query retuns <NULL> values. I would like to repace all <NULL> value in my datagrid with
some other text.

How would I do it via the ItemDataBound event..


Thanks

xy15973Asked:
Who is Participating?
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.

nauman_ahmedCommented:
Something like the following should work:

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
            If e.Item.Cells(0).Text Is Nothing Then
                e.Item.Cells(0).Text = "Not defined"
            End If


        End Sub

Best, Nauman.
0
AerosSagaCommented:
This may be an alternate way not sure if it works with e arguments or not, but I assume it will

  If IsDbNULL(e.Item.Cells(0).Text) Then
                e.Item.Cells(0).Text = "Not defined"
  End If

0
tusharashahCommented:
The way I've to do sometimes is with checking out for "" (empty string) and &nbsp; (space)...
Following is the code for that..
----------------------------------------------------------------------------------------------------------------
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
     switch( e.Item.ItemType )
     {
          case ListItemType.Item:
          case ListItemType.AlternatingItem:

          if( e.Item.Cells[0].Text == "" || e.Item.Cells[0].Text == "&nbsp;" )
                {
                     e.Item.Cells[0].Text = "0";
                }

          break;
     }
}
----------------------------------------------------------------------------------------------------------------

-tushar
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

AerosSagaCommented:
or alternately you could specify this in your SQL before you bind the grid.
0
xy15973Author Commented:
Thanks, do you know what the C# code would be like

If have tried

if (e.items.cells[0].text == DbNull) >>> error that DbNull is an class also tried DbNull.value ..cannot compare string to bool


also tried

if (e.items.cells[0].text == null)  >> nothing  so to if i replace null with ""..


When i do a len on the text it is = 6 , this point to the fact that that the system does get "<NULL>"  from database


0
AerosSagaCommented:
if (IsDbNULL(e.Item.Cells(0).Text)) {
 e.Item.Cells(0).Text = "Not defined";
}
0
tusharashahCommented:
The C# code for checking DBNull would be like following:

if (IsDbNULL(e.Item.Cells(0).Text)) {
 e.Item.Cells(0).Text = "Not defined";
}

-tushar
0
tusharashahCommented:
ahh.. too quick Aeros..
0
AerosSagaCommented:
hehe;)
0
tusharashahCommented:
Correction...

The C# code for checking DBNull would be like following:

if (IsDbNULL(e.Item.Cells[0].Text)) {
 e.Item.Cells[0].Text = "Not defined";
}

-tushar
0
AerosSagaCommented:
square brackets I should have notice that
0
xy15973Author Commented:
IsDbNull does not exsits for c#!


0
tusharashahCommented:
Try to compare like following & see..

 if( e.Item.Cells[0].Text == "" || e.Item.Cells[0].Text == "&nbsp;" )
                {
                     e.Item.Cells[0].Text = "0";
                }
-tushar
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
AerosSagaCommented:
hehe yay vb;)
0
nauman_ahmedCommented:
The C# equivalent is as follows:

if (e.Item.Cells[0].Text != null)
{
 e.Item.Cells[0].Text = "Not defined";
}


Best, Nauman.
0
nitrogenxCommented:
You can put it right in your SQL query if you want, post the code and I'll show you how.
0
nitrogenxCommented:
The SQL function COALESCE(a,b) will return the first non-null response.
An example:
SELECT
  COALESCE(Name,'Not Given'),
  COALESCE(Age,0),
  COALESCE(Date,'9/9/99')
FROM table
0
Volkan VardarSoftware Team LeaderCommented:
I think the best way to do this is changing the SQL Select Query like nitrogenx stated.
Another way is:
SELECT ISNULL(Name, '') AS Name, ISNULL(Age, 0) AS Age
FROM TABLE
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.

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.