System.InvalidCastException: Object cannot be cast from DBNull to other types

It's winform application and not using database in the project, yet getting System.InvalidCastException: Object cannot be cast from DBNull to other types. Is there any chance this exception is coming from SDK?

public virtual void OnCallConnected(EventArgs e) {
  mForm.Invoke(mLibrary.OnCallConnected, new object[] {
    this,
    e
  }); // Exception is occuring here
}.

public Main(){
    Integration.CallConnected += OnCallConnected;
}
private void OnCallConnected(object sender, EventArgs e)
        {
            log.Debug("OnCallConnected");

            CallConnectedEventArgs theArgs = (CallConnectedEventArgs)e;

            this.tbCallConnectedConversationID.Text = theArgs.ConversationID;
            this.tbCallConnectedRemotePhoneNumber.Text = theArgs.RemotePhoneNumber;
            this.tbCallConnectedDirection.Text = theArgs.Direction;
            this.tbCallConnectedAccountID.Text = theArgs.AccountID;

            this.lbCallConnectedContactList.Items.Clear();
            List<string> theContactAsString = GetNameValueList(theArgs.ContactList);
            if (theContactAsString.Count > 0)
            {
                this.lbCallConnectedContactList.Items.AddRange(theContactAsString.ToArray());
            }

        }

        List<string> GetNameValueList(List<KeyValuePair<string, string>> inList)
        {
            List<string> result = new List<string>();
            if (inList != null && inList.Count > 0)
            {
                foreach (KeyValuePair<string, string> li in inList)
                {
                    result.Add(li.Key + " - " + li.Value);
                }
            }

            return result;
        }

Hi,

I can't be sure without a stack trace. If you can attach a stack trace to this post I'll be able to debug it for you.

stack trace does not provide much information.
[2021-07-22 18:44:32,082] [ERROR:Notification Exception ex :> System.InvalidCastException: Object cannot be cast from DBNull to other types.

at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)

at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)

at MyIntegrationLibrary.IntegrationImplementation.OnCallConnected(EventArgs e)

at MyIntegrationLibrary.ManageStateCall.FireCallConnected(Conversation inConversation)

at MyIntegrationLibrary.ManageStateCall.ManageNotification(Conversation inConversation)

at MyIntegrationLibrary.IntegrationImplementation.NotificationReceivedHandler(INotificationData notificationData)]

I doubt it's coming from the SDK. There are no references to DBNull in the SDK.
My best guess is there's a cast in OnCallConnected from DBNull to another type and that's causing the exception. Possibly the line

CallConnectedEventArgs theArgs = (CallConnectedEventArgs)e;

but I can't be certain.

Hi @anon11147534
How to solve this problem?

the CallConnectedEventArgs have following properties and its values come from the contact list of the campaign from Genesys cloud.
This exception does not occur often and I am not able to reproduce it.

   public string ConversationID;
    public string RemotePhoneNumber;
    public string Direction;
    public string AccountID;
    public List<KeyValuePair<string, string>> ContactList;

Hi,

You'll need to find the code that's casting from DBNull to another type and run a check for its type before casting. I'm not a C# expert but it looks like there are several different ways to check the type of an object:

  • typeof takes a type name (which you specify at compile time).
  • GetType gets the runtime type of an instance.
  • is returns true if an instance is in the inheritance tree.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.