I'm trying to return a count of row's from a stored procedure in SQL. I've been working at this for hours now. I'm at a complete loss.
SQL Code: Returns result of 49 when run in SQL.
begin
select Count (EmployeeID)
from dbo.Employees
Where (Employees.Active = 'True')
end
Component Code:
@attribute [Authorize(Roles = "Admin")]
@using System.Collections;
@using WahlenDataAccessLibrary;
@using WahlenDataAccessLibrary.Models;
@using BlazorApp1.Models;
@inject IEmployeesData employeeData;
<div class="widget">
<h5 class="widget--title">Active Employees</h5>
<div class="widget--body">
<p class="widget--number">
<span>@employeeCount</span>
Active Employees
</p>
</div>
</div>
@code {
private string employeeCount { get; set; }
//private IEmployeeModel employeeCount = new EmployeeModel();
protected override async Task OnInitializedAsync()
{
var count = await employeeData.EmployeeCount();
//string employeeCount = await employeeData.EmployeeCount();
string employeeCount = count.ToString();
Console.WriteLine(employeeCount);
if (employeeCount != null)
{
Console.WriteLine("generic value");
}
else
{
Console.WriteLine("no value");
}
}
}
DataFile Code: To get the value from stored procedure.
public async Task<string> EmployeeCount()
{
var employeeCount = await _db.LoadValue("dbo.spWidget_EmployeeCount", "DefaultConnection");
return employeeCount.ToString();
}
}
The DataFile where 'LoadValue' is used. This is linked back to my SqlDataAccess File which uses this code.
public async Task<string> LoadValue(string storedProcedure, string connectionStringName)
{
string connectionString = _config.GetConnectionString(connectionStringName);
using (IDbConnection connection = new SqlConnection(connectionString))
{
var data = await connection.QueryAsync(storedProcedure,
commandType: CommandType.StoredProcedure);
return data.ToString();
}
}
When the application is running the console writes.
System.Collections.Generic.List`1[System.Object]
no value
I'm going to update the code here, as I've got it working. Task<IEnumerable> was more so the answer here. If you have an sql query that is simply counting rows, this is how you access that data on Blazor on your front end. You can return the value as a string.
My sqlaccess file code now looks like this.
public async Task<IEnumerable<int>> LoadValue(string storedProcedure, string connectionStringName)
{
string connectionString = _config.GetConnectionString(connectionStringName);
using (IDbConnection connection = new SqlConnection(connectionString))
{
var data = await connection.QueryAsync<int>(storedProcedure,
commandType: CommandType.StoredProcedure);
return data;
}
}
And this is on the front end component.
private string employeeCount { get; set; }
protected override async Task OnInitializedAsync()
{
employeeCount = await employeeData.EmployeeCount();
Console.WriteLine(employeeCount);
if (employeeCount != null)
{
Console.WriteLine("generic value");
}
else
{
Console.WriteLine("no value");
}