ODBC Query with multiple conditions gives no results

378 views Asked by At

When querying a 4d database via an odbc system dsn, I am able to get back data from a statement with joins and a single, or no, where condition.

This works:

    static void Main(string[] args)
    {
        OdbcConnection sys4dConn = new OdbcConnection("Dsn=sys4dName");
        try
        {
            OdbcCommand getData = new OdbcCommand("
            SELECT
                Item.Client,
                CAST(Item.xref1 AS VARCHAR),
                CAST(Item.xref2 AS VARCHAR),
                CAST(Item.xref3 AS VARCHAR),
                CAST(Item.xref4 AS VARCHAR),
                Item.owner,
                Item.Storage_Date,
                Item.Temporary_Item_ID,
                Item.ItemType,
                Item.Item_Condition,
                Location.UnitName,
                Part.ConcatPosition,
                Part.Position1,
                Part.Position2,
                Part.Position3,
                Part.Position4,
                Part.Position5,
                Item.Comments,
                Part.Checked_Out_Reason,
                AuditTrail.ReasonforChange,
                '123abc' as CheckoutStatus 
            FROM
                Item 
                LEFT JOIN
                    Part 
                    ON Item.PK_ItemUID = Part.FK_ItemUID 
                LEFT JOIN
                    AuditTrail 
                    ON Part.PK_PartUID = AuditTrail.FK_PartUID 
                    AND 
                    (
                        Part.Position1 IS NOT NULL 
                        AND Part.Position2 IS NOT NULL 
                        AND Part.Position3 IS NOT NULL 
                        AND Part.Position4 IS NOT NULL 
                        AND Part.Position5 IS NOT NULL 
                    )
                LEFT JOIN
                    Location 
                    ON Part.FK_UnitSectID = Location.PK_UnitSectID 
            WHERE
                Item.Client = '4468'", sys4dConn);

            getData.CommandType = CommandType.Text;
            OdbcDataAdapter daGetData = new OdbcDataAdapter(getData);

            using (DataTable dtTbl = new DataTable())
            {
                sys4dConn.Open();
                OdbcDataReader dr = getData.ExecuteReader();
                while (dr.Read())
                {
                    DataRow dtRow = dtTbl.NewRow();
                    for (int i = 0; i < dtTbl.Columns.Count; i++)
                    {
                        dtRow[i] = dr[i].ToString();
                    }
                    dtTbl.Rows.Add(dtRow);
                }
                sys4dName.Close();
            }
        }
    }

(The query text has been formatted for this post. The actual string has only spaces, no carriage returns or line feeds.)

This also works:

         ...WHERE
                Item.Client IN ('4468')", sys4dConn);

With each of the above, the query returns 12 rows of data. If I change the client to '7147', I get 14 rows of data.

When I want to pull both in one dataset, though, I get zero rows.

None of these work:

         ...WHERE
                Item.Client = '4468'
                OR Item.Client = '7147'", sys4dConn);


         ...WHERE
                Item.Client IN ('4468','7147')", sys4dConn);


         ...WHERE
                Item.Client = ?
                OR Item.Client = ?", sys4dConn);
            getData.Parameters.AddWithValue("client", "4468");
            getData.Parameters.AddWithValue("client", "7147");

The last one throws the following:

An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Is there some syntax that will allow me to include more than one condition in the where clause of my odbc query?

A version of the query with all of the joins removed does return the 26 rows using

         ...WHERE
                Item.Client = '4468'
                OR Item.Client = '7147'", sys4dConn);
0

There are 0 answers