How to do Search in Razor 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... 



    var EventSearch = "";
    var searchtype = "0";

    searchtype = Request["formSearchNo"];

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

    @{if (IsPost)

    = "%" + 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);
    tableStyle: "grid",
    headerStyle: "head",
    rowStyle: "row",
    columns: grid.Columns(
    grid.Column(columnName: "Event_ID", header: "Event Name"),
    grid.Column(columnName: "Venue Name", header: "Desc")


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


    Wednesday, November 14, 2018 10:56 PM

All replies

  • User-271186128 posted

    Hi Merilotcha,


    Inner join in Razor

    I suppose you are using SQL Server database, the inner join syntax as below:

    SELECT column_name(s)
    FROM table1
    INNER JOIN table2 ON table1.column_name = table2.column_name;

    More details, you could refer to the following articles:

    SQL INNER JOIN Keyword

    Besides, you could also refer to the following code in this sample:

        var sql = "Select BookId, Title, ISBN, Description, FirstName, LastName, Category From Books " + 
                   "Inner Join Authors on Books.AuthorId = Authors.AuthorId " + 
                   "Inner Join Categories on Books.CategoryId = Categories.CategoryId";
        var data =  db.Query(sql);

    If it still can't solve your problem, please post the table structure (columns) and which columns you want to select.

    Just one more thing, if you want to post code, you could click the "{;}" (Insert/Edit code sample) menu in the html editor, then insert the code in the popup modal and click the "OK" button. Screenshot as below:

    Best regards,

    Friday, November 16, 2018 2:31 AM
  • User2006925268 posted

    :))) Change to SEarch in razor... 

    Friday, November 16, 2018 3:46 AM
  • User-271186128 posted

    Hi Merilotcha,

    If the original problem is solved and you have a new problem, it is better to close the original thread first, then, create a new thread for the new problem, instead of modifying the original thread. It will be easier for us to find the new problem and help you solve the problem.


    Change to SEarch in razor... 

    As for this issue, you could refer to the following articles:

    Razor Pages with EF Core

    Working with a Database in ASP.NET Web Pages (Razor) Sites

    Displaying Search Results In A WebGrid

    Best regards,

    Monday, November 19, 2018 1:53 AM