❔ Using DataTable in .cshtml File with JavaScript

Hello everyone, I'm still new to using C#, and I'm trying to figure out how you display data from a SQL database that is called in the OnGet() method of my page with a paginated table.
using System.Data;
using System.Data.SqlClient;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace ExampleWebsite.Pages;

public class Responses : PageModel
{
public void OnGet()
{
DataTable table = new DataTable();
const string connectionString = "secret";
using (SqlConnection connection = new SqlConnection(
connectionString )
)
using(SqlCommand cmd = new SqlCommand(
"SELECT * FROM Responses",
connection )
)
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(Dt);
}
}
}
using System.Data;
using System.Data.SqlClient;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace ExampleWebsite.Pages;

public class Responses : PageModel
{
public void OnGet()
{
DataTable table = new DataTable();
const string connectionString = "secret";
using (SqlConnection connection = new SqlConnection(
connectionString )
)
using(SqlCommand cmd = new SqlCommand(
"SELECT * FROM Responses",
connection )
)
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(Dt);
}
}
}
This is where I am so far. Any help is greatly appreciated.
2 Replies
Angius
Angius2y ago
If you're using Razor Pages - which you seem to be doing - just create a property on that class and save your data there (also, a very weird choice to be using DataTables and ADO with modern ASP)
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server