0

I have a basic html page that I want to display a table on, populated from a SQL query.

I want to display all part numbers that are classified as essential items, that are out of stock.

I'm using CSHTML, razor syntax on webmatrix. I tried this:

var nilstock = db.QueryValue("select STOCKCODE, TOTALSTOCK from dbo.STOCK_ITEMS where STOCK_CLASSIFICATION in(170,190,200) and TOTALSTOCK <1 order by STOCKCODE");

I then call this var later, by@nilstock.ToString() or even just @nilstick but it returns only the first affected cell.

Any help gratefully accepted..:)

5
  • I'm not familiar with this stack but it seems to me like QueryValue would return a single value instead of a full result set. Commented Dec 11, 2013 at 22:49
  • OK, so should i just have query? Commented Dec 11, 2013 at 22:50
  • Try that and see what it does. Commented Dec 11, 2013 at 22:50
  • No. Still gives me a single result: the first part number in the results. Commented Dec 11, 2013 at 22:57
  • Whoops, correct response : now runs error: System.Collections.ObjectModel.ReadOnlyCollection`1[System.Object] Commented Dec 11, 2013 at 23:09

2 Answers 2

1

You need to change your query from a single value to return a whole dataset, then iterate through the items as in Stefan's answer. The whole chunk of code should look like this:

@{
    // Open database connection etc
    var nilstock = db.Query("select STOCKCODE, TOTALSTOCK from dbo.STOCK_ITEMS where STOCK_CLASSIFICATION in(170,190,200) and TOTALSTOCK <1 order by STOCKCODE");
}

<table>
@foreach(var row in nilstock)
{
    <tr>
        <td>@row.STOCKCODE</td>
        <td>@row.TOTALSTOCK</td>
    </tr>
}
</table>
Sign up to request clarification or add additional context in comments.

4 Comments

Yes! Thanks also to @stefan for his help
@jraede we've got our answer - you weren't far off!
@Bevan Yeah, it was the two answers together that you needed! For reference, your database command methods are db.Execute() for things that return nothing but number of rows affected (inserts, etc), db.QueryValue() for queries that return a single value from a single row, db.QuerySingle() that returns a single row and, as in this answer, db.Query() which can return any number of fields and any number of rows.
Thanks! Good follow thru. I'll store those, coz I'll be bound to need them soon!
1

Not that familiar with how you query the db, but you probably have iterate over the result set like this:

<table>
    @foreach(var n in nilstock)
    {
        <tr><td>@n.STOCKCODE</td><td>@n.TOTALSTOCK</td></tr>
    }
<table>

2 Comments

Hmmm... Now it crashes with reason: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'char' does not contain a definition for 'STOCKCODE'
I'm sure you're on the right track though, because it's trying to do it, there just seems to be some declaration bug somewhere...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.