0

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.
4
  • I'm not sure I follow, the SQL Query you posted doesn't do this: which will display sql data in gridview when "AssetType" column has data, "IssuedOn" column has data and "ReturnedOn" column doesnt have any data.. What's the filter you need to apply exactly? Commented May 28, 2020 at 5:22
  • I need a query which will display data when, AssetType <> "Laptop" or "Desktop" and IssuedOn = "any data" and ReturnedOn=null. Commented May 28, 2020 at 5:31
  • Just add these criteria to your sql query: SELECT * FROM AssetRequest WHERE (ReturnedOn IS NULL OR ReturnedOn ='') AND (IssuedOn IS NOT NULL) AND (AssetType NOT IN ('Laptop', 'Desktop')) ORDER BY id DESC Commented May 28, 2020 at 6:20
  • @TWP, sorry i think i haven't made myself clear enough, the 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. Commented May 28, 2020 at 10:12

3 Answers 3

1

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 = ''))
Sign up to request clarification or add additional context in comments.

Comments

0

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= ''

8 Comments

Thanks. I tried the code. But there is no data displayed in gridview, it returns empty
Does it display the records you wish when using this query in SSMS? Just to make sure the query is returning correct results
Put brackets around the OR-conditions in both union parts: (ReturnedOn is null or ReturnedOn ='')
Now i am getting the error "The data type text cannot be used as an operand to the UNION, INTERSECT or EXCEPT operators because it is not comparable"
@santosh you are using TEXT datatype, instead use nvarchar(max)
|
0

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 = '')

1 Comment

Thanks. But it doesn't hide the record when Assettype and Issuedon column has data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.