C
C#Xantres

Database function skipping data

Hello, when I run the below method it is supposed to return a string array with a list of users, but when I run the debugger, the reader object has rows, but it skips over the while loop. Here is the code:
C#
public static List<string> RetrieveUserList()
{
List<string> users = new List<string>();

DBConnect.StartConnection(); // used to open connection with database

try
{
using (DBConnect.Conn) // Use open connection
{
string getUsers = "SELECT userName FROM client_schedule.user"; // create command string

using (MySqlCommand cmd = new MySqlCommand(getUsers, DBConnect.Conn))
{
MySqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
string temp = reader.GetString(0);

users.Add(temp);
}
}
}
}
catch (MySqlExeception ex)
{
DBConnect.StopConnection(); // close if exception occurs

MessageBox.Show(ex.Message);
}

DBConnect.StopConnection(); // close connection

return users;
}
C#
public static List<string> RetrieveUserList()
{
List<string> users = new List<string>();

DBConnect.StartConnection(); // used to open connection with database

try
{
using (DBConnect.Conn) // Use open connection
{
string getUsers = "SELECT userName FROM client_schedule.user"; // create command string

using (MySqlCommand cmd = new MySqlCommand(getUsers, DBConnect.Conn))
{
MySqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
string temp = reader.GetString(0);

users.Add(temp);
}
}
}
}
catch (MySqlExeception ex)
{
DBConnect.StopConnection(); // close if exception occurs

MessageBox.Show(ex.Message);
}

DBConnect.StopConnection(); // close connection

return users;
}
No description
J
Jimmacle15d ago
.Append doesn't modify the source array you want to use a List<string> instead and call .Add this code doesn't look like it should compile at all, you declare string[] user = null; then try to append to something called users
X
Xantres15d ago
Sorry, I miss typed the variable, it is users
J
Jimmacle15d ago
so you have 2 issues the one i mentioned, and trying to call methods on a null reference
X
Xantres15d ago
I am working on this for a school project and have to use a virtual machine so I can't copy code from it to my machine Okay, so I have changed the above to what I have and that worked, thanks for your direction.
Want results from more Discord servers?
Add your server
More Posts
AssemblyLoadContext LoadfromAssemblyPath vs LoadFromNativeImagePathWhat is the difference between [LoadfromAssemblyPath](<https://learn.microsoft.com/en-us/dotnet/api/SpotifyAPI - i dont understand things but i want this so bad...With alot of Help from the internet i somehow managed to get to this code: (in picture) it is not ev✅ hey, can someone please fix this for me? it keeps giving me errors✅ WPF - how to approach a treeview displaying different types at different levels via MVVMI'm just learning WPF - so seeking some design advice. I'm designing a Visual Studio extension to viDoes hangfire has a redudant delay?I`ve created an Job that deletes object from db at certain time, but it looks like it is being delayhelp needed with accesing a variable in another scriptim trying to acces isgrounded in another script and have it be the same as hi but i get this errorIssue with websocket within a serviceIm having troubles using a websocket service within other parts of my projects, cannot use the Send HelpI'm new to C# in VSC and i have a Problem wich is why i can't execute the Code. What am i supposed t✅ Checking data from SQL databasehelp on verge of tears✅ How to export data from datagridview into excel fileI've tried like a few ways and cant really make sense of it, if someone can guide me through it it wProblem with Material Design Lib in wpfI have a Combobox item in Stack panel. Im using Material Desing for automatic styling but the Popup✅ Title: Issues with Authentication and Authorization using JWT with AutoRender Mode in Blazor NET8Hello friends, I am currently facing challenges with Authentication and Authorization using JWT wit✅ How do I reference a specific private instance of a classI would like to know how I could reference a specific instance of a class, if the class is private aReceiving Game Controller Input while Minimized (WinForms C#)Pretty general question here. How would I go about receiving input from my user while they aren't tprocess startIve been trying to figure out why the proccess.start() hasnt been working✅ Configurations say that appsettings is returning nullBasically, i have a configuration and i'm trying to access a value from my appsettings, and all that✅ ASP.NET Core Web Api + SignalRHi, I have an API deployed on IIS but when a person wants to access it they get an error: "ERR_CONNE✅ .NET sdk not workingSo, I've installed the .NET sdk and did what I was told, but it's still not being detected by vscode