locked
How to display a count of search results with keywords in a label? RRS feed

  • Question

  • User963835749 posted

    Sorry for a basic post here; I am very new to VB!

    I am currently building a search page, which includes a textbox and two drop-down lists, a search button, and a datagrid. If results are found and displayed, I would like a label to display:

    (e.g.) "24 results found for 'blue sky clouds' "

    ...where the integer represents the results count, and the keywords and/or drop-down list selections are included in the string.

    If no results are found I would like the label to display:

    (e.g.) "No results were found that match your selection. Please try again"

    I would also like the label to be invisible until a search occurs.

    Thanks in advance!


    Monday, March 8, 2010 6:00 PM

Answers

  • User-725220842 posted

    I would first like to give you the steps on how to do it. If you face any difficulties I shall assist you.

    1. Query the database from within the application & fetch the results for the desired search keyword (here - blue sky clouds).
    2. Count the number of rows that have been returned as a result to the query.
    3. If the number of rows is > 0 then the number is your result count. Now we set the label's text to the message you require i.e. 24 results found for the keyword blue sky clouds.
      lblResults.text = no_of_rows.ToString+" results found for the keyword "+txtKeyword.text
    4. If the number of rows is 0 we write the error message as requested.
      lblResults.text = "No results found for the keyword "+txtKeyword.text

    To make the label invisible add this attribute to the declaration of the label "visible = false"

    And when you wish to display the message ensure that the label is visible by typing

    lblResults.visible = true

    Hope that helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 8, 2010 11:45 PM
  • User963835749 posted

    Many thanks for your reply, SanjitN. I based part of my solution on your post, and also managed to get some additional pointers from a friend - I thought it might be helpful to others to post the final solution here:

    lbSearchResults.InnerHtml = CStr(SearchResults.Rows.Count) & " result" & IIf(SearchResults.Rows.Count = 1, " has", "s have") & " been found for <strong>" & Server.HtmlEncode(tbSearch.Text) "</strong>"

    The above displays the following if there is 1 result:

    "1 result has been found for keyword"

    If more than 1 result has been found, the sentance is altered:

    "1 results have been found for keyword"

    The keyword is displayed in bold text. The InnerHtml part was recommended to me to allow the search to convert html symbols in the search field. However, I do seem to be able to crash my site if I enter <b> here. I may post a separate help post for this!



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 11, 2010 9:53 AM

All replies

  • User-725220842 posted

    I would first like to give you the steps on how to do it. If you face any difficulties I shall assist you.

    1. Query the database from within the application & fetch the results for the desired search keyword (here - blue sky clouds).
    2. Count the number of rows that have been returned as a result to the query.
    3. If the number of rows is > 0 then the number is your result count. Now we set the label's text to the message you require i.e. 24 results found for the keyword blue sky clouds.
      lblResults.text = no_of_rows.ToString+" results found for the keyword "+txtKeyword.text
    4. If the number of rows is 0 we write the error message as requested.
      lblResults.text = "No results found for the keyword "+txtKeyword.text

    To make the label invisible add this attribute to the declaration of the label "visible = false"

    And when you wish to display the message ensure that the label is visible by typing

    lblResults.visible = true

    Hope that helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 8, 2010 11:45 PM
  • User963835749 posted

    Many thanks for your reply, SanjitN. I based part of my solution on your post, and also managed to get some additional pointers from a friend - I thought it might be helpful to others to post the final solution here:

    lbSearchResults.InnerHtml = CStr(SearchResults.Rows.Count) & " result" & IIf(SearchResults.Rows.Count = 1, " has", "s have") & " been found for <strong>" & Server.HtmlEncode(tbSearch.Text) "</strong>"

    The above displays the following if there is 1 result:

    "1 result has been found for keyword"

    If more than 1 result has been found, the sentance is altered:

    "1 results have been found for keyword"

    The keyword is displayed in bold text. The InnerHtml part was recommended to me to allow the search to convert html symbols in the search field. However, I do seem to be able to crash my site if I enter <b> here. I may post a separate help post for this!



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 11, 2010 9:53 AM
  • User-725220842 posted

    Innocent Well technically if you based a part of your solution on my answer shouldn't I get marked as answer too?? Tongue out

    Now you may send <b> tags directly as label text like mentioned below instead of using InnerHTML.

    lblText.Text = "<b>Hello World </b>";

    Outputs Hello World

    Hope that helps.

    Thursday, March 11, 2010 11:21 PM
  • User963835749 posted

    Of course, sorry! Didn't realise you could mark multiple posts as answers!

    Now you may send <b> tags directly as label text like mentioned below instead of using InnerHTML.

    lblText.Text = "<b>Hello World </b>";

    Outputs Hello World


    The site crashes if I enter symbols (e.g. < ) in the search textbox (to run a search on the attached database). I get the following error when this happens. Any ideas?!

    Server Error in '/' Application.

    A potentially dangerous Request.Form value was detected from the client (ctl00$MainContent$tbSearch="<b>").

    Description: Request Validation has detected a potentially dangerous client input value, and processing of the request has been aborted. This value may indicate an attempt to compromise the security of your application, such as a cross-site scripting attack. You can disable request validation by setting validateRequest=false in the Page directive or in the <pages> configuration section. However, it is strongly recommended that your application explicitly check all inputs in this case.

    Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (ctl00$MainContent$tbSearch="<b>").</pages>

    Saturday, March 13, 2010 12:08 PM
  • User-725220842 posted

    You cannot however inject HTML into Buttons or textboxes. They are a security concern. Hence the site crashed. You can use the code I mentioned only in label controls & not textboxes.

    Wednesday, March 17, 2010 12:25 AM
  • User963835749 posted

    You cannot however inject HTML into Buttons or textboxes. They are a security concern. Hence the site crashed. You can use the code I mentioned only in label controls & not textboxes.

     

    I don't wish to input HTML here, but a user of the site might try / do so by accident... How do I get the site to simply ignore these symbols and not crash in a spectacular fashion? I already have a RegularExpressionValidator to only accept A-Za-z0-9 -/ characters (which should prevent symbols such as : ; < = > ?), however sometimes the validator still allows you past if you click enough times!!

    Wednesday, March 24, 2010 7:44 AM
  • User963835749 posted

    You cannot however inject HTML into Buttons or textboxes. They are a security concern. Hence the site crashed. You can use the code I mentioned only in label controls & not textboxes.

     

    I don't wish to input HTML here, but a user of the site might try / do so by accident... How do I get the site to simply ignore these symbols and not crash in a spectacular fashion? I already have a RegularExpressionValidator to only accept A-Za-z0-9 -/ characters (which should prevent symbols such as : ; < = > ?), however sometimes the validator still allows you past if you click enough times!!

    Wednesday, March 24, 2010 7:44 AM
  • User963835749 posted

    You cannot however inject HTML into Buttons or textboxes. They are a security concern. Hence the site crashed. You can use the code I mentioned only in label controls & not textboxes.

     

    I don't wish to input HTML here, but a user of the site might try / do so by accident... How do I get the site to simply ignore these symbols and not crash in a spectacular fashion? I already have a RegularExpressionValidator to only accept A-Za-z0-9 -/ characters (which should prevent symbols such as : ; < = > ?), however sometimes the validator still allows you past if you click enough times!!

    Wednesday, March 24, 2010 7:45 AM
  • User963835749 posted

    You cannot however inject HTML into Buttons or textboxes. They are a security concern. Hence the site crashed. You can use the code I mentioned only in label controls & not textboxes.

     

    I don't wish to input HTML here, but a user of the site might try / do so by accident... How do I get the site to simply ignore these symbols and not crash in a spectacular fashion? I already have a RegularExpressionValidator to only accept A-Za-z0-9 -/ characters (which should prevent symbols such as : ; < = > ?), however sometimes the validator still allows you past if you click enough times!!

    Wednesday, March 24, 2010 7:45 AM
  • User-725220842 posted

    You may handle the System.Web.HttpRequestValidationException (try..catch) in your code to handle such inputs.


    Wednesday, March 24, 2010 8:05 AM