C
C#8mo ago
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();
}
}
}
}
}
No description
12 Replies
Jorjo
Jorjo8mo ago
This is supposed to create a new object with it's property values set to what the reader returns then later add the new objects with their respective values to a list which will be used in a for loop to itterate the property values and display information inside some paragraph elements and such. However despite the stored procedure working and returning the right results based on the city name it causes an exception to occur which is the attached image to this post. I tried doing exception handling as well but the exception message when printing to the debug console is simply just "ProductName" which is weird to say the least. The column names are written correctly but it still returns null no matter what, except when indexed, then the Name property gets the value of 916.
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Jorjo
Jorjo8mo ago
Oh well yeah I tried debugging it, hence why when I use an index for the column instead of the alias it gives me the weird value of 916 I got that from debugging the property being set to that but since I'm using the stored procedure alias it just gives a value of null and crashes due to me removing the exception handling logic. But do you need me to check the reader's values instead? anyway here:
Jorjo
Jorjo8mo ago
No description
Jorjo
Jorjo8mo ago
I put it anyway crashed right after i stepped over the Name property being set to the reader return value with the same exception
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Jorjo
Jorjo8mo ago
ok I've fixed it It was a privilege issue, there are some procedures that create separate databases for each city and my fellow practician here forgot to update the prodecure to give it rights to the newly created database. It couldn't access it the whole time. I'll close the post down thanks for the help eitherway
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX8mo ago
Use the /close command to mark a forum thread as answered
Jorjo
Jorjo8mo ago
thanks again!
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Jorjo
Jorjo8mo ago
lmao