none
BindingSource Problem RRS feed

  • Question

  • I've been away from programming for almost a year and now I'm almost lost.

    I hardly use databinding and bindingsources but decided to try it out based on this MSDN link.

    Here is my code (I get "Object reference not set to an instance of an object." error on the line indicated ie bindingSource1.DataSource = employeeTable). Could someone please tell me what I am doing wrong:


    Imports System.Data.SqlClient
    
    Public Class Form1
    
        Private bindingSource1 As BindingSource
    
       Private employeeTable As New DataTable
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim connectionString As String = "Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
    
           Dim connection As New SqlConnection()
    
            connection.ConnectionString = connectionString
    
            Dim employeeAdapter As New SqlDataAdapter _
    
                (New SqlCommand("Select * from Employees", connection))
    
            connection.Open()
    
            employeeAdapter.Fill(employeeTable)
    
            ' Set the DataSource property of the BindingSource to the employee table.
    
            bindingSource1.DataSource = employeeTable 'Error HERE
    
           ' Set up the binding to the ReportsTo column.
    
            Dim reportsToBinding As Binding = _
    
                TextBox2.DataBindings.Add("Text", bindingSource1, "ReportsTo", _
    
                    True)
    
    
            ' Set the NullValue property for this binding.
    
    
           reportsToBinding.NullValue = "No Manager"
    
    
           ' Set up the remaining binding.
    
            TextBox1.DataBindings.Add("Text", bindingSource1, "LastName", True)
    
        End Sub
    
    
    End Class
    
    
    
    
    
    
    
    

    Only performance counts!
    Friday, January 15, 2010 9:01 PM

Answers




  •     Private bindingSource1 As BindingSource '= New BindingSource();
        Private employeeTable As New DataTable



    Remove the comment, and it should work.
    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, January 15, 2010 9:08 PM

All replies




  •     Private bindingSource1 As BindingSource '= New BindingSource();
        Private employeeTable As New DataTable



    Remove the comment, and it should work.
    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, January 15, 2010 9:08 PM
  • Thanks, Rudedog2.

    Does it mean that I should take some contents of the MSDN library with a pinch of salt (or, a pint of vodka)?
    Only performance counts!
    Friday, January 15, 2010 9:13 PM