Solved

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

Posted on 2014-02-18
8
388 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
  • 3
  • 3
8 Comments
 
LVL 21

Expert Comment

by:Craig Wagner
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 41

Expert Comment

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

Author Comment

by:CEL_IT
Comment Utility
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
Comment Utility
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
Comment Utility
thank you so much craig : )
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now