leocb / MaterialSkin

Theming .NET WinForms, C# or VB.Net, to Google's Material Design Principles.
MIT License
439 stars 131 forks source link

Error with ComboBox while using DataSource #61

Open Katalijst opened 4 years ago

Katalijst commented 4 years ago

When setting DataSource of a combobox, I get an error System.NullReferenceException as soon as I click on the combobox.

Here is my code, working for regular .Net combobox :

    Public Sub RefreshPosition()
        Dim cmd As New MySqlCommand
        Dim dt As New DataTable
        Dim da As New MySqlDataAdapter
        Dim sql As String

        Try
            sql = "Select * from Position"
            With cmd
                .Connection = connecter()
                .CommandText = sql
            End With
            da.SelectCommand = cmd
            da.Fill(dt)

            cmbLoc.DataSource = dt
            cmbLoc.ValueMember = dt.Columns(0).ToString
            cmbLoc.DisplayMember = strTitlePNom

            connecter().Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
leocb commented 4 years ago

Hi, I believe this is related to what #55 fixed, I released it in the lastest version, are you using the latest? Try that and report back please :)

Katalijst commented 4 years ago

I'm using the 2.1.2 versions from NuGet Manager

leocb commented 4 years ago

hmm, ok, good to know. Could you get the stack trace of the error and post it here please? otherwise I have no way to know where it's failing.

Suda0518 commented 4 years ago

i have a question ,when i clsoing winform ,the form was closed ,but the process not stop,it was still runing,disposing was not disposed , it break at below :

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
leocb commented 4 years ago

I see you opened an issue (#65) for that last comment, We'll continue the discussion about it there

deepakchhapru commented 4 years ago

Hi @leocb , When Datasource property is set for combobox and after selecting value from list, when control gets focus again I get Null Reference Error.

Below is stackTrace

System.NullReferenceException was unhandled HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=MaterialSkin StackTrace: at MaterialSkin.Controls.MaterialComboBox.CustomDrawItem(Object sender, DrawItemEventArgs e) at System.Windows.Forms.ComboBox.OnDrawItem(DrawItemEventArgs e) at System.Windows.Forms.ComboBox.WmReflectDrawItem(Message& m) at System.Windows.Forms.ComboBox.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m) at System.Windows.Forms.Control.WmOwnerDraw(Message& m) at System.Windows.Forms.Control.WmDrawItem(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ContainerControl.WndProc(Message& m) at System.Windows.Forms.Application.ParkingWindow.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.NativeWindow.DefWndProc(Message& m) at System.Windows.Forms.Control.DefWndProc(Message& m) at System.Windows.Forms.Control.WmSetFocus(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ComboBox.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) InnerException:

leocb commented 4 years ago

Thanks for posting the stack trace it seems to be related to the debuger somehow(?), try it on a release compile and see if it still happens also, what version of the .net are your using? the lib is compiled on 4.6.2 and is know to have issues with version 4.7.x

deepakchhapru commented 4 years ago

The was same issue with MaterialSkin.dll when downloaded from nuget.org. I then downloaded the code from github compiled in debug mode and tried to find out on which line it is giving me error.

Error is in Class: "MaterialComboBox.cs" Method: "CustomDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)" on below line:

if (!string.IsNullOrWhiteSpace(DisplayMember)) { Text = Items[e.Index].GetType().GetProperty(DisplayMember).GetValue(Items[e.Index], null).ToString(); }

My application is compiled in 4.6.1

And Your code (downloaded from git) seems to be compiled in 4.6.1.

sabodh commented 3 years ago

any updates on this issue.

leocb commented 3 years ago

I haven't fixed this one yet; I don't use the material skin with a database and I don't plan to install one on my development computer just to debug this issue. If anyone wants to fix the issue and open a PR, I'd be more than happy to update the nugget package.

ainwood commented 1 day ago

FYI, I had this issue when trying to use an Enum as the datasource. It loaded and displayed fine, and was only when the down arrow was clicked that I got the Null Reference Exception. Turns out that I'd set the DisplayMember = "NAME" (i.e. Upper Case) Changing it to DisplayMember="Name" (i.e. Proper case) fixed it.