santosh
santosh

Reputation: 71

SQL query to display data in gridview

I have a grid view that displays data from a SQL Server database during page load. My gridview contains these columns:

AssetType, IssuedOn, ReturnedOn

I have used a query:

SqlCommand cmd = new SqlCommand(
    "SELECT * FROM AssetRequest WHERE ReturnedOn IS NULL OR ReturnedOn ='' ORDER BY id DESC",
    conn);

which will display data from SQL Server in the gridview when ReturnedOn column doesn't have any data.

The new query has to satisfy these conditions,

  1. It Should not displays a record when Assetype="Laptop" or "Desktop" and IssuedOn is not empty.
  2. But if the Assetype="Laptop" or "Desktop" and IssuedOn is empty it should display the record.
  3. If the Assetype=anything and Returnedon is not empty it should not display that record in gridview.

Upvotes: 0

Views: 1540

Answers (3)

Gaurav Patel
Gaurav Patel

Reputation: 41

I hope this query resolves your issue.

SELECT *
FROM AssetRequest
WHERE (AssetType IN ('Laptop', 'Desktop') AND (IssuedOn IS NULL OR IssuedOn = '') AND (ReturnedOn IS NULL OR ReturnedOn = '')) 
OR (AssetType NOT IN ('Laptop', 'Desktop') AND (ReturnedOn IS NULL OR ReturnedOn = ''))

Upvotes: 1

TWP
TWP

Reputation: 270

According to your conditions above, this should meet your requirements:

select * 
from AssetRequest 
where 
  ((IssuedOn IS NULL OR IssuedOn = '') AND (Assetype IN ('Laptop', 'Desktop')) OR
  (ReturnedOn IS NULL OR ReturnedOn = '')

Upvotes: 0

Muhammed Nigil
Muhammed Nigil

Reputation: 183

I hope this would help

select * 
from AssetRequest 
where (ReturnedOn is null or ReturnedOn ='' )
and Assetype not in ('Laptop','Desktop')
union
select * 
from AssetRequest 
where Assetype in ('Laptop','Desktop') and IssuedOn is null or IssuedOn= ''

Upvotes: 0

Related Questions