Click here to Skip to main content
15,867,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have declared two table. One is called Artists and has columns like
ArtistID, ArtistDisplayName, ArtistFirstName etc.

The second table is called MusicBoxsets and has columns like
MusicBoxsetIndex, MusicBoxsetArtistIndex etc

MusicBoxsetIndexArtistIndex contains the value ArtistID from the first table.

I want to display the ArtistDisplayname (from Artists table) and 4 columns from the MusicBoxsetIndexArtists table. These items are to be displayed on a datagridview.

What I have tried:

This is the code I have tried
Dim objBoxsetDataSet As New DataSet()
Dim objBoxsetDataAdapter As New SqlDataAdapter()

objBoxsetDataAdapter.SelectCommand.CommandText =
    "Select        ArtistDisplayName, MusicBoxsetTheTitle, MusicBoxsetTitle, " &
    "              MusicBoxstPurchaseDate, MusicBoxsetPurchasePrice " &
    "     From     Artists" &
    "     Join     MusicBoxsets On Artists.ArtistID = MusicBoxsetArtistIndex" &
    "     Order By MusicBoxsets.MusicBoxsetTitle"

objBoxsetDataAdapter.SelectCommand.CommandType = CommandType.Text

Try
    objCatalogueConnection.Open()
    objBoxsetDataAdapter.Fill(objBoxsetDataSet, "Artists")

Catch myException As System.Exception
    MessageBox.Show(myException.Message)
Finally
    objCatalogueConnection.Close()
End Try

'   Set the DatagridView properties to bind to this data
grdMusicBoxsets.AutoGenerateColumns = True
grdMusicBoxsets.DataSource = objBoxsetDataSet
grdMusicBoxsets.DataMember = "Artists"


When I run the code I get an exception on the SQL Select command saying
System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Data.SqlClient.SqlDataAdapter.SelectCommand.get returned Nothing.

Any ideas on how to fix this please?
Posted
Updated 13-Apr-21 10:19am
Comments
SeanChupas 13-Apr-21 14:12pm    
Something is null and you just need to debug it to find out. My guess is objBoxsetDataAdapter.SelectCommand is null.

1 solution

You never mentioned what line throw the exception.

The problem is you're assuming something has a valid object when it doesn't.

You're creating a new DataAdapter, but you never set up the SelectCommand with a connection object.

Just opening some connection object in the same room as a DataAdapter doesn't make the adapter use that connection.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900