'System.NullReferenceException'

P

Patrick Sullivan

I decided to try using relations and untyped datasets for syncing a datagrid
(child records)with a combobox (parent rows). Also put in stuff I thought I
needed to set up dataviews for filtering. Now the ds won't load objects
(tables) it seems like. I took out the tablemappings and schema actions for
a while but it still had the same problem. No errors from SQL Server.
Profiler says SET FMTONLY OFF; SET NO_BROWSETABLE ON;SELECT * FROM Contacts
SET NO_BROWSETABLE OFF; and SET FMTONLY OFF; SET NO_BROWSETABLE ON;SELECT *
FROM Companies order by compName SET NO_BROWSETABLE OFF; What does all that
mean?? TIA

Private Sub LoadData()
Dim conn As New SqlConnection(ConnectionString)
Dim selectCommand1 As String = "SELECT * FROM Companies order by
compName"
Dim compAdapter As New SqlDataAdapter(selectCommand1, conn)
Dim selectCommand2 As String = "SELECT * FROM Contacts"
Dim contAdapter As New SqlDataAdapter(selectCommand2, conn)
compAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
compAdapter.TableMappings.Add("Tables", "Companies")
contAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
contAdapter.TableMappings.Add("Table", "Contacts")
Try
compAdapter.Fill(ds)
contAdapter.Fill(ds)
conn.Close()
Catch exc As Exception
' Unable to connect to SQL Server
MessageBox.Show(exc.Message, "Error in LoadData()",
MsgBoxStyle.Critical)
End
End Try
DVCompanies.Table = ds.Tables("Companies")

An unhandled exception of type 'System.NullReferenceException' occurred
Additional information: Object reference not set to an instance of an
object.

DVContacts.Table = ds.Tables("Contacts")
DVContacts.RowFilter = "compID=-1"
With ds
.Relations.Add("FK_companyContacts", _
.Tables("Companies").Columns("compID"), _
.Tables("Contacts").Columns("contactCompany"), False)
End With
End Sub
 
W

W.G. Ryan MVP

In the debugger, go through after both fill statements and check your table
counts, make sure you have exactly what you expect and also make sure that
they are named correctly. ALso, which line is throwing the exception?
 
P

Patrick Sullivan

DVCompanies.Table = ds.Tables("Companies") is the line, but if I put the
relations set up before that then THAT shows the same exception. I used the
debugger already but did not look deep enough, I suppose. Thank you.
 
P

Patrick Sullivan

autos
ds:
datasetname = "NewDataSet"
haserrors = False
Tables:
count = 2
isreadonly = False
Item = <cannot view indexed property>

From what I can see in the Autos debug window, 2 tables are in ds which is
correct. But I can't see the names.
 
P

Patrick Sullivan

I put this fragment in and it returns "Tables exist". But how do I find out
what the tablenames are? Never had a problem like this! How can I query the
datatablecollection for the tablenames?

Dim tablesCol As DataTableCollection

tablesCol = ds.Tables

' Check if the tables exist.

If tablesCol.Count <> 0 Then

MessageBox.Show("Tables exist")

Else

MessageBox.Show("Tables not present")

End If

DVCompanies.Table = ds.Tables(0) //error - no object

DVContacts.Table = ds.Tables("Contacts") //error - no object

DVContacts.RowFilter = "compID=-1"
 
P

Patrick Sullivan

Using the below to find tablenames, I get 2 tables, one named "Table" and
the other named "Contacts" (which is correct for the second one, the other
should be "companies"). yuk.

Dim t As DataTable

For Each t In ds.Tables

If Not (t Is Nothing) Then

MessageBox.Show(t.ToString)

End If
 
P

Patrick Sullivan

Eureka!
Private DVContacts As New DataView

Private DVCompanies As New DataView

I left out the "New" keywords. Problem solved, but the rror messages were
not very helpful!
 
P

Patrick Sullivan

I had 2 causes of errors.

1. in the following tablemappings I had "Tables" instead of "Table"

compAdapter.TableMappings.Add("Table", "Companies")

2. Did not declare dataviews as New
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top