nora February 2016

Last 'OR' statement Ignored in OLEDB command

I'm trying to filter a OLEDB command by having multiple 'OR' statements work on a single database field but the last in the chain gets ignored and doesn't return anything.

I have a work around for now with a @null request that returns "DBNull.value" but if I remove "[doctype] = @null" from below it ignores "[doctype] = doctype3"

& "WHERE [doctype] = @doctype OR [doctype] = @doctype2 OR [doctype] = @doctype3 OR [doctype] = @null " _

I can increase or decrease the 'OR Statements' as I like but it is always the last 'OR Statement' that is ignored.

I've tried putting the 'OR statements' in brackets but then it returns nothing, or I'm doing it wrong.

I'm looking for simply why the last 'or statement' is ignored in the oledbcommand string but if you can improve any code I've wrote please do, but explain to me why/how.

Where they are used (Every other value has DBNull.value for testing purposes)

            f_doctype = "MS"
            f_doctype2 = "TMS"
            f_doctype3 = "CS"

            With cmdaRefresh.Parameters
            .AddWithValue("doctype", CType(f_doctype, String))
            .AddWithValue("doctype2", CType(f_doctype2, String))
            .AddWithValue("doctype3", CType(f_doctype3, String))
            .AddWithValue("null", DBNull.Value)
            .AddWithValue("docnum", DBNull.Value)
            .AddWithValue("docrev", DBNull.Value)
            .AddWithValue("matname", DBNull.Value)
            .AddWithValue("status", DBNull.Value)
            .AddWithValue("actionreq", DBNull.Value)
            .AddWithValue("createdby", DBNull.Value)
            .AddWithValue("createddate", DBNull.Value)
            .AddWithValue("finalby", DBNull.Value)
            .AddWithValue("finaldate", DBNull.Value)
            End With

        Dim cmdRefresh As New OleDbDataAdapter(cmdaRefresh)

        'open connection
        myconnection.Open()

        'read and fill dataset for        

Answers


Mark February 2016

You need to use IS NULL to check for null values (in most, perhaps all, databases, null does not equal null, so you need to use the IS operator instead). This also means you don't need all those parameters.

Depending on how you intend to combine the [doctype] IS NULL check with the other null checks, you may also need to wrap the ORs in parentheses, since AND may have higher precedence than OR.

Dim cmdaRefresh As OleDbCommand = New OleDbCommand(
    " SELECT [doctype], [docnum], [docrev], [matname], [status], [actionreq], [createdby], [createddate], [finalby], [finaldate] " _
    & "FROM " & tbl_string.tablename & " " _
    & "WHERE ([doctype] = @doctype OR [doctype] = @doctype2 OR [doctype] = @doctype3 OR [doctype] IS NULL) " _
    & "AND [docnum] IS NULL " _
    & "AND [docrev] IS NULL " _
    & "AND [matname] IS NULL " _
    & "AND [status] IS NULL " _
    & "AND [actionreq] IS NULL " _
    & "AND [createdby] IS NULL " _
    & "AND [createddate] IS NULL " _
    & "AND [finalby] IS NULL " _
    & "AND [finaldate] IS NULL " _
    & "ORDER BY [docnum] ASC, [docrev] ASC " _
    , myconnection)

If the[doctype] IS NULL check belongs with the other NULL checks then you don't need the parens.


nora February 2016

Found the reason why it wasn't working. Somewhere during my search I read that DBNull.value would return anything from the database when used with a .addwithvalue statement (which might of confused the whole thing). Which is not correct, it returns anything that has a value of nothing as in no white space or value. Which is why [doctype] IS NULL gave the same issue as above, it does the same thing, I think.

What I was looking for was a way to search through my [doctype]'s and return any value from the other database fields without having to specify anything 'for testing'

The statement I was looking for was [docnum] LIKE '%' this is called a Wildcard. A wildcard character can be used to substitute for any other character(s) in a string, so that mean anything is returned. However if the database field is of value Null then a IS NULL statement is still require, hence my [finalyby] & [finaldate] alterations below.

What I've came up with now is:

'CHANGED (dataadapter > command)
    Dim cmdaRefresh As OleDbCommand = New OleDbCommand(" SELECT [doctype], [docnum], [docrev], [matname], [status], [actionreq], [createdby], [createddate], [finalby], [finaldate] " _
                                                              & "FROM " & tbl_string.tablename & " " _
                                                              & "WHERE [doctype] IN (@doctype, @doctype2 ,@doctype3) " _
                                                              & "AND [docnum] LIKE '%' " _
                                                              & "AND [docrev] LIKE '%' " _
                                                              & "AND [matname] LIKE '%' " _
                                                              & "AND [status] LIKE '%' " _
                            

Post Status

Asked in February 2016
Viewed 1,800 times
Voted 14
Answered 2 times

Search




Leave an answer