HighTechTalks DotNet Forums  

Detecting Null Values

ASP.net Building Controls microsoft.public.dotnet.framework.aspnet.buildingcontrols


Discuss Detecting Null Values in the ASP.net Building Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Jordan
 
Posts: n/a

Default Detecting Null Values - 09-13-2004 , 04:23 PM






I have a DataGrid from which the datasource is a SqlDataReader based on a
SQL Server Stored Procedure. This SP returns columns will NULL values.

When I DataBind() to the DataGrid, all is well. It's when I want to detect
these null values in the ItemDataBound event that...well...I can't detect
them. I've tried everything I can think of.

Why does'nt (e.Items.Cells[1].Text == null) work?

tia, j



Reply With Quote
  #2  
Old   
Hermit Dave
 
Posts: n/a

Default Re: Detecting Null Values - 09-13-2004 , 04:42 PM






J,

You should only use datareader if you want to iterate and process the data
being returned.

for assigning datasource consider using dataset

DataReader keep the connection open till you explicitly close them or if you
command open specifies it.

As for your query.. i would suggest you you use
Returns the first nonnull expression among its arguments.

Syntax
COALESCE ( expression [ ,...n ] )

ie you specify say coalesce(columnname, '')

if the value in column is null it will return '' which is empty string


--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Jordan" <jfritts (AT) learn (DOT) colostate.edu> wrote

Quote:
I have a DataGrid from which the datasource is a SqlDataReader based on a
SQL Server Stored Procedure. This SP returns columns will NULL values.

When I DataBind() to the DataGrid, all is well. It's when I want to detect
these null values in the ItemDataBound event that...well...I can't detect
them. I've tried everything I can think of.

Why does'nt (e.Items.Cells[1].Text == null) work?

tia, j





Reply With Quote
  #3  
Old   
Jordan
 
Posts: n/a

Default Re: Detecting Null Values - 09-13-2004 , 05:06 PM



Thanks Dave.

Unfortunately, I'm using a GROUPING ROLLUP query so I can't detect the null
values using the COALESCE statement for the field I'd like to. I ended up
making this query a subquery as it does seem easier to detect any string in
..NET than a null value.

I do use SqlDataReader = Cmd.ExecuteReader(CommandBehavior.CloseConnection) .
Is this any different than a DataSet - probably more is passed back to the
client in the datareader instance if anything I'm guessing.

It's truly sad that functionaliy seems to have been removed from .NET.
Detecting nulls with a simple comparision is truly an *everyday* need.

j


"Hermit Dave" <hermitd.REMOVE (AT) CAPS (DOT) AND.DOTS.hotmail.com> wrote

Quote:
J,

You should only use datareader if you want to iterate and process the data
being returned.

for assigning datasource consider using dataset

DataReader keep the connection open till you explicitly close them or if
you
command open specifies it.

As for your query.. i would suggest you you use
Returns the first nonnull expression among its arguments.

Syntax
COALESCE ( expression [ ,...n ] )

ie you specify say coalesce(columnname, '')

if the value in column is null it will return '' which is empty string


--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Jordan" <jfritts (AT) learn (DOT) colostate.edu> wrote in message
news:O8wYJfdmEHA.1656 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
I have a DataGrid from which the datasource is a SqlDataReader based on
a
SQL Server Stored Procedure. This SP returns columns will NULL values.

When I DataBind() to the DataGrid, all is well. It's when I want to
detect
these null values in the ItemDataBound event that...well...I can't
detect
them. I've tried everything I can think of.

Why does'nt (e.Items.Cells[1].Text == null) work?

tia, j







Reply With Quote
  #4  
Old   
John Saunders
 
Posts: n/a

Default Re: Detecting Null Values - 09-13-2004 , 06:42 PM



"Jordan" <jfritts (AT) learn (DOT) colostate.edu> wrote

Quote:
I have a DataGrid from which the datasource is a SqlDataReader based on a
SQL Server Stored Procedure. This SP returns columns will NULL values.

When I DataBind() to the DataGrid, all is well. It's when I want to detect
these null values in the ItemDataBound event that...well...I can't detect
them. I've tried everything I can think of.
First of all, as has been said by another poster, don't use DataReader for
this. Use a DataSet.

Second, in the ItemDataBound event, you have access to the DataItem which is
being bound to that DataGridItem. If you are using a DataSet as your
DataSource, then DataItem will be a DataRowView instance. You can use that
to look at each column in the row. In particular, you can use:

DataRowView drv = ((DataRowView) e.Item.DataItem);
if (drv.Row.IsNull("myNullableColumn"))
{
// Column is NULL in this row
}

--
John Saunders
johnwsaundersiii at hotmail




Reply With Quote
  #5  
Old   
Hermit Dave
 
Posts: n/a

Default Re: Detecting Null Values - 09-14-2004 , 09:30 AM



and as the other user suggested

check the column for null
if dr is your DataRow then in c#
you can use
if(dr["ColumnName"] == System.DbNull.Value)
{
// you code here
}

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Jordan" <jfritts (AT) learn (DOT) colostate.edu> wrote

Quote:
Thanks Dave.

Unfortunately, I'm using a GROUPING ROLLUP query so I can't detect the
null
values using the COALESCE statement for the field I'd like to. I ended up
making this query a subquery as it does seem easier to detect any string
in
.NET than a null value.

I do use SqlDataReader =
Cmd.ExecuteReader(CommandBehavior.CloseConnection) .
Is this any different than a DataSet - probably more is passed back to the
client in the datareader instance if anything I'm guessing.

It's truly sad that functionaliy seems to have been removed from .NET.
Detecting nulls with a simple comparision is truly an *everyday* need.

j


"Hermit Dave" <hermitd.REMOVE (AT) CAPS (DOT) AND.DOTS.hotmail.com> wrote in message
news:OHUjLqdmEHA.3172 (AT) TK2MSFTNGP10 (DOT) phx.gbl...
J,

You should only use datareader if you want to iterate and process the
data
being returned.

for assigning datasource consider using dataset

DataReader keep the connection open till you explicitly close them or if
you
command open specifies it.

As for your query.. i would suggest you you use
Returns the first nonnull expression among its arguments.

Syntax
COALESCE ( expression [ ,...n ] )

ie you specify say coalesce(columnname, '')

if the value in column is null it will return '' which is empty string


--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Jordan" <jfritts (AT) learn (DOT) colostate.edu> wrote in message
news:O8wYJfdmEHA.1656 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
I have a DataGrid from which the datasource is a SqlDataReader based
on
a
SQL Server Stored Procedure. This SP returns columns will NULL values.

When I DataBind() to the DataGrid, all is well. It's when I want to
detect
these null values in the ItemDataBound event that...well...I can't
detect
them. I've tried everything I can think of.

Why does'nt (e.Items.Cells[1].Text == null) work?

tia, j









Reply With Quote
  #6  
Old   
Jordan
 
Posts: n/a

Default Re: Detecting Null Values (Thanks) - 09-14-2004 , 09:50 AM



Just wanted to say thanks. I will change the data retrieval to use a
DataSet. Since I'm not reading through the DataReader, I can't use the
DBNull.value so I will use the solution below, as it is exactly the instance
in which I wish to check...during the ItemDataBound event.

Quote:
DataRowView drv = ((DataRowView) e.Item.DataItem);
if (drv.Row.IsNull("myNullableColumn"))
{
// Column is NULL in this row
}
thanks again, j




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.