I’m working on converting some console code in a Microsoft sample for Dynamics CRM SDK 2011 to an ASP.NET web application. It runs fine in console mode, but after converting to run in ASP.NET I get the error:
A first chance exception of type ‘System.InvalidCastException’ occurred in Microsoft.VisualBasic.dll
Here is my source code:
Public Sub Run(ByVal connectionString As String, ByVal promptforDelete As Boolean)
Try
Dim connection As Microsoft.Xrm.Client.CrmConnection =
CrmConnection.Parse(connectionString)
_orgService = New OrganizationService(connection)
Using _orgService
Dim account_Renamed As Account = New Account With {.Name = "Fourth Coffee"}
_accountId = _orgService.Create(account_Renamed)
Response.Write(CType("{0} {1}", Char()), account_Renamed.LogicalName, account_Renamed.Name)
Dim cols As New ColumnSet(New String() {"name",
"address1_postalcode",
"lastusedincampaign"})
Dim retrievedAccount As Account = _orgService.Retrieve("account", _accountId, cols).ToEntity(Of Account)()
Response.Write("Retrieved record.")
retrievedAccount.Address1_PostalCode = "98052"
Response.Write("Updated Account Postal Code.")
End Using
Catch ex As Exception
End Try
End Sub
So, I figured it out. In a web-based application there is no need to use {0} {1}:
Instead it can look like this: