I have encountered this situation whereby this SQL statement does not work..
command.Parameters.AddWithValue("@OA_Name", obj.GEToperatingauthority());
command.CommandText = "SELECT [OA_ID] FROM [Operating_Authority_Table] WHERE [OA_Name] = [@OA_Name]";
but this SQL statement works when i changed the ['@OA_Name'] to simply a value found in the table.
command.CommandText = "SELECT [OA_ID] FROM [Operating_Authority_Table] WHERE [OA_Name] = ['OA 101']";
I have tested the value i got from the obj.GetOperatingauthority() and it is exactly the same as the one in the database. I have used reader.hasrows to check whether there are any rows returned..
btw i am using c# and access 2010 database..
edit: i put in the bulk of the code below:
using (var command = connection.CreateCommand())
{
connection.Open();
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@NRIC", obj.GETnricfinnumber());
command.Parameters.AddWithValue("@Participant_Name", obj.GETname());
command.Parameters.AddWithValue("@Gender", obj.GETgender());
command.Parameters.AddWithValue("@DOB", obj.GETdateofbirth());
command.Parameters.AddWithValue("@Nationality", obj.GETnationality());
command.Parameters.AddWithValue("@Race", obj.GETrace());
command.Parameters.AddWithValue("@Residential_Address", obj.GETresidentialaddress());
command.Parameters.AddWithValue("@Contact_Number", obj.GETcontactnumber());
command.Parameters.AddWithValue("@Email_Address", obj.GETemailaddress());
command.CommandText = "SELECT [NRIC] FROM [Participant_Table] WHERE [NRIC] = [@NRIC]";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
if (reader.GetString(0).ToLower().Equals(obj.GETnricfinnumber().ToLower()))
{
message.show(1, "", "exists");
//return false;
}
}
}
command.CommandText = "SELECT [OA_ID] FROM [Operating_Authority_Table] WHERE [OA_Name] = @OA_Name";
command.Parameters.AddWithValue("@OA_Name", obj.GEToperatingauthority());
using (var reader = command.ExecuteReader())
{
if (!reader.HasRows)
{
message.show(1, "", "no rows!"); return false; //to test whether has rows or no rows
}
edit 2: the getOperatingAuthority can't be wrong as it is basically a "closed-loop" system whereby OA_Name are used to populate a combobox, and values selected from this combo box are used to referred back to the table to get the ID.