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?