C
C#8mo ago
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
4 Replies
Jimmacle
Jimmacle8mo 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
Xantres
XantresOP8mo ago
Sorry, I miss typed the variable, it is users
Jimmacle
Jimmacle8mo ago
so you have 2 issues the one i mentioned, and trying to call methods on a null reference
Xantres
XantresOP8mo 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