Solved

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

Posted on 2014-02-18
8
393 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax Error 2 64
SQL Exceptions 3 45
IIS authorization for web service or reading header data on a webservice post 1 51
Initial get page response times and IIS8 15 51
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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