locked
displaying single cell from table RRS feed

  • Question

  • User-2060576634 posted

    hi everyone, how can we display a single cell from a database table instead of using foreach loop for displaying all of the rows? can anyone give an example please?

    Wednesday, January 14, 2015 2:57 PM

Answers

  • User1853794821 posted
    VAR db = DataBase.Open("starterSite");
    var result=db.Query("SELECT ContactName FROM costumers WHERE CustomerId=@0",3);
    @result.ContactName

    In my example above I assumed you intentionally named your table "costumers".

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 14, 2015 4:08 PM
  • User1853794821 posted

    Try "QuerySingle" instead of "Query".

    "Query" returns a set of rows; "QuerySingle" returns exactly one row.

    The most commonly used kinds of query you can make are:

    (1)  Query

    (2) QuerySingle

    (3) QueryValue.

    See http://msdn.microsoft.com/en-us/library/webmatrix.data.database_methods(v=vs.111).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 14, 2015 4:54 PM

All replies

  • User-735851359 posted

    Hello,

    You can get only the necessary record, you must provide the ID:

        var id = UrlData[0];
        var data = Database.Open("db");
        var sqlQ = "SELECT * FROM Items WHERE ItemID = @0";
        var info = data.QuerySingle(sqlQ,id); 

    On the page, you can show it as follows:

    @info.Title

    Wednesday, January 14, 2015 3:08 PM
  • User379720387 posted

    If "Title" was the column you are interested in I would not query for all the columns.

    SELECT Title FROM Items WHERE ItemID=@0

    var info = dbQuery.Value(....

    Wednesday, January 14, 2015 3:23 PM
  • User-2060576634 posted

    i still don't get it completely.. suppose the following image is our table named "costumers" and our database name is "startersite". how do we display the highlighted cell?

    Wednesday, January 14, 2015 4:03 PM
  • User1853794821 posted
    VAR db = DataBase.Open("starterSite");
    var result=db.Query("SELECT ContactName FROM costumers WHERE CustomerId=@0",3);
    @result.ContactName

    In my example above I assumed you intentionally named your table "costumers".

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 14, 2015 4:08 PM
  • User-2060576634 posted

    i use this code:

    @{
    var
    db = Database.Open("Azmoon");
    var soal =db.Query("SELECT Question FROM BarExam1Q WHERE QuestionId=@0",1);

    }
    @soal.Question

    but i receive this error:

    Compiler Error Message: CS1061: 'System.Collections.Generic.IEnumerable<dynamic>' does not contain a definition for 'Question' and no extension method 'Question' accepting a first argument of type 'System.Collections.Generic.IEnumerable<dynamic>' could be found (are you missing a using directive or an assembly reference?)

    Wednesday, January 14, 2015 4:29 PM
  • User1853794821 posted

    Try "QuerySingle" instead of "Query".

    "Query" returns a set of rows; "QuerySingle" returns exactly one row.

    The most commonly used kinds of query you can make are:

    (1)  Query

    (2) QuerySingle

    (3) QueryValue.

    See http://msdn.microsoft.com/en-us/library/webmatrix.data.database_methods(v=vs.111).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 14, 2015 4:54 PM