Hi @Ken Ekholm , Welcome to Microsoft Q&A.
Your error is because you only declared the array but didn't initialize it.
You can use List<string>
instead of Array
.
If you need an array, just use string[] values = valuesList.ToArray();
to get the array at the end, and the declaration of this values can also be placed before the database using for global use.
Use parameterized queries to avoid sql injection.
using (var connection = new MySqlConnection(connString))
{
connection.Open();
string query = "SELECT * FROM showvalues WHERE idvalue = @idStatus";
using (var command2 = new MySqlCommand(query, connection))
{
command2.Parameters.AddWithValue("@idStatus", idStatus);
using (var reader2 = command2.ExecuteReader())
{
List<string> valuesList = new List<string>();
while (reader2.Read())
{
listBoxMeasurement.Items.Add(reader2.GetString("textinfo"));
valuesList.Add(reader2.GetString("textinfo"));
}
string[] values = valuesList.ToArray();
connection.Close();
}
}
}
Best Regards,
Jiale
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.