locked
Database Query not Working - MVC 3 RRS feed

  • Question

  • Hi,

    I am coding a web application using MVC 3 in C#. I am trying to pull out some "News entries" from an Access .mdb database. Is this even possible or must it be an .mdf? Instead of returning the results, it says: Select from 0 New Entries. So it's not pulling them up at all. Here is the code from my Index page:

     

     Select from @Model.Count() News Entries</p>
    <ul>
     @foreach (var News in Model)
     {
     <li>@Html.ActionLink(News.Headline, "Browse", new { News = News.Headline })</li>
     }
    </ul>
    

     

    Here is the code from my News class:

     

     public class News
     {
     public int NewsID { get; set; }
     public string Headline { get; set; }
     public DateTime NewsDate {get; set;}
     public string AddedBy {get; set;}
    
     }
    }
    

     

    Here's the code from my NewsController:

     

    using ISPracticalExam0707168E.Models;
    
    namespace ISPracticalExam0707168E.Controllers
    {
     public class NewsController : Controller
     {
    
     RugbyNewEntities rugbyDB = new RugbyNewEntities();
    
     //
     // GET: /News/
    
     public ActionResult Index()
     {
      var News = rugbyDB.News.ToList();
      return View(News);
     
     }
    

     

    And then I have a RugbyNewsEntities.cs class:

     

    namespace ISPracticalExam0707168E.Models
    {
     public class RugbyNewEntities : DbContext
     {
     public DbSet<News> News { get; set; }
    
     }
    

    And the connection string in my web.config file is:

     

     <connectionStrings>
     <add name="RugbyNews"
      connectionString="Data Source=|DataDirectory|RugbyNews.mdb"
      providerName = "Microsoft.ACE.OLEDB.12.0" />
     </connectionStrings>
    

     

    Is there a line of code that I am missing somewhere that is causing no results?

     

    Thanks,

    Amy

     



    • Moved by Cookie Luo Monday, June 6, 2011 5:55 AM (From:Visual C# General)
    Tuesday, May 31, 2011 5:49 PM

All replies

  • On 5/31/2011 1:49 PM, amecily wrote:
     
    I have seen no examples of what you are trying to do working with
    Access. Access is not a good solution for a mutil user Web application,
    because it's not a multi user database solution. Maybe you need to
    switch to SQL Server Express (which is free) and use Linq-2-SQL or
    ADO.NET Entity Framework.
     
    Tuesday, May 31, 2011 6:28 PM
  • Hi, thanks a lot. On your advice, I have created a SQL table. And my connection string now looks like this:

     <connectionStrings>
      <add name="RugbyNewEntities" connectionString="data source=.\SQLEXPRESS; Integrated Security=SSPI;
    AttachDBFilename=|DataDirectory|\RugbyNews.mdf; User Instance=true" providerName="System.Data.SqlClient" />
      </connectionStrings>
    

    I've left everything else the same, but it still brings up 0 entries. I have 2 entries in the database that should be showing up. Can you see anything missing in my code?

     

    Thanks,

    Amy

    Tuesday, May 31, 2011 6:45 PM
  • On 5/31/2011 2:45 PM, amecily wrote:
    > Hi, thanks a lot. On your advice, I have created a SQL table. And my
    > connection string now looks like this:
    >
    > <connectionStrings>
    > <add name="RugbyNewEntities" connectionString="data source=.\SQLEXPRESS; Integrated Security=SSPI;
    > AttachDBFilename=|DataDirectory|\RugbyNews.mdf; User Instance=true" providerName="System.Data.SqlClient" />
    > </connectionStrings>
    >
    > I've left everything else the same, but it still brings up 0 entries. I
    > have 2 entries in the database that should be showing up. Can you see
    > anything missing in my code?
    >
     
    Maybe, you should try another example.
     
    <http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx>
     
     
    Tuesday, May 31, 2011 7:08 PM
  • Are you getting any error?

    Do you have a try - catch block that prevents you from getting error messages?

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Tuesday, May 31, 2011 8:22 PM
  • @darnold: Thanks, that site was super helpful. The Create is working - when I fill in a form and click Create, it shows up on my page. But the weird thing is that it doesn't update the database and the items from the database still don't show up. So the connection to my database is obviously not working.

    Here's the code again, from RugyNewEntities.cs

     

     public class RugbyNewEntities : DbContext
     {
     public DbSet<News> News { get; set; }
    
     }
    

     

    From the controller:

     

     

     public class NewsController : Controller
     {
    
     RugbyNewEntities rugbyDB = new RugbyNewEntities();
    
     //
     // GET: /News/
    
     public ActionResult Index()
     {
      var News = rugbyDB.News.ToList();
      return View(News);
    
     }
    
     public ActionResult Create()
     {
      return View();
     }
    
    
     [HttpPost]
     public ActionResult Create(News news)
     {
      if (ModelState.IsValid)
      {
      rugbyDB.News.Add(news);
      rugbyDB.SaveChanges();
    
      return RedirectToAction("Index");
      }
    
      return View(news);
    
     }
    

     

    The News.cs:

     

     public class News
     {
     public int NewsID { get; set; }
     public string Headline { get; set; }
     public DateTime NewsDate {get; set;}
     public string AddedBy {get; set;}
    
    
    
     }
    
    @Noam: no, there are no errors at all.

     



    Tuesday, May 31, 2011 9:58 PM
  • Hi

    Please try asp.net MVC3 forum.

    Asp.net issue is not support in c# forum.

     


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Just Karl Wednesday, January 15, 2014 3:37 PM
    Monday, June 6, 2011 5:55 AM