Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Not read data from database
In table porezi have data
My code
Table header have, table data no have, no error
Some help?

ASP.NET
<pre>using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data.SqlClient;

namespace MyStore.Pages.Porezi
{
    public class IndexModel : PageModel
    {
        public List<ClientInfo> listClients = new List<ClientInfo>();
        public void OnGet()
        {
            try
            {
                String connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True;Trust Server Certificate=True";

                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    String sql = "SELECT * FROM porezi";

                    using (SqlCommand command = new SqlCommand(sql, connection))
                    {
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                ClientInfo clientInfo = new ClientInfo();
                                clientInfo.id = "" + reader.GetInt32(0);
                                clientInfo.redni_broj = reader.GetString(1);
                                clientInfo.naziv = reader.GetString(2);
                                clientInfo.procenat= "" + reader.GetString(3);
                                clientInfo.fiksna_vrednost = "" + reader.GetString(4);
                                clientInfo.oznaka = reader.GetString(5);

                                listClients.Add(clientInfo);
                            }
                        }

                    }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.ToString());  
            }
        }

        public class ClientInfo
        {
            public String id;
            public String redni_broj;
            public String naziv;
            public String procenat;
            public String fiksna_vrednost;
            public String oznaka;
        }
    }

}


What I have tried:

Index page

C#
<pre>@page
@model MyStore.Pages.Porezi.IndexModel
@{
}
<br>
<h2>List of Clients</h2>
<a class='btn btn-primary btn-sm' href='/Porezi/Create'>New Client </a>
<table class="table">

    <thead>
        <tr>
            <th>id</th>
            <th>redni_broj</th>
            <th>naziv</th>
            <th>procenat</th>
            <th>fiksna_vrednost</th>
            <th>oznaka</th>
            <th>Action</th>
        </tr>

    </thead>
    <tbody>
        @foreach(var item in Model.listClients)
        {
            <tr>
                <td>@item.id</td>
                <td>@item.redni_broj</td>
                <td>@item.naziv</td>
                <td>@item.procenat</td>
                <td>@item.fiksna_vrednost</td>
                <td>@item.oznaka</td>
                <td>
                    <a class="btn btn-primary btn-sm" href="/Porezi/Edit?id=@item.id">Edit</a>
                    <a class="btn btn-danger btn-sm" href="/Porezi/Delete?id=@item.id">Delete</a>
                </td>

            </tr>
        }
    </tbody>
</table>
Posted
Comments
Richard MacCutchan 4-Mar-24 15:58pm    
"Not read data from database
In table porezi have data
My code
Table header have, table data no have, no error
Some help?"

What is that supposed to mean? Please use the Improve question link above, and add complete details of what is not working.
Stylus STYLUS 4-Mar-24 16:20pm    
I don't have data in datagrid
Here

@foreach(var item in Model.listClients)
{
@item.id @item.redni_broj @item.naziv @item.procenat @item.fiksna_vrednost @item.oznaka Edit
Delete
}

Start by adding three breakpoints: one at the start of the method, one at the end, and one in the catch block. Run your code in the debugger and when it hits the first breakpoint let it continue so you can see which of the other two it hits. If it hits the catch, look at the exception object. If it hits the end of the function, look at the listClients object and see how many elements are there.

From that, you you know one of four things:
0) that code deosn't get called at all.
1) You have a problem with your SQL or connection.
2) You have a problem with the database (i.e. it's not the one you thought)
3) You have a problem elsewhere in your code.

The first is pretty obvious ...
For the second two, you can run it again and step through the code to find out where it's a problem.
For the last one, you need to look at what happens to the collection elsewhere.
 
Share this answer
 
As well as OriginalGriff's answer, a further enhancement for you is to not use Console.WriteLine in your web applications. This doesn't write where you think it's going to write. Change this to use logging and you will get a better view of any exceptions that are thrown.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900