user22579796
user22579796

Reputation:

object reference not set to an instance of an object in combobox when cast the bindingsource to the datagridview in VB.NET

I tried to cast the bindingsource to the datagridview but there was an error, the problem was in the combobox binding. Below, if I use the first code option, there is no problem error, but if I use the second code option, there is a problem error object reference not set to an instance of an object. How can I keep using the second code option please guide me.

Thanks

first code option

Public Class Form2
    Private bindingSource As BindingSource = Nothing
Public Sub New()
        InitializeComponent()
        BindcomboboxJob1()
        ComboBox1.DropDownHeight = 80
        ComboBox1.Text = "Job1"
    End Sub
Public Sub New(Transno As People)
        Me.New
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of People)(CType(pservice.GetPeopleview(Transno.Transno), IList(Of People)))}
        DataGridView1.DataSource = bindingSource
    End Sub
Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
        Dim job1 = ComboBox1.Text.Trim()
 Try
            Dim new_product = New People With {
            .Transno = TextBox1.Text,
            .Username = TextBox2.Text,
            .Job1 = job1}
            Dim bs = TryCast(bindingSource.DataSource, BindingList(Of People))
  bs.Add(new_product)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Private Sub BindcomboboxJob1()
        If ComboBox1.DataSource IsNot Nothing Then Return
        Cursor.Current = Cursors.WaitCursor
        ComboBox1.DisplayMember = "Job1"
        ComboBox1.ValueMember = "Job1"
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of Job1)(CType(pservice.GetByJob1, IList(Of Job1)))}
        ComboBox1.DataSource = bindingSource
        Cursor.Current = Cursors.Default
    End Sub
End Class

second code option

Public Class Form2
    Private bindingSource As BindingSource = Nothing
 Public Sub New()
        InitializeComponent()
        ComboBox1.DropDownHeight = 80
        ComboBox1.Text = "Job1"
Public Sub New(Transno As People)
        Me.New
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of People)(CType(pservice.GetPeopleview(Transno.Transno), IList(Of People)))}
        DataGridView1.DataSource = bindingSource
    End Sub
'error in the code below when using the button event to add to the datagridview

Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
        Dim job1 = ComboBox1.Text.Trim()
 Try
            Dim new_product = New People With {
            .Transno = TextBox1.Text,
            .Username = TextBox2.Text,
            .Job1 = job1}
            Dim bs = TryCast(bindingSource.DataSource, BindingList(Of People))
  bs.Add(new_product)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Private Sub BindcomboboxJob1()
        If ComboBox1.DataSource IsNot Nothing Then Return
        Cursor.Current = Cursors.WaitCursor
        ComboBox1.DisplayMember = "Job1"
        ComboBox1.ValueMember = "Job1"
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of Job1)(CType(pservice.GetByJob1, IList(Of Job1)))}
        ComboBox1.DataSource = bindingSource
        Cursor.Current = Cursors.Default
    End Sub
 Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown

        BindcomboboxJob1()

    End Sub

    Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown

        BindcomboboxJob1()
    End Sub
End Class

Upvotes: 0

Views: 51

Answers (1)

user22579796
user22579796

Reputation:

This is due to help and recommendations from @dr.null . I thank you very much

Public Class Form2
    Private bindingSource As BindingSource = Nothing
    Private bindingSource1 As BindingSource = Nothing
 Public Sub New()
        InitializeComponent()
        ComboBox1.DropDownHeight = 80
        ComboBox1.Text = "Job1"
Public Sub New(Transno As People)
        Me.New
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of People)(CType(pservice.GetPeopleview(Transno.Transno), IList(Of People)))}
        DataGridView1.DataSource = bindingSource
    End Sub
'error in the code below when using the button event to add to the datagridview

Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
        Dim job1 = ComboBox1.Text.Trim()
 Try
            Dim new_product = New People With {
            .Transno = TextBox1.Text,
            .Username = TextBox2.Text,
            .Job1 = job1}
            Dim bs = TryCast(bindingSource.DataSource, BindingList(Of People))
  bs.Add(new_product)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Private Sub BindcomboboxJob1()
        If ComboBox1.DataSource IsNot Nothing Then Return
        Cursor.Current = Cursors.WaitCursor
        ComboBox1.DisplayMember = "Job1"
        ComboBox1.ValueMember = "Job1"
        bindingSource1 = New BindingSource With {.DataSource = New BindingList(Of Job1)(CType(pservice.GetByJob1, IList(Of Job1)))}
        ComboBox1.DataSource = bindingSource1
        Cursor.Current = Cursors.Default
    End Sub
 Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown

        BindcomboboxJob1()

    End Sub

    Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown

        BindcomboboxJob1()
    End Sub
End Class

Upvotes: 0

Related Questions