locked
Count cs# razor code error RRS feed

  • Question

  • User1519602350 posted

    I have more than 15 rows but the below query and code showing always one

    @{
         var sqlBannerCountOne="SELECT COUNT(PageUrl) as pageCount from Banner WHERE PageUrl = 'search'";
           var bannerCountOne = dbs.Query(sqlBannerCountOne);
     
            if (bannerCountOne.Count() < 2){
                   @bannerCountOne.Count()
                }
                
                else{
                    <p>two</p>
     
                }    
     
    }
    Friday, August 2, 2013 7:10 AM

Answers

  • User281315223 posted

    It's important to realize what you are actually selecting in your statement is the count of rows that appear and not the actual rows themselves, so when the following SQL executes : 

    SELECT COUNT(PageUrl) AS pageCount 
      FROM Banner 
     WHERE PageUrl = 'search'

    It's going to yield a single integer (the number of rows with PageUrl = 'search') and not the actual rows themselves. This is why your the Count method is always returning a single value.

    If you want to return all of the rows and use the Count method to get the number of rows as a result, you'll need to just select the rows simlar to below : 

    SELECT *
      FROM Banner 
     WHERE PageUrl = 'search'

    This will return a collection of rows within your Query and the Count method should then indicate the number of rows returned.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2013 7:52 AM
  • User-821857111 posted

    You have a number of partial answers here, so to round them up and provide one complete answer, here goes:

    Your SQL gets the total number of rows where PageUrl = "search". 

    When you retrieve that from the database, you use the Query method, which returns a collection. In this case, the collection will only ever have one item it it - the result of your query which is the total number of rows where PageUrl = "search" as already stated.

    bannerCountOne.Count() gives you the total number of items in the collection (or rows in the result) which is 1 - as you have discovered and for the reason described. If you continue to use the Query method (not advised), you will have to use the following syntax to get at the value returned by your query:

    var count = bannerCountOne.First().pageCount;
    if(count < 2){
        /etc
    }

    If you only return a scalar (single) value from a query, you should use QueryValue which returns an object:

    var bannerCountOne = db.QueryValue(sqlBannerCountOne);

    Then you can test that value:

    if(bannerCountOne < 2){
        // etc
    } 



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 5, 2013 9:12 AM

All replies

  • User197322208 posted

    WHERE PageUrl = 'search'";

    Do you want to find the word

    'search'

    ?

    Friday, August 2, 2013 7:36 AM
  • User1519602350 posted

    yes

    Friday, August 2, 2013 7:40 AM
  • User281315223 posted

    It's important to realize what you are actually selecting in your statement is the count of rows that appear and not the actual rows themselves, so when the following SQL executes : 

    SELECT COUNT(PageUrl) AS pageCount 
      FROM Banner 
     WHERE PageUrl = 'search'

    It's going to yield a single integer (the number of rows with PageUrl = 'search') and not the actual rows themselves. This is why your the Count method is always returning a single value.

    If you want to return all of the rows and use the Count method to get the number of rows as a result, you'll need to just select the rows simlar to below : 

    SELECT *
      FROM Banner 
     WHERE PageUrl = 'search'

    This will return a collection of rows within your Query and the Count method should then indicate the number of rows returned.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2013 7:52 AM
  • User895691971 posted

    1. Why not select all rows? 

    SELECT * FROM column WHERE this_column = this_value

    Secondly you can also try using params. Like

    var type = "search";
    ("SELECT * as pageCount from Banner WHERE PageUrl =@0", type);

    Friday, August 2, 2013 8:06 AM
  • User1546138644 posted

    You are returning the count in a column called pageCount, so you need to interrogate that colum to get your value, using bannerCountOne.pageCount

     

    Friday, August 2, 2013 8:06 AM
  • User38443587 posted

    Hello, From your code it should show 1 always, because bannerCountOne equal to 1 forever just like Rion William said. You can change your code.

    From:

    if (bannerCountOne.Count() < 2){
    	               @bannerCountOne.Count()
    	            }
    

    To:

    if (bannerCountOne.AsInt()< 2){ 
        @bannerCountOne.AsInt()
     }
    

    Thank you.

    Monday, August 5, 2013 5:00 AM
  • User-821857111 posted

    You have a number of partial answers here, so to round them up and provide one complete answer, here goes:

    Your SQL gets the total number of rows where PageUrl = "search". 

    When you retrieve that from the database, you use the Query method, which returns a collection. In this case, the collection will only ever have one item it it - the result of your query which is the total number of rows where PageUrl = "search" as already stated.

    bannerCountOne.Count() gives you the total number of items in the collection (or rows in the result) which is 1 - as you have discovered and for the reason described. If you continue to use the Query method (not advised), you will have to use the following syntax to get at the value returned by your query:

    var count = bannerCountOne.First().pageCount;
    if(count < 2){
        /etc
    }

    If you only return a scalar (single) value from a query, you should use QueryValue which returns an object:

    var bannerCountOne = db.QueryValue(sqlBannerCountOne);

    Then you can test that value:

    if(bannerCountOne < 2){
        // etc
    } 



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 5, 2013 9:12 AM
  • User-821857111 posted

    if (bannerCountOne.AsInt()< 2){ 
        @bannerCountOne.AsInt()
     }

    That won't even compile. bannerCountOne is an IEnumerable. AsInt() is an extension method on the string type.

    Monday, August 5, 2013 9:15 AM
  • User-821857111 posted

    Secondly you can also try using params

    There's no need to use parameters if you know the value you want to filter on.

    Monday, August 5, 2013 9:17 AM