-
November 23rd, 2010, 08:12 AM
#1
[SOLVED]Checking for no return data on query call
Hey guys, long story short i'm querying an access database for a serial number by a given host name and it is failing to correctly identify when the query returns no match (when the host name is not in the database)
The following is an excerpt code block that executes the query and checks the returned data:
Code:
// Prepare SQL query to grab serial number of host
//host variable is a _bstr_t that is given a value earlier
_bstr_t query = "SELECT [SerialNumber] FROM [Computer Inventory] WHERE [ComputerName] = \"" + host + "\";";
// Excecute the query
DAO::RecordsetPtr pRS = NULL;
pRS = pDbPtr->OpenRecordset(query, _variant_t(DAO::dbOpenDynaset));
//create record set, check for bad set
if (pRS && 0 < pRS->RecordCount)
{
DAO::FieldsPtr pFields = NULL;
pFields = pRS->GetFields();
if (pFields && pFields->Count == 0)
{
cout<<DAM<<": Error: Number of fields in the result set is 0."<<endl;
}
// get actual data
//check for VARIANT = VT_NULL and VARIANT = VT_EMPTY
if ((pFields->GetItem(0)->GetValue().vt != 1) && (pFields->GetItem(0)->GetValue().vt != 0))
serial = (_bstr_t)(pFields->GetItem(0)->GetValue());
else{
cout << "\nComputer has no record in database!";
return "99/99/9999";//return no date identifier (ignore)
}
}
I checked for VT_NULL and VT_EMPTY, is their another value the VARIANT could be if the query finds no matches in the database?
Last edited by entropy01; November 23rd, 2010 at 09:40 AM.
Reason: Solved
-
November 23rd, 2010, 08:45 AM
#2
Re: Checking for no return data on query call
if there is no data returned, it might be lost after casting
-
November 23rd, 2010, 09:39 AM
#3
Re: Checking for no return data on query call
Thanks it was a good though but I found the mistake...
This line "if (pRS && 0 < pRS->RecordCount)"
actually is the first check that concludes there was no data returned, and since there was no accompanying else for that if, there was no action being taken against no results!
The second check for no data:
if ((pFields->GetItem(0)->GetValue().vt != 1) && (pFields->GetItem(0)->GetValue().vt != 0))
Never got evaluated as essentially that case was already checked.
Thanks guys!
-
November 23rd, 2010, 10:48 AM
#4
Re: Checking for no return data on query call
I guess you have to do before getting pRS->RecordCount.
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|