I wrote a generic method to retrieve single values from database (MSSQL Server).
I encountered into a case that I need to get a Boolean
value from DB.
As you can see in the code below, a Object
local field (IsExist
) gets the result.
When the value in DB is False GenericScalar()
method return False (as it should)
and the condition: if (IsExist == null)
in GetWanLineDisconnectionData()
is true and the return block is executing, even though IsExist is False and not null.
Why is that?
How can I overcome this problem?
private void GetWanLineDisconnectionData()
{
string q = "SELECT WanLineDiscconection FROM AdditionalProjectsData WHERE SpCall= " + "'" + spCall + "'";
object IsExist = Orange.ProjectManagment.DAL.Database.GenericScalar<object>(q);
if (IsExist == null) {
return;
}
if (bool.Parse(IsExist) == true) {
RadWanDiscYes.Checked = true;
} else {
RadWanDiscNo.Checked = true;
}
}
Database method:
public static T GenericScalar<T>(string query)
{
OleDbConnection connection = new OleDbConnection(sqlConnString);
connection.Open();
OleDbCommand cmd = new OleDbCommand(query, connection);
try
{
var result = cmd.ExecuteScalar();
if (result == null)
{
return default(T);
}
else
{
return (T)result;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
CloseConnection(ref connection);
}
}
EDIT:
maybe a few screen shoots will better demonstrate it:
(note that: GetWanLineDisconnectionData()
is written in VB.NET and GenericScalar()
is written in C# on a different project in the solution):