locked
How to do SEARCH function in Webpage Razor C# RRS feed

  • Question

  • User2006925268 posted

    Good day. I'm having a problem doing a search in my database. and I've got no idea if I'm doing things correctly. I need help please. 

    My Database name is Ft_v1

    The Table is Events... Tables of Events are (Event_ID, Event_Name, Date, Description, Venue_Name)

    The result I'm looking for is how will I be able to display the  (Event_ID, Event_Name, Date, Description, Venue_Name) of the search item I've search... 

    THANK YOU SO MUCH... 

    @{
    var EventSearch = "";
    var searchtype = "0";

    searchtype = Request["formSearchNo"];
    }


    <h2>New Specification</h2>
    <br />

    @{if (IsPost)
    {


    EventSearch
    = "%" + Request["formCustomer"] + "%";

    //Print what is in @EventSearch
    <text>@EventSearch </text>

    //connect to db
    var db = Database.Open("Ft_v1");

    //compile sql query
    var sqlQ = "SELECT * FROM Events where Event_Name like @0";

    //execute SQL query
    var data = db.Query(sqlQ, EventSearch);

    //create grid and display returned sql query data

    var grid = new WebGrid(source: data, defaultSort: "name", rowsPerPage: 3);
    <text>
    @grid.GetHtml(
    tableStyle: "grid",
    headerStyle: "head",
    rowStyle: "row",
    columns: grid.Columns(
    grid.Column(columnName: "Event_ID", header: "Event Name"),
    grid.Column(columnName: "Venue Name", header: "Desc")
    )
    )
    </text>

    }


    else
    {
    <form action="" method="post">
    <p>EventSearch : <input type="text" name="formEventSearch" /></p>
    <p><input type="hidden" name="formSearchNo" value="1" /></p>
    <p><input type="submit" value="Search" /></p>
    </form> }

    if (searchtype == "1")
    {
    <h3>searched for EventSearch</h3>

    }
    }

    Friday, November 16, 2018 11:53 AM

All replies

  • User-821857111 posted

    What exactly is the problem? Have you checked this out: https://www.mikesdotnetting.com/article/180/displaying-search-results-in-a-webgrid ?

    Friday, November 16, 2018 1:00 PM
  • User2006925268 posted

    Hi Mike. Thank you for replying. I'm new in doing Web Razor so I'm quite not sure if my code is correct or not. I actually just want to display all the data of the search input. 

    Friday, November 16, 2018 2:10 PM
  • User-821857111 posted

    I'm quite not sure if my code is correct or not
    Do you get any errors? If not and the results are as you expect, then the code works. If you do get errors, please provide details.

    Saturday, November 17, 2018 8:01 AM