drop down on razor page

Joseph Kashishian 20 Reputation points

I'm looking to populate drop down from sql server database using system.data.sqlclient and trying to call a stored procedure and return data in a list and have it appear on Razor Page.

Anyone have any good examples?

I'm not looking to use entity framework.

A set of technologies in the .NET Framework for building web applications and XML web services.
3,779 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
11,629 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ping Ni-MSFT 650 Reputation points Microsoft Vendor

    Hi @Joseph Kashishian,

    Here is a whole working demo about how to use ado.net to get the data and fill the value into the dropdownlist.


    public class Test
        public int Id { get; set; }
        public string Name { get; set; }
    public class OccupantDetail {     
        public int StaffID { get; set; } 


    @model IndexModel
    <select asp-for="@Model.OccupantDetail.StaffID" asp-items='new SelectList(Model.StaffList, "Id", "Name")'></select> 


    public class IndexModel : PageModel
        private readonly string _connectionString;
        public IndexModel(IConfiguration configuration)
            _connectionString = configuration.GetConnectionString("DefaultConnection");
         public List<Test> StaffList { get; set; }
         public OccupantDetail OccupantDetail { get; set; }
        public async Task OnGet()
            StaffList = await GetTestAsync();
        public async Task<List<Test>> GetTestAsync()
            var records = new List<Test>();
            using (var connection = new SqlConnection(_connectionString))
                using (var command = new SqlCommand("GetData", connection))
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    await connection.OpenAsync();
                    using (var reader = await command.ExecuteReaderAsync())
                        while (await reader.ReadAsync())
                            var record = new Test
                                Id = (int)reader["Id"],
                                Name = (string)reader["Name"],
                                // Map other columns as necessary
            return records;


      "ConnectionStrings": {
        "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true"

    Stored Procedure in SQL Server

    create procedure GetData 
     select * from dbo.Test

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,