Solved

What's wrong with this block of code?

Posted on 2007-11-23
10
201 Views
Last Modified: 2010-04-01
Writing an ODBC application in C++ using the Win32 API.

The system sets the environment handles and connects to the DSN successfully.  However, the call to SQLExecDirect() is returning an error code, but the error handling block's call to SQLGetDiagRec() is not giving me any information about what the error is (A dialog with no information pops up).  I have included the relevant code block.  My main concern is the error handling block.  If I can at least figure out what the error is, I can troubleshoot the SQLExecDirect() problem myself.

Thanks!
retCode=SQLExecDirect(hStmt, (SQLCHAR*)spQuery, SQL_NTS);	//Execute discovery query

if(retCode!=SQL_SUCCESS && retCode!=SQL_SUCCESS_WITH_INFO)

{

	SQLCHAR sqlState[6]="";

	SQLINTEGER nativeError=NULL;

	SQLCHAR errMsg[SQL_MAX_MESSAGE_LENGTH]="";

	int i = 1;

	char message[512]="";

	SQLGetDiagRec(SQL_HANDLE_DBC, hDbc, i, sqlState, &nativeError, errMsg, sizeof(errMsg), NULL);

	sprintf(message, "Diag: %d, SQLSTATE: %s NativeError: %d ErrMsg: %s", i++, sqlState, nativeError, errMsg);

	MessageBox(NULL, message, "Err", MB_OK);

}

Open in new window

0
Comment
Question by:cuziyq
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 20338879
When you say "no information" can you be more specific please? An empty dialog or text without the error details filled in?
0
 
LVL 22

Expert Comment

by:grg99
ID: 20338915
Try initializing the error info fields with "foo", 999, and "goo".  Perhaps the get diag call is failing.  Does that function perhaps return its own error code that you could check?
0
 
LVL 14

Author Comment

by:cuziyq
ID: 20338957
No information.  In the if{} block, I have sqlState, nativeError, and errMsg all intialized to empty strings.  The call to SQLGetDiagRec() is supposed to fill those variables with the relevant information.  Then a message box comes up displaying the values of those variables.

The dialog box is coming up with nothing.  It says "Diag: 1, SQLSTATE: , NativeError: , ErrMsg: " with an OK button.  Setting a breakpoint at the MessageBox() line confirms that all the strings are still empty.

The block does work correctly in other contexts.  If I misspell the password or the DSN name, for example, the error pops up telling me exactly what is wrong.  It all runs through the same function.  This is the only case where I get nothing, and I don't know why.
0
 
LVL 20

Expert Comment

by:ikework
ID: 20339234
you assume there was an error, but you didnt check the return value of SQLGetDiagRec.

adapted from http://msdn2.microsoft.com/en-us/library/ms711727.aspx:


SQLRETURN rc2;

i = 1;

while ((rc2 = SQLGetDiagRec(SQL_HANDLE_DBC, hDbc, i, sqlState, 

                            &nativeError, errMsg, sizeof(errMsg), NULL)) 

                            != SQL_NO_DATA) 

{

    sprintf(message, "Diag: %d, SQLSTATE: %s NativeError: %d ErrMsg: %s", 

            i++, sqlState, nativeError, errMsg);

    MessageBox(NULL, message, "Err", MB_OK);

    ++i;

}
 
 

// ike

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 14

Author Comment

by:cuziyq
ID: 20339371
Well, folks, I figured out the source of my error.  My Stored Procedure has an ORDER BY clause on a SELECT COUNT(*) query, which is illegal.  I fixed the stored procedure and it now works fine.  I am sure I will revisit the issue later when I have some other stored procedure that doesn't work properly.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20339451
You should be able to order by count as a named field...

select count(*) as cnt from sometable group by somefield order by cnt;
0
 
LVL 20

Expert Comment

by:ikework
ID: 20339468
>> You should be able to order by count as a named field...

some rdbms allow it .. some not .. mostly "order by count(*)"  works
0
 
LVL 14

Accepted Solution

by:
cuziyq earned 0 total points
ID: 20341525
One more thing for those who are curious . . . the error handling block does not work because the call to SQLGetDiagRec() was passing in the connection handle.  It worked before because when I deliberately misspelled the password or the DSN, it was a connection issue.  However, when I messed up the query and called SQLGetDiagRec(), it was no longer a connection issue.  SQL_HANDLE_DBC is inappropriate for diagnosing a statement error.  Passing in the statement handle (SQL_HANDLE_STMT) is necessary to get error information about a failed query.  When I chaged it, I get a full, plain English text error message in all its glory.
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 20437964
Closed, 500 points refunded.
modus_operandi
EE Moderator
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now