I have this model:
public class Model
{
public IdentityUser UserId { get; set; }
public string data{ get; set; }
public decimal data2{ get; set; }
public DateTime data3{ get; set; }
}
And this function:
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "SELECT UserId, data, data2, data3 FROM Table;
using (SqlCommand command = new SqlCommand(sql, connection))
{
using (SqlDataReader reader = await command.ExecuteReaderAsync())
{
while (reader.Read())
{
Model test = new Model
{
UserId = reader.GetString(0) // error, can not convert from Identity to string
data = reader.GetString(1),
data2 = reader.GetDecimal(2),
data3 = reader.GetDateTime(3)
};
list.Add(test);
}
}
}
Before this, I populated the data from the table and was able to display the data for a specific user using command.Parameters.AddWithValue("@UserId", userId);
and I obtained the userId in the function:
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user != null)
{
userId = user.Identity.Name;
}
}
I want to display in a blazor page a table with all data from that table but I cannot get the UserId because I don’t know how to read it using SqlDataReader