Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:16:29+00:00 2026-06-17T12:16:29+00:00

Can anyone show me how to loop through my table in my view? The

  • 0

Can anyone show me how to loop through my table in my view?
The rows I have are: disease_id, disease, and remedy.

My DB Query

I tried to get all the results into a list, so i can loop through them in the view.

        SqlCommand cmd = new SqlCommand("SELECT * FROM disease", m_DBCon);
        m_Reader = cmd.ExecuteReader();

        List<string> record = new List<string>();
        while (m_Reader.Read())
        {
            string s = Convert.ToString(m_Reader[0]);
            record.Add(s);
        }

        ViewBag.Record = record;

My View

I want to loop the database records in a table

@{
    ViewBag.Title = "Dashboard";
    List<string> record = ViewBag.Record;
}

I keep getting an empty string. I thought I would have to do something like s[“disease”], s[“remedy”}, but s is a string so it doesn’t have all those fields.

    @{
        foreach (string s in record)
        {
            <tr>
              <td>@s</td>
            </tr>
        }
    }

Can anyone please give me some advice 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:16:30+00:00Added an answer on June 17, 2026 at 12:16 pm

    I’d first want to point out that it’s probably easier to create a DiseaseResult class first.
    Such that :

    public class DiseaseResult
    {
      public int DiseaseId { get; set; }
      public string Disease { get; set; }
      public string Remedy { get; set; }
    }
    

    that makes things a little bit easier. Now you can say:

    public ActionResult Index()
    {
        SqlCommand cmd = new SqlCommand("SELECT [disease_id], [disease], [remedy] FROM disease", m_DBCon);
        m_Reader = cmd.ExecuteReader();
    
        List<DiseaseResult> record = new List<DiseaseResult>();
        while (m_Reader.Read())
        {
            record.Add(new DiseaseResult() { DiseaseId = m_Reader[0], Disease = m_Reader[1], Remedy = m_Reader[2]);
        }
    
        return View(record);
    }
    

    Afterward in your View:

    @model List<DiseaseResult>
    
    @foreach(DiseaseResult item in Model)
    {
       <div>@item.Disease</div>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which loops through rows in a table so that only
Can anyone show me how to break out of a Sencha Ext.each loop?
Can anyone show a short and simple code snippet? It should take the name
can anyone show me the syntax for inserting a python tuple/list into a mysql
Can anyone show me a way of converting SQL Server data-types (varchar for example)
Can anyone show me how to create a mathematical expression for the number of
Can anyone show me how to make a UIManagedDocument globally accessible?
I'd like to do this in the INSERT SQL. Can anyone show me. Thanks
can anyone recommend a website crawler that can show me all of the links
Does anyone know how I can show a tooltip whenever a user hovers over

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.