Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DataGird replace  <NULL> value

Posted on 2004-10-29
19
Medium Priority
?
272 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:xy15973
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +3
19 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 12442652
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
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12443339
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
 
LVL 18

Expert Comment

by:tusharashah
ID: 12443473
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 17

Expert Comment

by:AerosSaga
ID: 12443506
or alternately you could specify this in your SQL before you bind the grid.
0
 

Author Comment

by:xy15973
ID: 12443507
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
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12443537
if (IsDbNULL(e.Item.Cells(0).Text)) {
 e.Item.Cells(0).Text = "Not defined";
}
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12443540
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
 
LVL 18

Expert Comment

by:tusharashah
ID: 12443560
ahh.. too quick Aeros..
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12443583
hehe;)
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12443665
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
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12443681
square brackets I should have notice that
0
 

Author Comment

by:xy15973
ID: 12443844
IsDbNull does not exsits for c#!


0
 
LVL 18

Accepted Solution

by:
tusharashah earned 1500 total points
ID: 12443869
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
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12443879
hehe yay vb;)
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 12443889
The C# equivalent is as follows:

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


Best, Nauman.
0
 
LVL 3

Expert Comment

by:nitrogenx
ID: 12448256
You can put it right in your SQL query if you want, post the code and I'll show you how.
0
 
LVL 3

Expert Comment

by:nitrogenx
ID: 12448386
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
 
LVL 6

Expert Comment

by:Volkan Vardar
ID: 12483223
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question