MaterialSkin icon indicating copy to clipboard operation
MaterialSkin copied to clipboard

Error with ComboBox while using DataSource

Open Katalijst opened this issue 5 years ago • 10 comments

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

Katalijst avatar Feb 24 '20 09:02 Katalijst

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 :)

leocb avatar Feb 25 '20 13:02 leocb

I'm using the 2.1.2 versions from NuGet Manager

Katalijst avatar Feb 27 '20 10:02 Katalijst

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.

leocb avatar Feb 28 '20 14:02 leocb

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);
    }

Suda0518 avatar Mar 02 '20 13:03 Suda0518

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

leocb avatar Mar 03 '20 14:03 leocb

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:

deepakchhapru avatar Jun 01 '20 17:06 deepakchhapru

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

leocb avatar Jun 01 '20 18:06 leocb

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.

deepakchhapru avatar Jun 02 '20 08:06 deepakchhapru

any updates on this issue.

sabodh avatar Nov 18 '20 04:11 sabodh

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.

leocb avatar Nov 18 '20 04:11 leocb