ASP.NET Core Web App - Looping through a DataTable

Chris Undritz 41 Reputation points
2022-12-06T16:43:50.16+00:00

Hello All,

I am creating an ASP.NET Core Web App using C# using .NET6.0. I have only just started and looking to complete a connection to an existing SQL database and just quickly display some data to prove the connection and the SQL Queries.

This is my setup; I have only created one page so far: ‘dashboard.cshtml’
267872-dashboardcshtmlcs-overview.png

267856-dashboardcshtml-overview.png

The SQL query works fine and, when stepping through the code, returns the expected values from the database as you can see below:
267881-dashboardcshtmlcs-datatable-value.png

I also added a string variable to see if this would display and also to test the display of a single variable in the front end. As you can see the expected value is assigned
267846-dashboardcshtmlcs-messsage-value.png

However, as you can see from the images above, there is a problem with the reference to the variables ‘message’ and ‘dtsensors’ in the html and so the application does not run. The errors are below:
267882-errors.png

So, could anyone help me to understand what the error message ‘the name <variable> does not exist in the current context’? And how to fix it? Also, could some one confirm how I loop through a DataTable in the front end? I cannot find any documentation on this.

Thanks in advance

Developer technologies ASP.NET ASP.NET Core
SQL Server Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-12-07T06:09:12.693+00:00

    Hi @Chris Undritz ,

    268082-image.png

    Razor exposes a Model property for accessing the model passed to the view, so you can access the properties via the @Model.{PropertyName}, refer to the following screenshot:

    267985-image.png

    More detail information about Razor syntax, see Razor syntax reference for ASP.NET Core.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-12-06T17:12:54.967+00:00

    c# is case sensitive, so its @Message. also only public properties can be accessed make dtSensors public

    1 person found this answer helpful.
    0 comments No comments

  2. Cesar Israel Arronte Lopez 1 Reputation point
    2022-12-07T00:17:04.753+00:00

    Also, could some one confirm how I loop through a DataTable in the front end? I cannot find any documentation on this.

    I recommend using a different object rather than a DataTable to share this class with the front-end, like a Model (class). A model class will work like a lightweight object to store the values from your SQL result set. Something like this (just for giving you an example):

    public class Sensor{  
      public string ID { get; set; }  
      public string Make { get; set; }  
      public string Model { get; set; }  
      public string Sensor { get; set; }  
      public string Status { get; set; }  
    }  
    

    Then your IndexModel page will contain a public property like a List of Sensors public List<Sensor> Sensors, so you can iterate easily the list of sensors in the front-end.

    Take your time to review the MVC pattern.

    0 comments No comments

  3. SurferOnWww 4,631 Reputation points
    2022-12-07T01:58:09.663+00:00

    I suggest that you

    • use the Reverse Engineering to create data model (context class and entity classes):

    Reverse Engineering
    https://learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding/?tabs=dotnet-core-cli

    Scaffold-DbContext
    https://learn.microsoft.com/en-us/ef/core/cli/powershell#scaffold-dbcontext

    • and create controller and views using the scaffolding function of Visual Studio:

    Create controller and views
    https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-7.0#create-controller-and-views

    0 comments No comments

  4. Chris Undritz 41 Reputation points
    2022-12-07T08:55:13.65+00:00

    Hi All. Thanks for the responses. In reference to the first answer above I have made dtSensors public and also changed the message variable to correct syntax and it did not help. I still have the same error messages. I have already successfully used the model property however I want to use a data table. It is clearly possible to retrieve the data table, but is it possible to loop through a data table in the front end in this instance?

    Also I have been asked by my management to not use Entity Framework for support reasons. They have requested that I directly pull the data from the existing SQL database as the rest of the team are skilled in SQL. The existing websites we have created have been done as ASP.NET webforms using VB.NET. I have been given the opportunity to try and do this in C# and .NET6 and so this is why I am, so far, trying to do it this way.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.