"Error setting up the grid. Object reference not set to an instance of an object"

carlosag

Member
Joined
Feb 14, 2007
Messages
6
Programming Experience
Beginner
"Error setting up the grid. Object reference not set to an instance of an object"

I get this error: "Error setting up the grid. Object reference not set to an instance of an object" whenever i try to select a store name from the combo box in my application

it should display sales data but instead will display this error and it wont display the sales data at all

:confused:
 
This is the code for the form and the datatier

FORM

Imports System.Data
Public Class StoreSalesForm
Private apubsdatatier As pubsdatatier
Private apubsdataset As pubsdataset
Private storesbindingsource As BindingSource
Private salesbindingsource As BindingSource
Private gridinitializedboolean As Boolean

Private Sub StoreSalesForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set up the data for the combo box and text boxes
Try
apubsdatatier = New pubsdatatier
With Me
.apubsdataset = apubsdatatier.getdataset
'Set up stores binding source.
.storesbindingsource = New BindingSource
With .storesbindingsource
.DataSource =
Me.apubsdataset
.DataMember =
"stores"
.Sort = "stor_name"
End With
'Bind the form controls.
With .StoreNameComboBox
.DataSource =
Me.storesbindingsource
.DisplayMember =
"stor_name"
.ValueMember = "stor_id"
.DataBindings.Add("text", storesbindingsource, _
"stor_name", False, DataSourceUpdateMode.Never)
.SelectedIndex = -1
End With
.StoreIDTextBox.DataBindings.Add("text", _
.storesbindingsource,
"stor_id", False, DataSourceUpdateMode.Never)
.CITYTextBox.DataBindings.Add(
"text", _
.storesbindingsource,
"city", False, _
DataSourceUpdateMode.Never)
'Clear initial contents.
.StoreIDTextBox.Clear()
.CITYTextBox.Clear()
'Set up the sales binding source.
.salesbindingsource = New BindingSource
With .salesbindingsource
.DataSource =
Me.apubsdataset
.DataMember =
"sales"
End With
End With
Catch ex As Exception
MessageBox.Show(
"Error:" & ex.Message)
End Try
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub StoreNameComboBox_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles StoreNameComboBox.SelectionChangeCommitted
'Retrieve the sales information for the grid
Dim StoreIDString As String
'Retrieve the ID of the selected store
StoreIDString = StoreNameComboBox.SelectedValue.ToString
'Initialize the grid's binding
If Not gridinitializedboolean Then
'Bind and format the grid
Me.SalesDataGridView.DataSource = salesbindingsource
Setupgridcolumns()
gridinitializedboolean =
True

End If
'Filter the grid's data.
salesbindingsource.Filter = "stor_id = ' " & StoreIDString & "'"
End Sub
Private Sub Setupgridcolumns()
'Set up the columns headings for the grid.
Try
With Me.SalesDataGridView
.Columns!store_ID.Visible =
False
.Columns!ORD_NUM.HeaderText = "Order Number"
.Columns!ord_date.HeaderText = "Date"
.Columns!qty.HeaderText = "Quantity"
.Columns!payterms.HeaderText = "Terms"
.Columns!title_id.HeaderText = "Title ID"
End With
Catch ex As Exception
MessageBox.Show(
"Error setting up the grid. " & ex.Message)
End Try
End Sub
End
Class

DATA TIER CLASS

Imports System.Data
Public Class Pubsdatatier
'Module-Level variables.
Private aSalesTableAdapter As PubsDataSetTableAdapters.salesTableAdapter
Private astoresTAbleAdapter As Pubsdatasettableadapters.storestableadapter
Private apubsdataset As pubsdataset
Public Sub New()
Try
With Me
'Instantiate the TableAdapters and Dataset.
.astoresTAbleAdapter = New Pubsdatasettableadapters.storestableadapter
.aSalesTableAdapter =
New pubsdatasettableadapters.SalesTableAdapter
.apubsdataset =
New pubsDataSet
'Fill Data Set
.aStoresTableAdapter.fill(.apubsdataset.stores)
.aSalesTableAdapter.fill(.apubsdataset.sales)

End With
Catch ex As Exception
Throw ex
End Try
End Sub
Public Function getdataset() As Pubsdataset
'Return Data Set.
Return aPubsdataset
End Function
End
Class
 
VB.NET:
With .StoreNameComboBox
.DataSource = Me.storesbindingsource
.DisplayMember = "stor_name"
.ValueMember = "stor_id"
[COLOR=red].DataBindings.Add("text", storesbindingsource, _[/COLOR]
[COLOR=red]"stor_name", False, DataSourceUpdateMode.Never)[/COLOR]
.SelectedIndex = -1
EndWith


It looks like the problem is with the above code. I'm not sure why you try to set databindings of the type 'Text' when you have already set the datasource property. I don't think that the line i've highlighted in red should be there.
 
Back
Top