简体   繁体   中英

How to display number of rows in SQL Table to an HTML container

I'm making a Bug Tracker application using ASP.Net using MVC. Whenever a ticket gets created, it gets added to my Tickets.sql database. On my VIEW dashboard.cshtml of my application, I want to be able to display the number of tickets there is inside of the Ticket Database. But I'm getting stuck trying to implement the function. I'm not sure how to do it either. I'd appreciate some help with the process and with knowing what I need to do.

My Dashboard that shows where the number of tickets should be displayed:

<div class="col-md-4">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3>Unresolved Tickets</h3>
                </div>
                <div id="num-unresolved-tickets" class="panel-body">
                    <h2 class="text-center">
                        <?php>
                            $SELECT COUNT(ID) AS UserId FROM Tickets;
                        </?php>
                    </h2>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3>Resolved Tickets</h3>
                </div>
                <div id="num-resolved-tickets" class="panel-body">
                    <h2 class="text-center">4</h2>
                </div>
            </div>
        </div>

Based on my research, it is hard for us to achieve data from database in the cshtml directly.

We can use c# code to execute SqlCommand to show data in the cshtml .

First, we need to add the following class to deal with SqlConnection and SqlCommand .

public class SqlHelper
    {

        private SqlConnection con;
        //To Handle connection related activities    
        private void connection()
        {
            string constr = "CONNSTR";
            con = new SqlConnection(constr);

        }

        public string ExecuteSql(string sql)
        {
            connection();
            SqlCommand command = new SqlCommand(sql,con);
            con.Open();
            SqlDataReader reader = command.ExecuteReader();
  
            while (reader.Read())
            {
                return reader.GetName(0)+ ": "+reader[0].ToString();
               
            }
            con.Close();
            return string.Empty;
        }
    }

Second, we can call the method in the controller.

public ActionResult Index()
        {
            string sql = "SELECT COUNT(ID) AS UserId FROM Tickets";
            SqlHelper helper = new SqlHelper();
            ViewBag.Message = helper.ExecuteSql(sql);
            return View();
        }

Third, please set the following cshtml.

<h2>Index</h2>
<div id="num-unresolved-tickets" class="panel-body">
    <h2 class="text-center">
        <b style="color:red"> @ViewBag.Message</b>
    </h2>
</div>

Finally, you can get the correct result.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM