2

I was wondering if there was a parameter in SQL for all (not *!) For example, I'm writing a search table now, and if the user does not input something in the text box, it would mean to ignore that specific parameter and display ALL of them for that field. I understand you could make separate OLEDB or SQL commands for each scenario and it would work, but I would just like to do it in one command where if the textbox is empty, I would just ignore it. So far, what this guy said I tried but didn't work... it said I had some type mismatch

http://timothychenallen.blogspot.com/2007/06/sql-server-all-values-parameters-in.html

This is my code for this portion right now

da.SelectCommand = new OleDbCommand("SELECT *
                                       FROM TestQuery
                                      WHERE (VendorName = @VendorName) 
                                        AND CustomerName = @CustomerName", cs);

if (combo_VendorView.Text != "") 
  da.SelectCommand.Parameters.Add("@VendorName", OleDbType.VarChar).Value = combo_VendorView.Text.ToString();
  da.SelectCommand.Parameters.Add("@CustomerName", OleDbType.VarChar).Value = combo_CustomerView.Text.ToString();

  dsB.Clear();
  da.Fill(dsB);
  dgv_DataLookup.DataSource = dsB.Tables[0];

Say if I leave txt.VendorName blank, I want to basically ignore that parameter. Thanks for your help! :)

UPDATED CODE

 da.SelectCommand = new OleDbCommand("SELECT * 
                                        FROM TestQuery 
                                       WHERE (CustomerName = @CustomerName 
                                           OR @CustomerName IS NULL)", cs);
da.SelectCommand.Parameters.Add("@CustomerName", OleDbType.VarChar).Value = combo_CustomerView.Text.ToString();

i'm using ado.net visual studio 2010 if that makes a difference with oledb (access) it does fine searching with parameters but when i do not put the customer name in, it shows only the names of the columns of test query but no information... i want it to basiclly be like select * for this one column

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Wilson Kao
  • 43
  • 2
  • 6
  • duplicate of this http://stackoverflow.com/questions/4745792/sql-cancel-where-on-null-parameter and many others. – gjvdkamp Jul 11 '11 at 13:38
  • 1
    sadly, most people's ways are not working... I googled this problem and the answers that are provided should work but i'm not sure why its not. this is an update of my code – Wilson Kao Jul 11 '11 at 16:19
  • 1
    What is the value of `combo_CustomerView.Text.ToString()`? It certainly isn't `DBNull.Value`, which is what you would want it to be for this to work as you intend. – FishBasketGordo Jul 11 '11 at 16:46
  • possible duplicate of [T-SQL Conditional WHERE Clause](http://stackoverflow.com/questions/4485965/t-sql-conditional-where-clause) – OMG Ponies Jul 11 '11 at 16:46
  • MM first don't know if you are having trouble for this. combo_CustomerView.Text.ToString(). It Should be combo_CustomerView.SelectedItem.Text or combo_CustomerView.SelectedValue if you want the Text of the selected item or the value. And you could try something like this da.SelectCommand.Parameters.Add("@CustomerName", OleDbType.VarChar).Value = combo_CustomerView.SelectedItem.Text == string.Empty ? null : combo_CustomerView.SelectedItem.Text – Hector Sanchez Jul 11 '11 at 16:47
  • 1
    What does `ToString()` return for a Null value? Null or an empty (zero length) string? If the latter, `@CustomerName IS NULL` will never be True. – HansUp Jul 11 '11 at 16:47
  • 2
    @HansUp: `ToString()` doesn't return anything for a null reference; it throws a `NullReferenceException`. – Adam Robinson Jul 11 '11 at 17:17

11 Answers11

1

You can use the COALESCE operator in this example. If your parameter was passed in as null, or you put in logic to convert an empty string to null, you could essentially do this:

SELECT *
FROM TestQuery
WHERE 
   VendorName = COALESCE(@VendorName,VendorName)
   AND CustomerName = COALESCE(@CustomerName, CustomerName)

If VendorName was NULL, it would simply check if VendorName was equal to VendorName, which would always be true.

George Johnston
  • 31,652
  • 27
  • 127
  • 172
  • `VendorName = COALESCE(@VendorName,VendorName)` may force a table scan to occur, rather than allowing an index lookup to happen (because it has to compute the value of the COALESCE expression for each row) – Damien_The_Unbeliever Jul 11 '11 at 13:59
  • Hi, for this error i get IErrorInfo.GetDescription failed with E_FAIL(0x80004005 – Wilson Kao Jul 11 '11 at 14:42
1

Use explicit NULL evaluation like below:

WHERE (Vendorname = @Vendorname OR @Vendorname IS NULL)
AND (CustomerName = @Customername OR @Customername IS NULL)
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
JNK
  • 63,321
  • 15
  • 122
  • 138
  • Hi, thanks for your answer. However, when I do this I end up getting nothing. For example in my code, if I took out the customerName then i want it to display ALL customers but now I have zero customers in this view. – Wilson Kao Jul 11 '11 at 14:02
  • @Wilson - is `@Customername` passed as `NULL` when it's not filled in? – JNK Jul 11 '11 at 14:04
  • No... is it supposed to be? I just tried it and it told me @Parameter @CustomerName has no default value – Wilson Kao Jul 11 '11 at 14:11
  • @Wilson - yes, give it a default value of `NULL`. Then the above `WHERE` clause will work as you like. – JNK Jul 11 '11 at 14:12
  • @Damien - thanks for the edit, must have accidentally hit the code block button without realizing it. – JNK Jul 11 '11 at 14:13
  • :/ it's still the same problem. I even set it when my form loads up combo_CustomerView.Text = null; and when i debugged it the locals windows said selecteditem = null and instead of getting all, I'm getting none – Wilson Kao Jul 11 '11 at 14:26
  • @Wilson - then it's not actually going to `SQL` as a `NULL`...you can modify the code to just `SELECT @Parametername` to see what it is coming across as – JNK Jul 11 '11 at 14:29
  • hey sorry for all the confusion, but i'm not sure if i understand this fully now. for the or statement, it will choose one or the other. so if there is a vendor name it'll pick the vendorname. however, if there is no vendor name, it will set the vendorname parameter to null. thereofore, the select statement will be looking for all vendor names that are equal to null... is this correct? because what i was looking for is that if there is no vendorname then you want to look for all vendornames that are not null (all of them). just wanted to clarify if thats what your code meant before – Wilson Kao Jul 11 '11 at 14:47
  • No. The code means - if you have a `@Vendorname` **PARAMETER** set, match that. If not, then the **PARAMETER** that you are **PASSING TO THE QUERY** will be `NULL`. – JNK Jul 11 '11 at 14:49
1

you probably need a variation on JNK's answer as I think your user input is probably coming through as empty string rather than NULL eg

WHERE (Vendorname = @Vendorname OR @Vendorname = '')
AND (CustomerName = @Customername OR @Customername = '')

This may of course vary from field to field.

El Ronnoco
  • 11,753
  • 5
  • 38
  • 65
1

Is it possible @CustomerName is not equal to NULL but to "" (an empty string)?

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
1
combo_CustomerView.Text.ToString()  // is converting null to ''   

so use:

da.SelectCommand = new OleDbCommand("SELECT * FROM TestQuery WHERE CustomerName = @CustomerName OR @CustomerName = ''", cs);
Saic Siquot
  • 6,513
  • 5
  • 34
  • 56
0

Generic code

where (col1=@param1 or @param1 is null) and (col2=@param2 or @param2 is null) and...
Madhivanan
  • 13,470
  • 1
  • 24
  • 29
0
da.SelectCommand = new OleDbCommand("SELECT * FROM TestQuery WHERE (@VendorName is null or VendorName = @VendorName) AND (@CustomerName is null or CustomerName = @CustomerName)", cs);
da.SelectCommand.Parameters.Add("@VendorName", OleDbType.VarChar).Value = string.isNullOrEmpty(combo_VendorView.Text) ? null : combo_VendorView.Text.ToString();
da.SelectCommand.Parameters.Add("@CustomerName", OleDbType.VarChar).Value = string.IsNullOrEmpty(combo_CustomerView.Text) ? null : combo_CustomerView.Text.ToString();
dsB.Clear();                
da.Fill(dsB);
dgv_DataLookup.DataSource = dsB.Tables[0];
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
0

well, maybe it is not better but it is close

set @VendorName = coalesce(@VendorName,'%')
set @CustomerName = coalesce(@CustomerName,'%')

SELECT * FROM TestQuery 
WHERE VendorName like @VendorName
AND CustomerName like @CustomerName

Now if you send it query (new OleDbCommand("SELECT * FROM TestQuery WHERE (VendorName = @VendorName) AND CustomerName = @CustomerName", cs)) why do you build dynamic the query?

Here an good article about that http://weblogs.sqlteam.com/jeffs/archive/2007/09/18/sql-conditional-where-clauses.aspx

Carlos Cocom
  • 937
  • 1
  • 8
  • 23
0

I don't do much with .NET, but I assume that combo_CustomerView.Text.ToString() returns the string "Null" or the empty string ("") when the combo box is empty. What you really want to do there is assign the NULL literal. In pseudocode:

If combo_box is empty then
    ...Parameters.Add().Value = Null
Else
    ...Parameters.Add().Value = combo_box.ToString()
End If
mwolfe02
  • 23,787
  • 9
  • 91
  • 161
0

The problem is that you're never setting @CustomerName to null. In your code, if the user selects the empty entry in the ComboBox, the @CustomerName parameter will have an empty string as its value.

You need something like this:

if(string.IsNullOrEmpty(combo_CustomerView.Text))
{
    da.SelectCommand.Parameters.Add("@CustomerName", OleDbType.VarChar).Value = DBNull.Value;
}
else
{
    da.SelectCommand.Parameters.Add("@CustomerName", OleDbType.VarChar).Value = combo_CustomerView.Text;
}

Incidentally, please don't call .ToString() on variables that are strings. It's akin to comparing boolean variables to true (or false).

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
0

If the combo box is not selected, why bother with the parameter in your SQL statement's where clause at all? You already have the logic on the Vendor combo box.

JeffO
  • 7,957
  • 3
  • 44
  • 53