473,495 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help With Null Value Problem in asp.net DB Query

9 New Member
I've been working on building a back end on an access database that allows users to search records using a web search box. I've been able to successfully set up simple search query's but have run into trouble where multiple criteria with "AND" statements (OR statements would return too much to be useful). My problem is this, when all five text boxes are filled out the query returns records no problem, but if even one is left blank i get nothing what so ever. Is there any thing I can do in either SQL or VB to either ignore null values entirely or convert them to wildcard values?
Jul 30 '10 #1
9 2655
dascott20
9 New Member
The SQL code is:

Expand|Select|Wrap|Line Numbers
  1. SELECT [Full Title] AS Full_Title, [Author List] AS Author_List, [Title Call Number] AS Title_Call_Number, Series, Location 
  2. FROM Library 
  3. WHERE ([Full Title] LIKE '%' + ? + '%') AND ([Author List] LIKE '%' + ? + '%') AND ([Title Call Number] LIKE '%' + ? + '%') AND (Location LIKE '%' + ? + '%') AND ([Search Terms] LIKE '%' + ? + '%') ORDER BY [Full Title]
Jul 30 '10 #2
Frinavale
9,735 Recognized Expert Moderator Expert
Could you use an OR instead of an AND in your SQL query to get what you're looking for?

If not, I suggest that you build it according to what the user has entered:
Expand|Select|Wrap|Line Numbers
  1. Dim sqlQuery as String = " SELECT [Full Title] AS Full_Title, [Author List] AS Author_List, [Title Call Number] AS Title_Call_Number, Series, Location " _
  2. " FROM Library "_
  3. " WHERE "
  4.  
  5. Dim whereStatement As String
  6. If String.IsNullOrEmpty(txt_Title) = False Then
  7.  whereStatement = "([Full Title] LIKE '%' + ? + '%')"
  8. End If
  9.  
  10. If String.IsNullOrEmpty(txt_AuthorList) = False Then
  11.   whereStatement = " AND ([Author List] LIKE '%' + ? + '%')"
  12. End If
  13.  
  14. '....
The second approach is messy so I suggest you check out the first suggestion first :)

-Frinny
Jul 30 '10 #3
dascott20
9 New Member
@Frinavale
Lol Sorry the first suggestion was the code I am currently using and it doesn't display null values. I'll try this approach though. Messy is ok It has to be easy on the user end not necessarily for me :P. I could use an OR statement but that risks information overload on the user end.
Jul 30 '10 #4
Frinavale
9,735 Recognized Expert Moderator Expert
Perhaps ask the Access Experts how to create a sql query that best suits your needs before pursuing the messy path :)
Jul 30 '10 #5
NeoPa
32,556 Recognized Expert Moderator MVP
Unless you have the ANSI-92 option set in Access you would use '*' instead of '%'.

String concatenation is normally done with & rather than + (although + should work in most cases. See Using "&" and "+" in WHERE Clause).

I'm not sure what the ? means or does in your code, but that would need to be replaced by the value (or Null) of whatever the operator selected.

Otherwise, it seems you have the fundamental concept right already.

Welcome to Bytes!
Jul 31 '10 #6
dascott20
9 New Member
I apologize there seems to be miscommunication. The Access Database is already up and functional. Where I am having trouble is with Asp.net. In Access for a wildcard query if a field is left blank then acess automatically ignores that field and pulls up the rest of the results based on the the others(i.e. if i have four fields for search criteria and leave one blank, Access will ignore that one and search the other three.) whereas if a box is left blank in asp.net it will look for a null value in that field and since that field is not blank in the database that record will be omitted despite the contents of the other three fields. I'm looking to simulate what is going on in Access.

In an idea inspred by Frinavale I am currently trying to get the "SelectCommand=" to change depending on what textboxes actually have a string by using a VB event.

Here's what I have so far:
Expand|Select|Wrap|Line Numbers
  1. Public Class SearchResults2
  2.     Inherits System.Web.UI.Page
  3.     Dim sqlQuery As String
  4.     Dim whereStatement As String
  5.  
  6.  
  7.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  8.  
  9.     End Sub
  10.  
  11.     Private Sub SearchResults2_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreLoad
  12.         Library.SelectCommand = " SELECT [Full Title] AS Full_Title, [Author List] AS Author_List, [Title Call Number] AS Title_Call_Number, [Series] AS Series, [Location] AS Location"
  13.         Library.SelectCommand &= "FROM Library "
  14.         Library.SelectCommand &= "WHERE "
  15.         If Not String.IsNullOrWhiteSpace(TSearch.Text) Then
  16.             Library.SelectCommand &= "([Full Title] LIKE '%' + ? + '%')"
  17.         ElseIf String.IsNullOrWhiteSpace(TSearch.Text) Then
  18.             Library.SelectCommand &= "([Full Title] Like '%')"
  19.  
  20.         End If
  21.  
  22.         If Not String.IsNullOrWhiteSpace(AUSearch.Text) Then
  23.             Library.SelectCommand &= " AND ([Author List] LIKE '%' + ? + '%')"
  24.         ElseIf String.IsNullOrWhiteSpace(AUSearch.Text) Then
  25.             Library.SelectCommand &= " AND ([Author List] Like '%')"
  26.  
  27.         End If
  28.  
  29.         If Not String.IsNullOrWhiteSpace(CNSearch.Text) Then
  30.             Library.SelectCommand &= " AND ([Title Call Number] LIKE '%' + ? + '%')"
  31.         ElseIf String.IsNullOrWhiteSpace(CNSearch.Text) Then
  32.             Library.SelectCommand &= " AND ([Title Call Number] LIKE '%')"
  33.  
  34.         End If
  35.  
  36.         If Not String.IsNullOrWhiteSpace(LSearch.Text) Then
  37.             Library.SelectCommand &= " AND ([Title Call Number] LIKE '%' + ? + '%')"
  38.         ElseIf String.IsNullOrWhiteSpace(LSearch.Text) Then
  39.             Library.SelectCommand &= " AND ([Title Call Number] LIKE '%')"
  40.  
  41.         End If
  42.         If Not String.IsNullOrWhiteSpace(KWSearch.Text) Then
  43.             Library.SelectCommand &= " AND ([Search Terms] LIKE '%' + ? + '%')"
  44.         ElseIf String.IsNullOrWhiteSpace(KWSearch.Text) Then
  45.             Library.SelectCommand &= " AND ([Search Terms] LIKE '%')"
  46.         End If
  47.     End Sub
  48.  
  49.     Private Sub SearchResults2_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  50.         TSearch.Text = Request.QueryString("Title").ToString
  51.         AUSearch.Text = Request.QueryString("Author").ToString
  52.         CNSearch.Text = Request.QueryString("CallNumber").ToString
  53.         LSearch.Text = Request.QueryString("Location").ToString
  54.         KWSearch.Text = Request.QueryString("Key_Words").ToString
  55.  
  56.  
  57.  
  58.     End Sub
  59.  
Aug 2 '10 #7
dascott20
9 New Member
@dascott20
Keep In mind, that I was hired on as a student in net admin, not programming or web development. However this is my tasking at work so I can't hand it off. So please leave clear suggestions (Small words and color pictures appreciated :P).
Aug 2 '10 #8
Frinavale
9,735 Recognized Expert Moderator Expert
What you have should work (are you having problems with it?) but I recommend that you create your query using a StringBuilder instead of concatenating everything into the Library.SelectCommand. The reason I recommend this is because it takes less resources to use a StringBuilder than to concatenate strings together (strings are immutable and cannot be changed so when you add 2 together you are creating a new string...whereas a StringBuilder is better for performance).

Please note that this is not really necessary to accomplish your goal but will speed up your application slightly.

-Frinny
Aug 2 '10 #9
dascott20
9 New Member
This is to let everyone know that I have solved this issue, the answer posted by Frinavale below was very helpful in that process. If you are reading this to solve a simisare problem, below is a solid setup, with two important factors to remember.

1)Declare All your variables.

2)Keep in mind what order you are coding youre commands.
Nov 16 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1275
by: manning_news | last post by:
I've got the following query in SQL 2000: select a.SSN, a.MonthName, a.IMClinicDay, b.IMClinicDay as SecondDay from tblResidentRotations a inner join view7 b on a.SSN = b.SSN where...
26
30881
by: Martin R | last post by:
Hi, How to find first not null value in column whitout chacking whole table (if there is a not null value then show me it and stop searching, the table is quite big)? thx, Martin *** Sent...
0
1296
by: darrylforma | last post by:
What I need is an SQL query that joins three tables. TableA and TableB are joined on Column1 which is common to both. Rows are filtered by Column2 in TableA equalling a specific value...
1
2496
by: RvGrah | last post by:
I have a TabelAdapter that has an int column that allows null (no lectures please, I have my reasons). When I use intellisense to fill in the parameters in the InserQuery method, it shows the...
1
4430
by: djdarpan | last post by:
Hi, I am new with MySql and PHP. I have a problem while I am checking NULL value in database. I need If there is empty field in my table row return message "Somthing "and if there is...
4
10274
by: mwanstall | last post by:
Hi All, I have started tearing my hair out over this problem! I am pulling some data from a table and passing it as variables into a function in Access. One of the variables I'm passing through...
1
3743
by: billa856 | last post by:
Hi, I am trying to insert Null value in column(ShipDate) in my table.That column(ShipDate)'s type id date/time and format is short date. I am using "" to insert Null in that column(ShipDate)...
2
5017
by: wizardry | last post by:
hello - i'm trying to insert a blob into my table, it will insert but the string that i insert when i query the inserted data returns null with 0 bytes in the column. I have other tables set...
0
3044
prabirchoudhury
by: prabirchoudhury | last post by:
CRITERIA; +-------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+-------+...
2
14641
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how...
0
7120
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4583
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.