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)