Hi,
Got a bit of a bizarre issue here, I am trying to use ExecuteScalar to see if any values are present and depending if it is to perform a certain action. What seems to be happening is that even with a BLANK table with no values, it isn't being registered null, here is a snippet of the code for you to see:
private void check_services()
{
try
{
dbConnect.services_initialise();
dbConnect.services_Open_Connection();
string check_query = "SELECT MIN(services_remaining) FROM services WHERE SRID=@SRID AND services_left=@SL";
MySqlCommand cs = new MySqlCommand(check_query, dbConnect.services_connection);
cs.Parameters.AddWithValue("@SRID", txtServiceRecordID.Text);
cs.Parameters.AddWithValue("@SL", "TRUE");
if (cs.ExecuteScalar() == null)
{
create_new_service();
}
else
{
using (MySqlDataReader read = cs.ExecuteReader())
{
while (read.Read())
{
services_remaining = read.GetString(read.GetOrdinal("services_remaining"));
rowID = read.GetString(read.GetOrdinal("ID"));
}
}
update_previous_record();
}
}
catch (MySqlException ex)
{
MessageBox.Show("Error checking services! " + ex, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
What is most strange and not being caught in the try/catch is a exception when run that say's the following:
"
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in MySql.Data.dll
Additional information: Could not find specified column in results: services_remaining"
I've checked and double checked the column name and it is correct and spelt correctly. I'm really stumped on this one, the errors happening in both cases.
CASE 1: Blank table with no values in it --> doesn't call "create_new_service()" instead jumps to else and tries to pull the values and presents the error mentioned above.
CASE 2: Values in the table --> it jumps to the else, tries to pull the values and presents the error mentioned above.
Not sure what it causing it, hopefully someone might be able to help.
Appreciate all your help and feedback!