question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[.NET 7.0.1] InvalidCastException in ErrorProvider

See original GitHub issue

.NET version

7.0.1

Did it work in .NET Framework?

Yes

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

System.InvalidCastException exception is thrown in ErrorProvider.

Steps to reproduce

  1. Create a .NET 7.0 WinForm app.
  2. Replace Form1.cs with the following code
  3. Launch the app.
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            var model = new MyModel();
            this.DataBindings.Add("Text", model, "Error");
            new ErrorProvider(this).DataSource = model; // InvalidCastException !!!
        }
    }

    public class MyModel : System.ComponentModel.IDataErrorInfo
    {
        public string this[string columnName] => "";
        public string Error => "";
    }
}

Excepted Result: No exception thrown.

Actual Result: This exception is thrown in code:

System.InvalidCastException HResult=0x80004002 Message=Unable to cast object of type ‘System.Collections.Generic.KeyValuePair`2[System.Windows.Forms.Control,System.String]’ to type ‘System.Collections.DictionaryEntry’. Source=System.Windows.Forms Stack Trace: at System.Windows.Forms.ErrorProvider.ErrorManager_CurrentChanged(Object sender, EventArgs e) at System.Windows.Forms.ErrorProvider.SetErrorManager(Object newDataSource, String newDataMember, Boolean force) at WinFormsApp1.Form1…ctor() (Form1.cs):line 13 at WinFormsApp1.Program.Main() (Program.cs):line 14

Other information https://github.com/dotnet/winforms/pull/7037

// Hashtable controlError = new Hashtable(bindingsCount);
Dictionary<Control, string> controlError = new(bindingsCount);
...
IEnumerator enumerator = controlError.GetEnumerator();
while (enumerator.MoveNext())
{
    DictionaryEntry entry = (DictionaryEntry)enumerator.Current; // Invalid Cast !!!
    SetError((Control)entry.Key, (string?)entry.Value);
}

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gpetroucommented, Dec 27, 2022

Sorry, I should have checked that part of the code when I changed it. I added a small PR with a fix.

0reactions
Ashley-Licommented, Feb 14, 2023

Verified with .NET 8 Preview 1 TP build: .NET 8.0.100-preview.1.23108.10, issue was fixed as same as above test result.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ErrorProvider Class (System.Windows.Forms)
Provides a user interface for indicating that a control on a form has an error associated with it.
Read more >
InvalidCastException Class (System)
An InvalidCastException exception is thrown when the conversion of an instance of one type to another type is not supported. For example, attempting...
Read more >
c# - Problem with ErrorProvider.Clear()
I have one problem. I validate two texboxs. If texbox are not validate I show error message with error provider. Situation : tbAzetId.Text=" ......
Read more >
Errorprovider c# winforms
Im working with errorprovider in a c# winforms application. Now I want to have a "double" validation. Once on the textfields directly, ...
Read more >
Sejak Visual Basic 1.0 muncul pada tahun 1991 ...
Pada tahun 2001 Microsoft memperkenalkan teknologi.net Framework, dan Visual ... As String = "erick" 'menghasilkan error invalid cast exception Dim bil As ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found