-3

I am trying to get the data from my SQL table to display onto my View page which I would like to display the contents from the table

I can currently read the items in the database using the code below

SqlConnection connection = new SqlConnection(VV);

using (connection)
{
  //LIMIT 5 DESC from ID which shows last 5 work outs 
  SqlCommand myCommand = new SqlCommand("SELECT * FROM Strength", connection);
  connection.Open();

  SqlDataReader read = myCommand.ExecuteReader();
  if (read.HasRows)
  {
    while (read.Read())
    {
      Id = read["Id"].ToString();
      System.Diagnostics.Debug.WriteLine(Id);

      Weight = read["Weight"].ToString();                    
      System.Diagnostics.Debug.WriteLine(Weight);

      Rep = read["Rep"].ToString();
      System.Diagnostics.Debug.WriteLine(Rep);
    }
  }
  else
  {
    Console.WriteLine("nothing");
  }
  read.Close();
}

And now I want to display this in a HTML table on the view. I have tried a few things such as

 ViewBag.HtmlStr = "<table class='table table-striped top-buffer'"
                 + "style='width:300px'>"
                 + "<tr><th>Weight(KG)</th><th>Reps</th></tr>"
                 + "<tr><td>" + TableWeight + "</td>"
                 + "</tr><tr><td>" + TableRep + "</td></tr></table>";

However it is only giving me one row.

Any advice? Many thanks

4
  • 1
    Code inside while (read.Read()) will be executed for each row returned by the database. You need to create a list of objects and and a new item to the list for each row, and then send that list to your view. Commented Nov 2, 2018 at 10:49
  • Possible duplicate of Pass Html String from Controller to View ASP.Net MVC Commented Nov 2, 2018 at 10:50
  • 2
    Oh and I strongly recommend you NOT to pass HTML from your controller to your view. You should pass your model (data), not HTML Commented Nov 2, 2018 at 10:51
  • 1
    create a model, then use that model in your view to create html table using Razor. Commented Nov 2, 2018 at 10:52

1 Answer 1

1
using (connection)
{
  //LIMIT 5 DESC from ID which shows last 5 work outs 
  SqlCommand myCommand = new SqlCommand("SELECT * FROM Strength", connection);
  connection.Open();

  SqlDataReader read = myCommand.ExecuteReader();

  string result = "<table class='table table-striped top-buffer'" 
                + "style='width:300px'>" 
                + "<tr><th>Weight(KG)</th><th>Reps</th></tr>";               

  if (read.HasRows)
  {
    while (read.Read())
    {
      Id = read["Id"].ToString();
      System.Diagnostics.Debug.WriteLine(Id);

      Weight = read["Weight"].ToString();
      System.Diagnostics.Debug.WriteLine(Weight);

      Rep = read["Rep"].ToString();
      System.Diagnostics.Debug.WriteLine(Rep);

      result += "<tr><td>" + Weight + "</td>"
             +  "</tr><tr><td>" + Rep + "</td></tr>";
    }
  }
  else
  {
    Console.WriteLine("nothing");
  }
  read.Close();

  ViewBag.HtmlStr = result + "</table>";
}

You must consider that your data source contains multiple rows and you should add each row as a TR inside your table.

Sign up to request clarification or add additional context in comments.

5 Comments

Not a good idea to mix HTML and data access code. You should create a list of objects from the SqlDataReader and then send that list to your view.
@RuiJarimba I agree, but if he wants to use this approach, I showed him what's the problem with his code.
Thank you @Amin, It works now. Another quick question, why is it not a good idea to mix HTML and data access code?
You're welcome. HTML format is your representation format and it's not relevant to your data structure. So you should create a good data in your controller and you should represent it with your desired format in your view.
@Leon separation of concerns. Please read about the Model-View-Controller (MVC) or watch some videos such as Introduction to ASP.NET MVC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.