473,378 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

SELECT .. WHERE column IS NULL?

I just cannot manage to perform a SELECT query with NULL parameter...
My CATEGORY table does have one row where TCATEGORYPARENTID is null (real DB
null value).

TCATEGORYID and TCATEGORYPARENTID are uniqueidentifier columns.

My questions:

- is Type="Object", below, necessary?
- what shall I specify for DefaultValue in my function? I tried everything.
How come the DefaultValue must be a string? Why can't I specify
DBNull.Value?
- How do I make this work....

Thanks for your help.

-J

Code follows:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:MyBaseConnectionString %>"
SelectCommand="SELECT [TCATEGORYID], [[TCATEGORYNAME] FROM [TCATEGORY] WHERE
([TCATEGORYPARENTID] = @TCATEGORYPARENTID) ORDER BY [TCATEGORYPARENTID],
[TCATEGORYNAME]">
<SelectParameters>
<asp:Parameter Name="TCATEGORYPARENTID" Type="Object" />
</SelectParameters>
</asp:SqlDataSource>

private void QueryCategory()
{
SqlDataSource1.SelectParameters["TCATEGORYPARENTID"].DefaultValue =
System.Data.SqlTypes.SqlGuid.Null.ToString();

DataView dv =
(DataView)SqlDataSource1.Select(DataSourceSelectAr guments.Empty);

if (dv != null) // I always end up with either dv null or
dv.Table.Rows.Count = 0 :-(
{
string prefix = new String(' ', depth);
foreach (DataRow row in dv.Table.Rows)
{
[...]
}
}
}
Aug 13 '06 #1
5 5475
I am not an ASP.NET person so can't help with that but your SQL code appears
to be wrong.
SELECT * FROM tablex WHERE NULL = NULL -- will always return zero rows.

Tim S

Try This

SelectCommand = "
SELECT [TCATEGORYID], [[TCATEGORYNAME]
FROM [TCATEGORY]
WHERE (([TCATEGORYPARENTID] = @TCATEGORYPARENTID) OR ([TCATEGORYPARENTID]
IS NULL AND @TCATEGORYPARENTID IS NULL))
ORDER BY [TCATEGORYPARENTID], [TCATEGORYNAME]
"
"John" <no***@replytotgroup.pleasewrote in message
news:us**************@TK2MSFTNGP03.phx.gbl...
>I just cannot manage to perform a SELECT query with NULL parameter...
My CATEGORY table does have one row where TCATEGORYPARENTID is null (real
DB null value).

TCATEGORYID and TCATEGORYPARENTID are uniqueidentifier columns.

My questions:

- is Type="Object", below, necessary?
- what shall I specify for DefaultValue in my function? I tried
everything. How come the DefaultValue must be a string? Why can't I
specify DBNull.Value?
- How do I make this work....

Thanks for your help.

-J

Code follows:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyBaseConnectionString %>"
SelectCommand="SELECT [TCATEGORYID], [[TCATEGORYNAME] FROM [TCATEGORY]
WHERE ([TCATEGORYPARENTID] = @TCATEGORYPARENTID) ORDER BY
[TCATEGORYPARENTID], [TCATEGORYNAME]">
<SelectParameters>
<asp:Parameter Name="TCATEGORYPARENTID" Type="Object" />
</SelectParameters>
</asp:SqlDataSource>

private void QueryCategory()
{
SqlDataSource1.SelectParameters["TCATEGORYPARENTID"].DefaultValue =
System.Data.SqlTypes.SqlGuid.Null.ToString();

DataView dv =
(DataView)SqlDataSource1.Select(DataSourceSelectAr guments.Empty);

if (dv != null) // I always end up with either dv null or
dv.Table.Rows.Count = 0 :-(
{
string prefix = new String(' ', depth);
foreach (DataRow row in dv.Table.Rows)
{
[...]
}
}
}


Aug 13 '06 #2
Thanks Tim, but your solution is less than ideal, because I would have to
have two different SqlDataSources:

- one to handle the case where I want a NULL parent category (WHERE
[TCATEGORYPARENTID] IS NULL)
- the other one to handle the case where I provide a parent category (WHERE
[TCATEGORYPARENTID] = @TCATEGORYPARENTID)

Don't SelectParameters work for NULL parameter values as well?

It seems like a severe limitation to me.
"Tim Stahlhut" <st******@netzero.comwrote in message
news:12*************@corp.supernews.com...
>I am not an ASP.NET person so can't help with that but your SQL code
appears to be wrong.
SELECT * FROM tablex WHERE NULL = NULL -- will always return zero rows.

Tim S

Try This

SelectCommand = "
SELECT [TCATEGORYID], [[TCATEGORYNAME]
FROM [TCATEGORY]
WHERE (([TCATEGORYPARENTID] = @TCATEGORYPARENTID) OR ([TCATEGORYPARENTID]
IS NULL AND @TCATEGORYPARENTID IS NULL))
ORDER BY [TCATEGORYPARENTID], [TCATEGORYNAME]
"
"John" <no***@replytotgroup.pleasewrote in message
news:us**************@TK2MSFTNGP03.phx.gbl...
>>I just cannot manage to perform a SELECT query with NULL parameter...
My CATEGORY table does have one row where TCATEGORYPARENTID is null (real
DB null value).

TCATEGORYID and TCATEGORYPARENTID are uniqueidentifier columns.

My questions:

- is Type="Object", below, necessary?
- what shall I specify for DefaultValue in my function? I tried
everything. How come the DefaultValue must be a string? Why can't I
specify DBNull.Value?
- How do I make this work....

Thanks for your help.

-J

Code follows:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyBaseConnectionString %>"
SelectCommand="SELECT [TCATEGORYID], [[TCATEGORYNAME] FROM [TCATEGORY]
WHERE ([TCATEGORYPARENTID] = @TCATEGORYPARENTID) ORDER BY
[TCATEGORYPARENTID], [TCATEGORYNAME]">
<SelectParameters>
<asp:Parameter Name="TCATEGORYPARENTID" Type="Object" />
</SelectParameters>
</asp:SqlDataSource>

private void QueryCategory()
{
SqlDataSource1.SelectParameters["TCATEGORYPARENTID"].DefaultValue =
System.Data.SqlTypes.SqlGuid.Null.ToString();

DataView dv =
(DataView)SqlDataSource1.Select(DataSourceSelectA rguments.Empty);

if (dv != null) // I always end up with either dv null or
dv.Table.Rows.Count = 0 :-(
{
string prefix = new String(' ', depth);
foreach (DataRow row in dv.Table.Rows)
{
[...]
}
}
}



Aug 13 '06 #3
It is the SQL standard in all databases that claim to be close to SQL 92 let
alone SQL99.

Tim S

"John" <no***@replytotgroup.pleasewrote in message
news:e0**************@TK2MSFTNGP04.phx.gbl...
Thanks Tim, but your solution is less than ideal, because I would have to
have two different SqlDataSources:

- one to handle the case where I want a NULL parent category (WHERE
[TCATEGORYPARENTID] IS NULL)
- the other one to handle the case where I provide a parent category
(WHERE [TCATEGORYPARENTID] = @TCATEGORYPARENTID)

Don't SelectParameters work for NULL parameter values as well?

It seems like a severe limitation to me.
"Tim Stahlhut" <st******@netzero.comwrote in message
news:12*************@corp.supernews.com...
>>I am not an ASP.NET person so can't help with that but your SQL code
appears to be wrong.
SELECT * FROM tablex WHERE NULL = NULL -- will always return zero rows.

Tim S

Try This

SelectCommand = "
SELECT [TCATEGORYID], [[TCATEGORYNAME]
FROM [TCATEGORY]
WHERE (([TCATEGORYPARENTID] = @TCATEGORYPARENTID) OR
([TCATEGORYPARENTID] IS NULL AND @TCATEGORYPARENTID IS NULL))
ORDER BY [TCATEGORYPARENTID], [TCATEGORYNAME]
"
"John" <no***@replytotgroup.pleasewrote in message
news:us**************@TK2MSFTNGP03.phx.gbl...
>>>I just cannot manage to perform a SELECT query with NULL parameter...
My CATEGORY table does have one row where TCATEGORYPARENTID is null
(real DB null value).

TCATEGORYID and TCATEGORYPARENTID are uniqueidentifier columns.

My questions:

- is Type="Object", below, necessary?
- what shall I specify for DefaultValue in my function? I tried
everything. How come the DefaultValue must be a string? Why can't I
specify DBNull.Value?
- How do I make this work....

Thanks for your help.

-J

Code follows:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyBaseConnectionString %>"
SelectCommand="SELECT [TCATEGORYID], [[TCATEGORYNAME] FROM [TCATEGORY]
WHERE ([TCATEGORYPARENTID] = @TCATEGORYPARENTID) ORDER BY
[TCATEGORYPARENTID], [TCATEGORYNAME]">
<SelectParameters>
<asp:Parameter Name="TCATEGORYPARENTID" Type="Object" />
</SelectParameters>
</asp:SqlDataSource>

private void QueryCategory()
{
SqlDataSource1.SelectParameters["TCATEGORYPARENTID"].DefaultValue =
System.Data.SqlTypes.SqlGuid.Null.ToString();

DataView dv =
(DataView)SqlDataSource1.Select(DataSourceSelect Arguments.Empty);

if (dv != null) // I always end up with either dv null or
dv.Table.Rows.Count = 0 :-(
{
string prefix = new String(' ', depth);
foreach (DataRow row in dv.Table.Rows)
{
[...]
}
}
}




Aug 13 '06 #4
I know there is a standard to respect, but we are talking about the
authoring layer here (ADO.NET). I had thought the authoring layer would
handle this common case.

I have tried using a SqlCommand instead and obtain the same result: a
parameter with a value of NULL doesn't seem to be supported:

using (SqlConnection conn = new
SqlConnection(SqlDataSource1.ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM TCATEGORY WHERE
TCATEGORYPARENTID = @TCATEGORYPARENTID");
cmd.Connection = conn;
SqlParameter p = new SqlParameter("@TCATEGORYPARENTID",
SqlDbType.UniqueIdentifier);
p.Value = DBNull.Value;
cmd.Parameters.Add(p);

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
int c = ds.Tables[0].Rows.Count; // I get 0 here, that's wrong, I've got
one row with TCATEGORYPARENTID being NULL
}

"Tim Stahlhut" <st******@netzero.comwrote in message
news:12*************@corp.supernews.com...
It is the SQL standard in all databases that claim to be close to SQL 92
let alone SQL99.

Tim S

"John" <no***@replytotgroup.pleasewrote in message
news:e0**************@TK2MSFTNGP04.phx.gbl...
>Thanks Tim, but your solution is less than ideal, because I would have to
have two different SqlDataSources:

- one to handle the case where I want a NULL parent category (WHERE
[TCATEGORYPARENTID] IS NULL)
- the other one to handle the case where I provide a parent category
(WHERE [TCATEGORYPARENTID] = @TCATEGORYPARENTID)

Don't SelectParameters work for NULL parameter values as well?

It seems like a severe limitation to me.
"Tim Stahlhut" <st******@netzero.comwrote in message
news:12*************@corp.supernews.com...
>>>I am not an ASP.NET person so can't help with that but your SQL code
appears to be wrong.
SELECT * FROM tablex WHERE NULL = NULL -- will always return zero rows.

Tim S

Try This

SelectCommand = "
SELECT [TCATEGORYID], [[TCATEGORYNAME]
FROM [TCATEGORY]
WHERE (([TCATEGORYPARENTID] = @TCATEGORYPARENTID) OR
([TCATEGORYPARENTID] IS NULL AND @TCATEGORYPARENTID IS NULL))
ORDER BY [TCATEGORYPARENTID], [TCATEGORYNAME]
"
"John" <no***@replytotgroup.pleasewrote in message
news:us**************@TK2MSFTNGP03.phx.gbl...
I just cannot manage to perform a SELECT query with NULL parameter...
My CATEGORY table does have one row where TCATEGORYPARENTID is null
(real DB null value).

TCATEGORYID and TCATEGORYPARENTID are uniqueidentifier columns.

My questions:

- is Type="Object", below, necessary?
- what shall I specify for DefaultValue in my function? I tried
everything. How come the DefaultValue must be a string? Why can't I
specify DBNull.Value?
- How do I make this work....

Thanks for your help.

-J

Code follows:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyBaseConnectionString %>"
SelectCommand="SELECT [TCATEGORYID], [[TCATEGORYNAME] FROM [TCATEGORY]
WHERE ([TCATEGORYPARENTID] = @TCATEGORYPARENTID) ORDER BY
[TCATEGORYPARENTID], [TCATEGORYNAME]">
<SelectParameters>
<asp:Parameter Name="TCATEGORYPARENTID" Type="Object" />
</SelectParameters>
</asp:SqlDataSource>

private void QueryCategory()
{
SqlDataSource1.SelectParameters["TCATEGORYPARENTID"].DefaultValue =
System.Data.SqlTypes.SqlGuid.Null.ToString();

DataView dv =
(DataView)SqlDataSource1.Select(DataSourceSelec tArguments.Empty);

if (dv != null) // I always end up with either dv null or
dv.Table.Rows.Count = 0 :-(
{
string prefix = new String(' ', depth);
foreach (DataRow row in dv.Table.Rows)
{
[...]
}
}
}




Aug 13 '06 #5
SELECT * FROM TCATEGORY WHERE
(TCATEGORYPARENTID = @TCATEGORYPARENTID
OR (TCATEGORYPARENTID IS NULL AND @TCATEGORYPARENTID IS NULL))

will fetch records where the IDs match or both the ID and input is
null.

John wrote:
I know there is a standard to respect, but we are talking about the
authoring layer here (ADO.NET). I had thought the authoring layer would
handle this common case.

I have tried using a SqlCommand instead and obtain the same result: a
parameter with a value of NULL doesn't seem to be supported:

using (SqlConnection conn = new
SqlConnection(SqlDataSource1.ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM TCATEGORY WHERE
TCATEGORYPARENTID = @TCATEGORYPARENTID");
cmd.Connection = conn;
SqlParameter p = new SqlParameter("@TCATEGORYPARENTID",
SqlDbType.UniqueIdentifier);
p.Value = DBNull.Value;
cmd.Parameters.Add(p);

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
int c = ds.Tables[0].Rows.Count; // I get 0 here, that's wrong, I've got
one row with TCATEGORYPARENTID being NULL
}

"Tim Stahlhut" <st******@netzero.comwrote in message
news:12*************@corp.supernews.com...
It is the SQL standard in all databases that claim to be close to SQL 92
let alone SQL99.

Tim S

"John" <no***@replytotgroup.pleasewrote in message
news:e0**************@TK2MSFTNGP04.phx.gbl...
Thanks Tim, but your solution is less than ideal, because I would have to
have two different SqlDataSources:

- one to handle the case where I want a NULL parent category (WHERE
[TCATEGORYPARENTID] IS NULL)
- the other one to handle the case where I provide a parent category
(WHERE [TCATEGORYPARENTID] = @TCATEGORYPARENTID)

Don't SelectParameters work for NULL parameter values as well?

It seems like a severe limitation to me.
"Tim Stahlhut" <st******@netzero.comwrote in message
news:12*************@corp.supernews.com...
I am not an ASP.NET person so can't help with that but your SQL code
appears to be wrong.
SELECT * FROM tablex WHERE NULL = NULL -- will always return zero rows.

Tim S

Try This

SelectCommand = "
SELECT [TCATEGORYID], [[TCATEGORYNAME]
FROM [TCATEGORY]
WHERE (([TCATEGORYPARENTID] = @TCATEGORYPARENTID) OR
([TCATEGORYPARENTID] IS NULL AND @TCATEGORYPARENTID IS NULL))
ORDER BY [TCATEGORYPARENTID], [TCATEGORYNAME]
"
"John" <no***@replytotgroup.pleasewrote in message
news:us**************@TK2MSFTNGP03.phx.gbl...
I just cannot manage to perform a SELECT query with NULL parameter...
My CATEGORY table does have one row where TCATEGORYPARENTID is null
(real DB null value).

TCATEGORYID and TCATEGORYPARENTID are uniqueidentifier columns.

My questions:

- is Type="Object", below, necessary?
- what shall I specify for DefaultValue in my function? I tried
everything. How come the DefaultValue must be a string? Why can't I
specify DBNull.Value?
- How do I make this work....

Thanks for your help.

-J

Code follows:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyBaseConnectionString %>"
SelectCommand="SELECT [TCATEGORYID], [[TCATEGORYNAME] FROM [TCATEGORY]
WHERE ([TCATEGORYPARENTID] = @TCATEGORYPARENTID) ORDER BY
[TCATEGORYPARENTID], [TCATEGORYNAME]">
<SelectParameters>
<asp:Parameter Name="TCATEGORYPARENTID" Type="Object" />
</SelectParameters>
</asp:SqlDataSource>

private void QueryCategory()
{
SqlDataSource1.SelectParameters["TCATEGORYPARENTID"].DefaultValue =
System.Data.SqlTypes.SqlGuid.Null.ToString();

DataView dv =
(DataView)SqlDataSource1.Select(DataSourceSelect Arguments.Empty);

if (dv != null) // I always end up with either dv null or
dv.Table.Rows.Count = 0 :-(
{
string prefix = new String(' ', depth);
foreach (DataRow row in dv.Table.Rows)
{
[...]
}
}
}

Aug 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: skidvd | last post by:
Hello: I have just recently converted to using the InnoDB table type so that I can enforce FOREIGN key constraints. I have been using MyISAM tables (accessed via JDBC) successfully for some...
0
by: Rob Knowles | last post by:
I have created a script that runs once a week and copies data from one table (phpbb_users is the actual table name) in a database called users I have a table called users. There are two tables...
3
by: Ian T | last post by:
Hi, I've got what I think (probably incorrectly) should be a simple SELECT : Two colums with data like col1 col2 1 50 1 51 2 50
4
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table...
3
by: dumbledad | last post by:
Hi All, I'm confused by how to replace a SELECT statement in a SQL statement with a specific value. The table I'm working on is a list of words (a column called "word") with an index int...
10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
4
by: Lucky | last post by:
hi guys! Currently i'm facing a bit odd situation. i cant modify my code so i have to make changes in SQL Server. the problem is, i've modified one table by adding new column and now i want...
4
by: tshad | last post by:
How do I tell DataAdapter that Column 2 and 3 are string and not integer? Following is the example data that comes from the .csv file FEDERAL TAX,1084,0000 COREHCR,1084,0000 CLIENT P,1084,0000...
4
tjc0ol
by: tjc0ol | last post by:
Hi guys, I'm a newbie in php and I got error in my index.php which is: 1054 - Unknown column 'p.products_id' in 'on clause' select p.products_image, pd.products_name, p.products_id,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.