Not read data from database
In table porezi have data
My code
Table header have, table data no have, no error
Some help?
My code
TypeScript
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data.SqlClient;
namespace MyStore.Pages.Porezi
{
public class IndexModel : PageModel
{
public List listClients = new List();
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;
}
}
}
REst of code


Jignesh KumarRank #91 · 22.9K reputationPosted Mar 4, 2024, 11:07 PM EST
First of all, you are returning Void means that the method not returning any value.
public IActionResult 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()); } return listClients; }And this method you need to call in your controller,
Please refer to this article, it will help you out,
https://www.c-sharpcorner.com/article/crud-operations-in-asp-net-core-mvc-net-5-0/
Abhishek YadavRank #217 · 11.3K reputationPosted Mar 6, 2024, 12:43 AM EST
Please refer to this article, it will help you out,
click here