1

For me and my partners lab, we set up a .mdf data source complete with tables and data inside of them using SQL statements. We have a form with various labels, and when the index on our combo box is changed, it will update the labels with the info from the database. The issue arises when the index is changed, the seat labels throw a Null Reference Exception when they are attempted to be assigned data sourced from the .mdf file. I apologize if my explanation is not crystal clear, I'm relatively new to VB and this is my first ever post on StackOverflow. I have provided the code below, just curious as to where we are going wrong (Connection? SQL? Reference? Etc.) Thanks a lot for your time, it's due tonight by 11:59pm

FORM.vb

    Public Class Form1

    Private SeatLabels As New ArrayList
    Private CurrentMovie As Integer
    Private MoviePatrons As New Collection
    Private Sub AddLabelsToArray()
        SeatLabels.Add(lblSeatOne)
        SeatLabels.Add(lblSeatTwo)
        SeatLabels.Add(lblSeatThree)
        SeatLabels.Add(lblSeatFour)
        SeatLabels.Add(lblSeatFive)
        SeatLabels.Add(lblSeatSix)
    End Sub


    Private Sub PopulateSeats(MovieIndex As Integer)
        'MsgBox(DBL.Seatings.GetSeatings(MovieIndex).Patron)
        For value As Integer = 0 To 5
            SeatLabels(value) = DBL.Seatings.GetSeatings(MovieIndex).Patron
        Next
    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Theatre_Lab5DataSet.Performances' table. You can move, or remove it, as needed.
        Me.PerformancesTableAdapter.Fill(Me.Theatre_Lab5DataSet.Performances)
        Call AddLabelsToArray() '' Add seats labels to SeatLabels array
    End Sub


    Private Sub cmbMovieTitle_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbMovieTitle.SelectedIndexChanged
        CurrentMovie = cmbMovieTitle.SelectedIndex
        Call PopulateSeats(CurrentMovie)
    End Sub
End Class

DB.vb


    Imports System.Data, System.Data.SqlClient
Namespace DBL
    Public Class Connection
        Public Shared Function GetConnectionString()
            Return My.Settings.Theatre_Lab5ConnectionString
        End Function
    End Class

Public Class Performances
    Public Class FieldNames
        Public Const PerfID As String = "PerfID"
        Public Const perf_Date As String = "perf_Date"
        Public Const Title As String = "Title"
        Public Const Ticket_Price As String = "Ticket_Price"
        Public Const Picture As String = "Picture"
    End Class
#Region "Properties"
    Private _PerfID As Integer
    Public Property PerfID() As Integer
        Get
            Return _PerfID
        End Get
        Set(ByVal value As Integer)
            _PerfID = value
        End Set
    End Property

    Private _perf_Date As String
    Public Property perf_Date() As String
        Get
            Return _perf_Date
        End Get
        Set(ByVal value As String)
            _perf_Date = value
        End Set
    End Property

    Private _Title As String
    Public Property Title() As String
        Get
            Return _Title
        End Get
        Set(ByVal value As String)
            _Title = value
        End Set
    End Property

    Private _Ticket_Price As Integer
    Public Property Ticket_Price() As Integer
        Get
            Return _Ticket_Price
        End Get
        Set(ByVal value As Integer)
            _Ticket_Price = value
        End Set
    End Property

    Private _Picture As String
    Public Property Picture() As String
        Get
            Return _Picture
        End Get
        Set(ByVal value As String)
            _Picture = value
        End Set
    End Property
#End Region

    Public Shared Function GetPerformance(PerfID As Integer) As Performances
        Dim Conn As New SqlConnection(DBL.Connection.GetConnectionString)

        ' Define the SQL
        Dim sqlString As String
        sqlString = "SELECT * FROM Performance WHERE PerfID = @PerfID"

        Dim Command As New SqlCommand(sqlString, Conn)
        Command.CommandType = CommandType.Text

        Dim Parm As New SqlParameter()
        Parm.ParameterName = "@PerfID"
        Parm.Value = PerfID
        Command.Parameters.Add(Parm)

        Dim LocalPerformances As New Performances

        Try
            Conn.Open()

            Dim MyDataReader As SqlDataReader
            MyDataReader = Command.ExecuteReader

            MyDataReader.Read()

            LocalPerformances.PerfID = MyDataReader(FieldNames.PerfID)
            LocalPerformances.perf_Date = CType(MyDataReader(FieldNames.perf_Date), Date)
            LocalPerformances.Title = CType(MyDataReader(FieldNames.Title), String)
            LocalPerformances.Ticket_Price = MyDataReader(FieldNames.Ticket_Price)
            LocalPerformances.Picture = MyDataReader(FieldNames.Picture)

            MyDataReader.Close()
            Conn.Close()
        Catch ex As Exception
            'TODO: Put some code here
            LocalPerformances = Nothing
        End Try

        Return LocalPerformances
    End Function

End Class
Public Class Seatings
    Public Class FieldNames
        Public Const ID As String = "ID"
        Public Const SeatNo As String = "SeatNo"
        Public Const PerfID As String = "PerfID"
        Public Const Patron As String = "Patron"
        Public Const IsPaid As String = "IsPaid"
    End Class
#Region "Properties"
    Private _ID As Integer
    Public Property ID() As Integer
        Get
            Return _ID
        End Get
        Set(ByVal value As Integer)
            _ID = value
        End Set
    End Property

    Private _SeatNo As Integer
    Public Property SeatNo() As Integer
        Get
            Return _SeatNo
        End Get
        Set(ByVal value As Integer)
            _SeatNo = value
        End Set
    End Property

    Private _PerfID As Integer
    Public Property PerfID() As Integer
        Get
            Return _PerfID
        End Get
        Set(ByVal value As Integer)
            _PerfID = value
        End Set
    End Property

    Private _Patron As Integer
    Public Property Patron() As Integer
        Get
            Return _Patron
        End Get
        Set(ByVal value As Integer)
            _Patron = value
        End Set
    End Property

    Private _IsPaid As Boolean
    Public Property IsPaid() As Boolean
        Get
            Return _IsPaid
        End Get
        Set(ByVal value As Boolean)
            _IsPaid = value
        End Set
    End Property
#End Region
    Public Shared Function GetSeatings(PerfID As Integer) As Seatings
        Dim Conn As New SqlConnection(DBL.Connection.GetConnectionString)

        ' Define the SQL
        Dim sqlString As String
        sqlString = "SELECT * FROM Seating WHERE PerfID = @PerfID"

        Dim Command As New SqlCommand(sqlString, Conn)
        Command.CommandType = CommandType.Text

        Dim Parm As New SqlParameter()
        Parm.ParameterName = "@PerfID"
        Parm.Value = PerfID
        Command.Parameters.Add(Parm)

        Dim LocalSeatings As New Seatings

        Try
            Conn.Open()

            Dim MyDataReader As SqlDataReader
            MyDataReader = Command.ExecuteReader

            MyDataReader.Read()

            LocalSeatings.ID = MyDataReader(FieldNames.ID)
            LocalSeatings.SeatNo = MyDataReader(FieldNames.SeatNo)
            LocalSeatings.PerfID = MyDataReader(FieldNames.PerfID)
            LocalSeatings.Patron = MyDataReader(FieldNames.Patron)
            LocalSeatings.IsPaid = MyDataReader(FieldNames.IsPaid)

            MyDataReader.Close()
            Conn.Close()
        Catch ex As Exception
            'TODO: Put some code here
            LocalSeatings = Nothing
        End Try

        Return LocalSeatings
    End Function

    Public Shared Function GetAllPatrons(PerfID As Integer) As List(Of Seatings)
        Dim ReturnList As New List(Of Seatings)

        Dim Conn As New SqlConnection(DBL.Connection.GetConnectionString)

        ' Define the SQL
        Dim sqlString As String
        sqlString = "SELECT * FROM Seatings WHERE PerfID = @PerfID"

        Dim Command As New SqlCommand(sqlString, Conn)
        Command.CommandType = CommandType.Text

        Dim Parm As New SqlParameter()
        Parm.ParameterName = "@PerfID"
        Parm.Value = PerfID
        Command.Parameters.Add(Parm)

        Dim LocalSeatings As New Seatings

        Try
            Conn.Open()

            Dim MyDataReader As SqlDataReader
            MyDataReader = Command.ExecuteReader

            Do While MyDataReader.Read()
                LocalSeatings = New Seatings
                LocalSeatings.ID = MyDataReader(FieldNames.ID)
                LocalSeatings.SeatNo = MyDataReader(FieldNames.SeatNo)
                LocalSeatings.PerfID = MyDataReader(FieldNames.PerfID)
                LocalSeatings.Patron = MyDataReader(FieldNames.Patron)
                LocalSeatings.IsPaid = MyDataReader(FieldNames.IsPaid)

                ReturnList.Add(LocalSeatings)
            Loop

            MyDataReader.Close()
            Conn.Close()
        Catch ex As Exception
            'TODO: Put some code here
            ReturnList = Nothing
        End Try

        Return ReturnList
    End Function
End Class


End Namespace

DATA SOURCE INFO

Theatre_Lab5DataSet.mdf

Theatre_Lab5DataSet.xsd

David Bond
  • 11
  • 2

0 Answers0