472,142 Members | 1,183 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Add IF THEN clause to SELECT statement

I have a SQL 2000 table named IMSRejects with the fields:
IMSRejects.ProductDescription
IMSRejects.VendorName
IMSRejects.CatalogNumber
IMSRejects.ProductIMS2Desc
IMSRejects.CMA8Desc
Many of the ProductIMS2Desc and CMA8Desc values are Null. I have an ASP.NET
2.0 web page which populates a datagrid with this data. I also have text
boxes on my web page so users can filter the datagrid based on keywords. The
problem I'm having is that the SELECT statement that populates the datagrid
does no handle null values for ProductIMS2Desc and CMA8Desc. Here's the
statement:

Dim _SqlAccounts As String = "Select * FROM IMSRejects WHERE " & _
"IMSRejects.ProductDescription LIKE '%" & strProductName & "%' AND "
& _
"IMSRejects.VendorName LIKE '%" & strVendorName & "%' AND " & _
"IMSRejects.CatalogNumber LIKE '%" & strCatalogName & "%' AND " & _
"IMSRejects.ProductIMS2Desc LIKE '%" & strFranchiseName & "%' AND "
& _
"IMSRejects.CMA8Desc LIKE '%" & strProductLineName & "%' " & _
"ORDER BY VendorName ASC, ProductIMS2Desc ASC, CMA8Desc ASC"

I'd like to default to True a checkbox on my web page named cbShowAll and
add an IF clause that will show all records including those where
ProductIMS2Desc and CMA8Desc contain NULL values. When the checkbox is FALSE,
I'd like the grid to only show records ProductIMS2Desc and CMA8Desc don't
have NULL values.

THANKS!!
Jan 25 '07 #1
3 4918
Set up the query as a stored procedure rather than write it on the fly. You
will find that it is much easier to control the paths by sending a single
boolean than building a statement blindly.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

********************************************
Think outside the box!
********************************************
"Mike C" <Mi***@discussions.microsoft.comwrote in message
news:C0**********************************@microsof t.com...
>I have a SQL 2000 table named IMSRejects with the fields:
IMSRejects.ProductDescription
IMSRejects.VendorName
IMSRejects.CatalogNumber
IMSRejects.ProductIMS2Desc
IMSRejects.CMA8Desc
Many of the ProductIMS2Desc and CMA8Desc values are Null. I have an
ASP.NET
2.0 web page which populates a datagrid with this data. I also have text
boxes on my web page so users can filter the datagrid based on keywords.
The
problem I'm having is that the SELECT statement that populates the
datagrid
does no handle null values for ProductIMS2Desc and CMA8Desc. Here's the
statement:

Dim _SqlAccounts As String = "Select * FROM IMSRejects WHERE " & _
"IMSRejects.ProductDescription LIKE '%" & strProductName & "%' AND
"
& _
"IMSRejects.VendorName LIKE '%" & strVendorName & "%' AND " & _
"IMSRejects.CatalogNumber LIKE '%" & strCatalogName & "%' AND " & _
"IMSRejects.ProductIMS2Desc LIKE '%" & strFranchiseName & "%' AND "
& _
"IMSRejects.CMA8Desc LIKE '%" & strProductLineName & "%' " & _
"ORDER BY VendorName ASC, ProductIMS2Desc ASC, CMA8Desc ASC"

I'd like to default to True a checkbox on my web page named cbShowAll and
add an IF clause that will show all records including those where
ProductIMS2Desc and CMA8Desc contain NULL values. When the checkbox is
FALSE,
I'd like the grid to only show records ProductIMS2Desc and CMA8Desc don't
have NULL values.

THANKS!!
Jan 25 '07 #2
Mike,

As Cowboy suggested, I also think you should use a stored proc for this.

To address your NULL value problems; If you have the option, make the
database columns ProductIMS2Desc and CMA8Desc not nullable. Use empty
strings ('') instead of NULLs.

Sometimes this not always possible, so the second thing you could do is to
use the t-sql coalesce() or isnull() functions to help handle the database
NULL values.

Your where clause would be something like this:
....
coalesce(IMSRejects.ProductIMS2Desc, '') LIKE '%yabayaba%'
....

or if you used the isnull() function
.....
isnull(IMSRejects.ProductIMS2Desc, '') LIKE '%yabayaba%'
.....

Your VB code should look something like the code below. I've used a
StringBuilder to help with performance and readability on the string
contatenations. I'd recommend using SelectParameters to help prevent sql
injection attacks (see
http://www.codeproject.com/cs/databa...gAdHocSQL.asp).
Also the _SqlSource variable is your SqlDataSource object that your GridView
is bound to.

Hope this helps,
Jason Vermillion

Dim _GenSql As StringBuilder = New StringBuilder(256)
Dim strProductName As String = ""
Dim strVendorName As String = ""
Dim strCatalogName As String = ""
Dim strFranchiseName As String = ""
Dim strProductLineName As String = ""
Dim _SqlAccounts As String = ""

strProductName = "%" & strProductName & "%"
strVendorName = "%" & strVendorName & "%"
strCatalogName = "%" & strCatalogName & "%"
strFranchiseName = "%" & strFranchiseName & "%"
strProductLineName = "%" & strProductLineName & "%"

_GenSql.AppendLine("Select * FROM IMSRejects WHERE")
_GenSql.AppendLine("IMSRejects.ProductDescription LIKE @ProductName
AND")
_GenSql.AppendLine("IMSRejects.VendorName LIKE @VendorName AND")
_GenSql.AppendLine("IMSRejects.CatalogNumber LIKE @CatalogName AND")

' Use the t-sql Coalesce() or isnull() function to
If (Me.cbShowAll.Checked = True) Then
_GenSql.AppendLine("coalesce(IMSRejects.ProductIMS 2Desc,'') LIKE
@FranchiseName AND")
_GenSql.AppendLine("coalesce(IMSRejects.CMA8Desc,' ') LIKE
@ProductLineName")

Else
_GenSql.AppendLine("IMSRejects.ProductIMS2Desc LIKE
@FranchiseName AND")
_GenSql.AppendLine("IMSRejects.CMA8Desc LIKE @ProductLineName")
End If

_GenSql.AppendLine("ORDER BY VendorName ASC, ProductIMS2Desc ASC,
CMA8Desc ASC")

_SqlAccounts = _GenSql.ToString()

_SqlSource.SelectCommand = _SqlAccounts
_SqlSource.SelectParameters.Add("ProductName", strProductName)
_SqlSource.SelectParameters.Add("VendorName", strVendorName)
_SqlSource.SelectParameters.Add("CatalogName", strCatalogName)
_SqlSource.SelectParameters.Add("FranchiseName", strFranchiseName)
_SqlSource.SelectParameters.Add("ProductLineName", strProductLineName)

Jan 26 '07 #3
That worked great. Thank you! I have some parts of my code as stored
procedures but still trying to understand the syntax for others.

"Jason Vermillion" wrote:
Mike,

As Cowboy suggested, I also think you should use a stored proc for this.

To address your NULL value problems; If you have the option, make the
database columns ProductIMS2Desc and CMA8Desc not nullable. Use empty
strings ('') instead of NULLs.

Sometimes this not always possible, so the second thing you could do is to
use the t-sql coalesce() or isnull() functions to help handle the database
NULL values.

Your where clause would be something like this:
...
coalesce(IMSRejects.ProductIMS2Desc, '') LIKE '%yabayaba%'
...

or if you used the isnull() function
....
isnull(IMSRejects.ProductIMS2Desc, '') LIKE '%yabayaba%'
....

Your VB code should look something like the code below. I've used a
StringBuilder to help with performance and readability on the string
contatenations. I'd recommend using SelectParameters to help prevent sql
injection attacks (see
http://www.codeproject.com/cs/databa...gAdHocSQL.asp).
Also the _SqlSource variable is your SqlDataSource object that your GridView
is bound to.

Hope this helps,
Jason Vermillion

Dim _GenSql As StringBuilder = New StringBuilder(256)
Dim strProductName As String = ""
Dim strVendorName As String = ""
Dim strCatalogName As String = ""
Dim strFranchiseName As String = ""
Dim strProductLineName As String = ""
Dim _SqlAccounts As String = ""

strProductName = "%" & strProductName & "%"
strVendorName = "%" & strVendorName & "%"
strCatalogName = "%" & strCatalogName & "%"
strFranchiseName = "%" & strFranchiseName & "%"
strProductLineName = "%" & strProductLineName & "%"

_GenSql.AppendLine("Select * FROM IMSRejects WHERE")
_GenSql.AppendLine("IMSRejects.ProductDescription LIKE @ProductName
AND")
_GenSql.AppendLine("IMSRejects.VendorName LIKE @VendorName AND")
_GenSql.AppendLine("IMSRejects.CatalogNumber LIKE @CatalogName AND")

' Use the t-sql Coalesce() or isnull() function to
If (Me.cbShowAll.Checked = True) Then
_GenSql.AppendLine("coalesce(IMSRejects.ProductIMS 2Desc,'') LIKE
@FranchiseName AND")
_GenSql.AppendLine("coalesce(IMSRejects.CMA8Desc,' ') LIKE
@ProductLineName")

Else
_GenSql.AppendLine("IMSRejects.ProductIMS2Desc LIKE
@FranchiseName AND")
_GenSql.AppendLine("IMSRejects.CMA8Desc LIKE @ProductLineName")
End If

_GenSql.AppendLine("ORDER BY VendorName ASC, ProductIMS2Desc ASC,
CMA8Desc ASC")

_SqlAccounts = _GenSql.ToString()

_SqlSource.SelectCommand = _SqlAccounts
_SqlSource.SelectParameters.Add("ProductName", strProductName)
_SqlSource.SelectParameters.Add("VendorName", strVendorName)
_SqlSource.SelectParameters.Add("CatalogName", strCatalogName)
_SqlSource.SelectParameters.Add("FranchiseName", strFranchiseName)
_SqlSource.SelectParameters.Add("ProductLineName", strProductLineName)
Feb 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Summa | last post: by
2 posts views Thread by Michael C | last post: by
5 posts views Thread by pwiegers | last post: by
3 posts views Thread by Rahul Babbar | last post: by
reply views Thread by leo001 | last post: by

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.