I have an application using the MySQL .Net connection but for some reason i’m having a problem parsing the results to return them,
public NameValueCollection[] query(string query)
{
connect();
NameValueCollection[] resultSet;
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = query;
connection.Open();
Reader = command.ExecuteReader();
string theserows = "";
while (Reader.Read())
{
for (int i = 0; i < Reader.FieldCount; i++){
theserows += Reader.GetName(i)+"="+Reader.GetValue(i).ToString() + ",";
Count = i;
}
theserows += "\n";
}
connection.Close();
string[] results = theserows.Split(new char[] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
int countResultRows = 0;
resultSet = new NameValueCollection[Count];
foreach (string s in results)
{
resultSet[countResultRows] = new NameValueCollection();
string[] result = s.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
foreach (string col in results)
{
if (string.IsNullOrEmpty(col))
continue;
string[] kv = col.Split('=');
resultSet[countResultRows].Add(kv[0], kv[1]);
}
countResultRows++;
}
return resultSet;
}
In this theserows = "site_id=1,\n" but i have an exception thown on the string[] result = s.Split(','); with the exception IndexOutOfRangeException
Could any one give any insight into why this error would be occurring?
On another note the reason i’m reading it all then building the NameValueCollection is I want to add a logging system that logs the full query and it’s results
EDIT::
“count”(lowercase) changed to countResultRows
CallStack
HTML5Streamer.exe!HTML5Streamer.Classes.MySQL.query(string query) Line 53 C#
HTML5Streamer.exe!HTML5Streamer.Classes.Query.getSiteId(string domain) Line 17 + 0x10 bytes C#
HTML5Streamer.exe!HTML5Streamer.Classes.Query.checkUserPass(string username, string password, string domain) Line 31 + 0xb bytes C#
HTML5Streamer.exe!HTML5Streamer.Classes.Service.ProccessAdminRequest(System.Net.HttpListenerContext context) Line 239 + 0xd9 bytes C#
HTML5Streamer.exe!HTML5Streamer.Classes.Service.ProcessRequest(System.Net.HttpListenerContext context) Line 176 + 0xb bytes C#
HTML5Streamer.exe!HTML5Streamer.Classes.Service.ListenerCallback(System.IAsyncResult result) Line 150 + 0xb bytes C#

https://i.stack.imgur.com/EST4w.png
Line 52 is resultSet[countResultRows] = new NameValueCollection();
function starts at line 26 ends at 65
You shouldn’t use same variable names for 2 different values (count, Count), even if it is syntactically correct, this can be confusing.
Second, you should check if kv length is > 1 before accessing the array
If I follow the logic of you program, given the string “site_id=1,\n”, after the split,
result = {“site_id=1″,”\n”}, for the second string kv = {“\n”} and therefor kv[1] will return an OutOfRangeException.
Here’s how I would’ve written your code
Avoiding any possible OutOfRangeExceptions