---
title: "Using if statement to check individual value returned from a query"  
description: "Using if statement to check individual value returned from a query"  
author: "Anonymous User"  
published: 2015-03-25  
updated: 2015-03-25  
canonical: https://www.mindstick.com/forum/23052/using-if-statement-to-check-individual-value-returned-from-a-query  
category: "javascript"  
tags: ["jquery"]  
reading_time: 1 minute  

---

# Using if statement to check individual value returned from a query

I have a seleect query which the return query is used in an [if statement](https://www.mindstick.com/forum/12682/jquery-toggle-if-statement).

The query is correct, but the [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is becuase the [query returns](https://www.mindstick.com/forum/159429/sql-query-returns-a-timeout-expired-error) [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) from [multiple rows](https://www.mindstick.com/forum/160908/how-to-concatenate-text-from-multiple-rows-into-a-single-text-string-in-sql-server) it doesnt work.

I want to use the if statement to [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) individual [values](https://www.mindstick.com/forum/327/sum-textbox-values) retrived by the query.

```
string security = "SELECT ProjectId FROM Project_List WHERE (ProfileId = (SELECT ProfileId FROM User_Profile WHERE (UserId = @UserId)))";using (SqlConnection myConnection = new SqlConnection(connectionString)){    myConnection.Open();    SqlCommand myCommand = new SqlCommand(security, myConnection);    myCommand.Parameters.AddWithValue("@UserId", currentUserId);     if (Request.QueryString["ProjectId"] == myCommand.ExecuteReader().ToString())    {     }    else    {         Response.Redirect("projectlist.aspx");    }}
```

## Replies

### Reply by Pravesh Singh

Instead of ExecuteScalar, which, as it's name implies returns a single value, you have to use ExecuteReader:

```
SqlDataReader reader = myCommand.ExecuteReader();while (reader.Read()){   // grab next record selected   int project_id =(int) reader[0];    // do whatever you want with it   if (Request.QueryString["ProjectId"]== project_id.ToString())   {    }   else   {      
Response.Redirect("projectlist.aspx");   }}
```

The while loop will traverse all records returned by the SELECT [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) and exit as soon as there no more records available.


---

Original Source: https://www.mindstick.com/forum/23052/using-if-statement-to-check-individual-value-returned-from-a-query

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
