VB->Option strict and LINQ result - vb.net

I was recently introduced to LINQ and got a sample how to search for error messages after executing a query against a database. The code looks like this:
Dim errors As Object = From e In execution.Messages _
Where e.MessageType = 120 Or e.MessageType = 110
Select e.Message
If errors IsNot Nothing Then
For Each m As OperationMessage In errors
LogToError("ExecutePackage->Error message: " & m.Message)
sbError.AppendLine(m.Message)
Next
End If
Now, when I use Option strict ON I get an error on the "errors" object on this line:
For Each m As OperationMessage In errors
This is natural for me. So, I tried to change the code to this:
For Each m As OperationMessage In CType(errors, OperationMessageCollection)
Now, when I run it I get this error:
Unable to cast object of type 'WhereSelectEnumerableIterator`2[Microsoft.SqlServer.Management.IntegrationServices.OperationMessage,System.String]' to type 'Microsoft.SqlServer.Management.IntegrationServices.OperationMessageCollection'.
So, it seems to me that converting a LINQ query to another type at runtime does not work? What is the proper way of doing this and keeping Option strict ON?

Related

make SQL query fail on condition

For XYZ reason I need a query to explicitly fail (return error code to connection) if some condition is met (on Snowflake).
Can someone recommend an approach?
Some illustration in pseudo-code:
IF 0= ( SELECT COUNT(*) FROM XYZ) THEN FAIL
I like Simeon's approach, but you may want a custom error message if this is running in a long script. Throwing an error in a JavaScript UDF will allow custom (if untidy) error messages:
create or replace function RAISE_ERROR(MESSAGE string)
returns string
language javascript
as
$$
throw "-->" + MESSAGE + "<--";
$$;
select
case (select count(*) from XYZ)
when 0 then raise_error('My custom error.')
else 'There are rows in the table'
end
;
If there are no rows in XYZ, it will generate an error message that reads:
JavaScript execution error: Uncaught --> My custom error <--. in RAISE_ERROR at '
throw MESSAGE;' position 4 stackstrace: RAISE_ERROR line: 2
It's not the tidiest of error messages, but it will allow you to embed a custom error message if you need help identifying the error. The arrows should help direct people to the real error message thrown in the stack.
SELECT IFF(true, 1::number, (1/0)::number);
then:
IFF(TRUE, 1::NUMBER, (1/0)::NUMBER)
1
where-as
SELECT IFF(false, 1::number, (1/0)::number);
gives:
Division by zero

VFP. Trapping COM errors

Trapping Excel COM errors from a VFP program.
A program is writing out an extract from several large tables to Excel using Automation.
Typical instructions include :
lCell = "I1234”
.sSheet1.Range(lCell).value = cCust.Name
This generally works fine, but if cCust.name has a value such as ‘=’ (which Excel does not like), this causes an OLE COM error.
Had tried using TRY .. . CATCH . . . ENDTRY to trap this error, but the OLE COM error still reports.
I suppose that I could find out all the possible errors in the data and clean them up, but is there any way of getting Automation to ignore the error and carry on (leaving the field unpopulated)?
Thank you.
I am adding this as an answer, since it contains some code that would otherwise cause a mess. With ON error it is working perfectly fine for me. ie:
*** Constant Group: XlSaveAction
#Define xlDoNotSaveChanges 2
#Define xlSaveChanges 1
Local loExcel As Excel.Application
Local lcValue
loExcel = Createobject('Excel.Application')
loExcel.Workbooks.Add()
loExcel.ActiveWorkbook.SaveAs('c:\temp\ExcelReadTest.xlsx')
loExcel.DisplayAlerts = .F.
*loExcel.Visible = .T.
On Error lcValue = .Null.
With loExcel.ActiveWorkbook.ActiveSheet
.Range('A5').Value = '=Like me'
lcValue = .Range('A5').Value
? m.lcValue
Endwith
On Error
loExcel.ActiveWorkbook.Close(xlSaveChanges)
loExcel.Quit()

Can not catch error in merge statement

Hi I have problem to catch the error in merge statement.
The merge statement will get the following error when run in SQL Plus:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
When I execute the statement with ExecuteNonQuery in Vb.net will not get any exception.
Is there any way to catch that error in Vb.net without using log errors in merge statement?
EDIT:
Try
Dim count as Interger=0
Dim cmd As New OleDb.OleDbCommand(strSQL)
count = cmd .ExecuteNonQuery()
Catch ex as Exception
... Respond ...
End Try
Sample Merge Statement with error:
MERGE INTO TABLE_A a USING
(
SELECT ID,SUM(AMOUNT) AMOUNT
FROM TABLE_B b
GROUP BY ID
) b
ON ( b.ID=a.ID )
WHEN MATCHED THEN
UPDATE SET a.AMOUNT=a.AMOUNT+b.AMOUNT
WHEN NOT MATCHED THEN
INSERT (ID, DT, AMOUNT) VALUES (b.ID, 0, b.AMOUNT);
The SQL Plus will get error at the insert but Vb.net can not catch.
The data type for field DT is DATE.
EDIT 3:
My bad - in that case, I think you'll have to subscribe to oleDbConnection.InfoMessage event and check for applicable SQL-00932 messages after the MERGE command. I don't see any equivalent property to FireInfoMessageEventOnUserErrors in OLE-DB (which, again, seems totally backwards...).
EDIT 2:
It appears there's a default threshold that will cause vb.net to ignore certain SQL exceptions (can't fathom the rationale behind such a decision). Regardless, I think if you do the following, you should see the exception raised.
cmd.FireInfoMessageEventOnUserErrors = true;
End EDIT2
EDIT: I just read the problem a bit closer. If the vb.net execution is not throwing any exception at all, that would indicate that you're not running the exact same statement in both. My initial guess is that you're passing a date parameter that is passing SQL Plus string variable incorrectly as 01/01/2015 (which is 1 divided by 1 divided by 2015) and vb.net variable correctly as '01/01/2015' (that is, as a string with quotation marks).
End EDIT
I have no idea what you mean by "using log errors in merge statement", but it sounds like you just need the syntax for using the vb.net try-catch syntax to catch and act on Oracle errors.
From here: http://www.tek-tips.com/viewthread.cfm?qid=467119
Pertinent code snippet:
Try
... Sql here ...
Catch ex as OleDbException
... Respond to error here ...
End Try

Invalid character value for cast specification sometimes

Im currently investigating a bug that just happens sometimes. I have not been able to see any patterns to the behavior yet. I have a vb6 com+-application that communicates with the server.
Err: Invalid character value for cast specification. (&H80040E21)
Src: Microsoft OLE DB Provider for SQL Server
Stacktrace:
GetClientRS("SELECT * FROM Visit where GUID=?")
GetVisit("('{5EF4A26A-0909-4371-97F7-A2597CB7ADE3}')")
This is the error that happens sometimes. Maybe 1-2 times for every 5 times it is called. I have tried a trace in SQL server profiler but I am unable to detect any errors.
Does anyone have any ideas I could try?
Edit:
Code for calling the command:
GetVisit:
Set cmd = CreateCommand("SELECT * FROM dbo.per_Visit WHERE GUID=?")
cmd.Parameters.Append cmd.CreateParameter(, adGUID, , , SafeGUID(sGUID))
Set GetVisit = GetClientRS(cmd, True)
SafeGUID:
If Mid$(sGUID, 1, 1) <> "{" Then
sGUID = "{" & sGUID & "}"
End If
SafeGUID = sGUID
SafeGUID function is probably not clever enough.
When
sGUID = "('{5EF4A26A-0909-4371-97F7-A2597CB7ADE3}')"
the value actually passed as parameter is
{('{5EF4A26A-0909-4371-97F7-A2597CB7ADE3}')}
which is not a valid GUID.

VB.Net "COMException was unhandled" Error

I have a VB.Net program written with .Net 2.0
I'm trying to write a code that logs entire registry entries into a log file.
I have got it to work wit the following code.
http://pastebin.com/kmA63cUe
However whenever I try to add an additional Registry key to log
LogPrint4("<-----------------HKLM/WebCheck-------------------->")
If reg.enumvalues(&H8000002, "Software\Microsoft\Windows\CurrentVersion\WebCheck", keys2, types) = 0 Then
If IsArray(keys2) Then
For p = 0 To UBound(keys2)
reg.getstringvalue(&H8000002, "Software\Microsoft\Windows\CurrentVersion\WebCheck", keys2(p), value)
LogPrint4(keys2(p) & "=" & value)
Next
End If
End If
It comes up with the following error
COMException was unhandled Check the ErrorCode Property of the
exception to determine the HRESULT returned by the COM object.
I'm not sure why it is limiting it only to 5 checks.
Anyone have any ideas?
Consider using the built-in Registry support instead of the COM libraries. At a minimum, you'll get clearer error messages. It is supported in .NET 2.0.
http://msdn.microsoft.com/en-us/library/microsoft.win32.registry(v=vs.80).aspx
Good luck!