0

I am using Webmatrix with VB.NET. I have a database declared as

Dim db As Database
db = Database.Open("xxx")

I build this query:

Dim strQuery As String
strQuery = "SELECT NomexSheeter.BlockNumber, NomexSheeter.WorkOrder, dbo_tbl_WIPMASTER_NF.ITEM_NBR " _
            & "FROM NomexSheeter INNER JOIN dbo_tbl_WIPMASTER_NF ON NomexSheeter.WorkOrder = dbo_tbl_WIPMASTER_NF.WORK_ORDER " _
            & "WHERE NomexSheeter.WorkOrder = @0 AND NomexSheeter.BlockNumber = @1"

I call the query like this:

Dim data = db.Query(strQuery, "99A", "901C")

Here is some markup...

<input type="hidden" id="barcodeValue" value="@data.ITEM_NBR"/>

I get an compilation error when running the page that says: 'ITEM_NBR' is not a member of 'System.Collections.Generic.IEnumerable(Of Object)'.

Since there is very little VB.NET Webmatrix documentation online, I am left guessing.

1 Answer 1

1

Your return type is a collection. It is an IEnumerable(Of Object). To access properties of individual elements in the collection, you either have to use ElementAt() to access a specific item or a For Each loop to access each element in turn.

Dim data = db.QuerySingle(strQuery, "99A", "901C")
<input type="hidden" id="barcodeValue" value="@data.ElementAt(0).ITEM_NBR"/>

Or you could use the First method to get to the first row if you are certain at least one will be returned:

Dim data = db.QuerySingle(strQuery, "99A", "901C")
<input type="hidden" id="barcodeValue" value="@data.First().ITEM_NBR"/>

If you only expect one row of data to match your criteria, you should use the QuerySingle method instead of the Query method. That returns an object, and you can access its properties directly.

Dim data = db.QuerySingle(strQuery, "99A", "901C")
<input type="hidden" id="barcodeValue" value="@data.ITEM_NBR"/>

There is more about hte various Database methods and their return types in this C#-focused article of mine, but you should be able to understand why each return type needs to be treated differently: http://www.mikesdotnetting.com/Article/214/How-To-Check-If-A-Query-Returns-Data-In-ASP.NET-Web-Pages

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mike! That makes perfect sense! I will try it.

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.