hany mohamed

hany mohamed

  • 1.6k
  • 109
  • 3.7k

i can not display the data

Apr 22 2024 12:34 PM

When I open the page, the data recorded

 Why I cannot display data

@page
@model MyStore.Pages.Clients.IndexModel
@{
}

<br>
<h2>List of Clients</h2>
<a class='btn btn-primary btn-sm' herf='/Clients/Create'>New Client</a>
<table calss="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>&nbsp;&nbsp;&nbsp;&nbsp;</th>
            <th>Name</th>
            <th>&nbsp;&nbsp;&nbsp;&nbsp;</th>
            <th>Email</th>
            <th>&nbsp;&nbsp;&nbsp;&nbsp;</th>
            <th>Phone</th>
            <th>&nbsp;&nbsp;&nbsp;&nbsp;</th>
            <th>Address</th>
            <th>&nbsp;&nbsp;&nbsp;&nbsp;</th>
            <th>Created At</th>
            <th>&nbsp;&nbsp;&nbsp;&nbsp;</th>
            <th>Action</th>
            <th>&nbsp;&nbsp;&nbsp;&nbsp;</th>
        </tr>
    </thead>
    <tbody>
    @foreach(var item in Model.listclients)
        {
            <tr>
                <td>@item.id</td>
                <td>@item.name</td>
                <td>@item.email</td>
                <td>@item.phone</td>
                <td>@item.address</td>
                <td>@item.created_at</td>
                <td>
                    <a class="btn btn-primary btn-sm" herf="/Clients/[email protected]">Edit</a>
                    <a class="btn btn-primary btn-sm" herf="/Clients/[email protected]">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>
This is the code
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data.SqlClient;

namespace MyStore.Pages.Clients
{
    public class IndexModel : PageModel
    {
        public List<ClientInfo> listclients = new List<ClientInfo>();
        public void OnGet()
        {
            try
            {
                string connectionString = "Data Source=.\\sqlexpress;Initial Catalog=mystore;Integrated Security=True;Encrypt=True;Trust Server Certificate=True";
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    string sql = "SELECT * From clients";
                    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.name = reader.GetString(1);
                                clientInfo.email = reader.GetString(2);
                                clientInfo.phone = reader.GetString(3);
                                clientInfo.address = reader.GetString(4);
                                clientInfo.created_at = reader.GetDateTime(5).ToString();
                                listclients.Add(clientInfo);
                            }
                        }
                    }

                }
            }

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

        }
    }
    public class ClientInfo
    {
        public string id;
        public string name;
        public string email;
        public string phone;
        public string address;
        public string created_at;
    }
}


in the database does not appear. Are there any other files that should be related to this? i am using vb 2022 razor page  .Net8


Answers (8)