© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
26 replies
Jorjo

✅ IndexOutOfRangeException when trying to set class properties to reader return values

using System.Data.SqlClient;
using System.Diagnostics;
using Microsoft.SqlServer;

namespace AdminSide.Database
{
    internal class Product
    {
        internal string Name { get; private set; }
        internal int Price { get; private set; }
        internal string ImageLink { get; private set; }

        internal List<Product> products = new List<Product>();

        internal void GetProducts()
        {
            var config = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .Build();

            var conn = config.GetConnectionString("AllVendingMachinesDB");
            
            using (var connection = new SqlConnection(conn))
            {
                using (var command = new SqlCommand("READINGVM", connection))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@VMName", Model.AllProperties.CityName); 

                    connection.Open();

                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        Name = reader["ProductName"].ToString();
                        Price = (int)reader["Price"];
                        ImageLink = reader["ProductURL"].ToString();

                        products.Add(new Product() {Name = Name, Price = Price, ImageLink = ImageLink});
                    }
                    reader.Close();
                }
            }
        }
    }
}
using System.Data.SqlClient;
using System.Diagnostics;
using Microsoft.SqlServer;

namespace AdminSide.Database
{
    internal class Product
    {
        internal string Name { get; private set; }
        internal int Price { get; private set; }
        internal string ImageLink { get; private set; }

        internal List<Product> products = new List<Product>();

        internal void GetProducts()
        {
            var config = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .Build();

            var conn = config.GetConnectionString("AllVendingMachinesDB");
            
            using (var connection = new SqlConnection(conn))
            {
                using (var command = new SqlCommand("READINGVM", connection))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@VMName", Model.AllProperties.CityName); 

                    connection.Open();

                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        Name = reader["ProductName"].ToString();
                        Price = (int)reader["Price"];
                        ImageLink = reader["ProductURL"].ToString();

                        products.Add(new Product() {Name = Name, Price = Price, ImageLink = ImageLink});
                    }
                    reader.Close();
                }
            }
        }
    }
}
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Dynamically assign values to class instance properties / Indexing class instance properties
C#CC# / help
2y ago
❔ ✅ IndexOutOfRangeException
C#CC# / help
3y ago
❔ Trying to return
C#CC# / help
3y ago