对于我和我的合作伙伴实验室,我们使用SQL语句设置了一个.mdf数据源,其中包含表格和数据。我们有一个包含各种标签的表单,当我们的组合框中的索引发生更改时,它将使用数据库中的信息更新标签。当索引发生更改时,会出现问题,当尝试分配来自.mdf文件的数据时,座位标签会抛出Null Reference Exception。如果我的解释不清楚,我很抱歉,我对VB相对较新,这是我在StackOverflow上的第一篇文章。我已经提供了下面的代码,只是好奇我们哪里出错了(连接?SQL?参考?等等)非常感谢你的时间,它将于今晚11点59分到期。
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
数据来源信息