Look at this:
myReader = cmd.ExecuteReader();
if (myReader.HasRows)
{
while (myReader.Read())
{
info = myReader.GetString("info");
...
}
}
.....
Well, I don´t know why, but when I compile it and use it in another PC, it explodes because of the while. But If I do:
myReader = cmd.ExecuteReader();
if (myReader.HasRows)
{
if (myReader.Read()) <------------------- NOTICE THE IF
{
info = myReader.GetString("info");
...
}
}
.....
… will work. Notice that I know that I will have only one element every time, because of the logic of the program.
It is OK to do this? It looks horrible and maybe is not a good practice (?). I´m asking for your orientation.
EDIT:
Will post some more code, because could help in finding my errors:
while (myReader.Read())
{
info = myReader.GetString("info");
...
/**
* Write the relevant info in the LOGS
*/
connection.Close(); <---------- :S
connection.Open(); <---------- if I don´t do this I got some problems with the connection!!!!
string queryLog = "INSERT INTO ....;
MySqlCommand cmdLog = new MySqlCommand(queryLog, connection);
cmdLog.ExecuteNonQuery();
}
I do this whenever I am expecting just one row, nothing wrong with it.
If I’m expecting a single value, I use
ExecuteScalarinstead. This will simply return the value held in the first column of the first row of the resultset returned, usally the set only has one column and one row, so you don’t tend to notice this.Also I tend not to bother with
if (myReader.HasRows)asif (myReader.Read())covers this.I have no idea why the
whilecauses you issues… I’m just offering my opinion on using theifover thewhileif you aren’t expecting multiple rows.