Solved

how to pass database results from a class in asp.net web pages

Posted on 2014-02-18
8
396 Views
Last Modified: 2014-02-21
Hello,

I can run sql doing the following on a page:

var db = Database.Open("DefaultConnection");
var selectBrands = db.Query("SELECT DISTINCT SubDescription1 AS Brands FROM dbo.Item");
db.Close();
@foreach (var row in selectBrands )
{
<li>row.Brands</li>
}

What I want to do is run the sql query within a function in a class. The function will return the data which I am trying to iterate through. I have attached text file with the code.

I get the following error:

Error      19      'object' does not contain a definition for 'Brands' and no extension method 'Brands' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

What I am trying to do is separate markup from logic by setting up classes, is this the right way of going about it?
0
Comment
Question by:CEL_IT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
8 Comments
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39868024
Are you using ASP.NET MVC? The "@" in your view makes it look like you're using Razor syntax.

If using MVC you should retrieve the data in your Controller and then pass it to the View as a strongly-typed Model.

There is no attached file.
0
 

Author Comment

by:CEL_IT
ID: 39868029
Sorry about the attachement, I am not using MVC but Web Page Razor V3.
test.txt
0
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39868181
1. Your Menus class shouldn't implement IEnumerable. You're not enumerating over the class itself, you're enumerating over the collection that one of the methods returns.

2. Your getBrands method returns IEnumerable. When you iterate over an IEnumerable everything you get back is of type "object", because the collection could contain any valid type from the .NET type system so it has to return the highest-level type that could represent anything.

3. If an exception occurs during the Open() or Query() you will never close your database connection.

I don't have access to WebMatrix so I can't test that this will work, but you might try coding your class as follows:

public class Menus
{
    public IEnumerable<dynamic> getBrands()
    {
        using( var db = Database.Open("DefaultConnection") )
        {
            var selectBrands = db.Query("SELECT DISTINCT SubDescription1 AS Brands FROM dbo.Item");

            return selectBrands;
    }
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Expert Comment

by:guru_sami
ID: 39868211
Just wondering isn't using WebPages means there isn't any code-behind but you do inline coding?
0
 

Author Comment

by:CEL_IT
ID: 39869724
Craig,

that's worked perfect! thank you for the explanations as well. Do we need to close the database connection inside the using as below?

public class Menus
{
    public IEnumerable<dynamic> getBrands()
    {
        using( var db = Database.Open("DefaultConnection") )
        {
            var selectBrands = db.Query("SELECT DISTINCT SubDescription1 AS Brands FROM dbo.Item");

            db.Close();
            return selectBrands;
    }
}

Open in new window

0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39870101
No, you don't need to close the database connection. When you exit the "using" block the connection will be closed and disposed automatically.
0
 

Author Closing Comment

by:CEL_IT
ID: 39870883
thank you so much craig : )
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question