0

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 on 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.

3
  • how is Table.UserId defined in the database ? Commented May 15, 2024 at 12:06
  • are you sure the error isn't "can not convert from string to IdentityUser" ? which would be very different? Commented May 15, 2024 at 12:07
  • Honestly, IMO the error here is using IdentityUser in your domain/POCO model; that should just be public string UserId { get; set; } - then: job done; IdentityUser/IdentityUser<T> is a rich type, that isn't really sensibly usable from an anaemic data source such as a simple column read Commented May 15, 2024 at 12:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.