Solved

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

Posted on 2014-02-18
8
391 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

16 Experts available now in Live!

Get 1:1 Help Now