我收到以下错误:
对象引用未设置为对象的实例。
'/ WebSite3'应用程序中的服务器错误。
对象引用未设置为对象的实例。
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
这是我的代码
Partial Class frmChoseIncident
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim objCDBGuest As New CDBGuestProfile
Dim objCGuest As New CGuestProfile
Dim arrList As New ArrayList
If Page.IsPostBack = False Then
Dim intGuestID1 As Integer
intGuestID1 = Request.QueryString("id")
objCGuest = objCDBGuest.getOneIncident(intGuestID1)
txtIncidentName.Text = objCGuest.IncidentName
txtIncidentSummary.Text = objCGuest.IncidentSummary
txtRoomRate.Text = objCGuest.RoomRate
txtArrivalDate.Text = objCGuest.ArrivalDate
txtDepartureDate.Text = objCGuest.DepartureDate
txtGuestName.text = objCGuest.GuestName
txtCompany.Text = objCGuest.GuestCompany
txtIncidentDate.Text = objCGuest.IncidentDate
txtAddedDate.Text = objCGuest.AddedDate
txtRoomID.Text = objCGuest.RoomId
txtPreparedBy.Text = objCGuest.PreparedBy
txtDepartment.Text = objCGuest.Department
txtGuestTime.Text = objCGuest.GuestTime
lstHotel.SelectedItem.Value = objCGuest.Hotel
txtAction.Text = objCGuest.Action
txtCost.Text = objCGuest.Cost
txtComplimentary.Text = objCGuest.Complimentary
txtAmount.Text = objCGuest.Amount
lstGlitchStatus.SelectedItem.Value = objCGuest.GlitchStatus
lstGuestHistory.SelectedItem.Value = objCGuest.GuestHistory
txtComments.Text = objCGuest.Comments
lstHotel.SelectedItem.Enabled = False
txtAction.ReadOnly = True
txtCost.ReadOnly = True
txtComplimentary.ReadOnly = True
txtAmount.ReadOnly = True
lstGlitchStatus.SelectedItem.Enabled = False
lstGuestHistory.SelectedItem.Enabled = False
txtComments.ReadOnly = True
txtIncidentName.ReadOnly = True
txtIncidentSummary.ReadOnly = True
txtRoomRate.ReadOnly = True
txtArrivalDate.ReadOnly = True
txtDepartureDate.ReadOnly = True
txtGuestName.ReadOnly = True
txtCompany.ReadOnly = True
txtIncidentDate.ReadOnly = True
txtRoomID.ReadOnly = True
txtPreparedBy.ReadOnly = True
txtDepartment.ReadOnly = True
txtGuestTime.ReadOnly = True
End If
End Sub
功能
Public Function getOneIncident(ByVal pintGuestID1 As Integer) As CGuestProfile
Dim objCmd As New MySqlCommand
Dim objCn As New MySqlConnection(connectionString)
Dim objAdapter As New MySqlDataAdapter
Dim strSQL As String = ""
Dim objDs As New DataSet
Dim objDataRow As DataRow
strSQL = "SELECT * FROM tblGuestProfile WHERE strGuestCodeTI=" & pintGuestID1
objCmd.CommandText = strSQL
objCmd.Connection = objCn
objAdapter.SelectCommand = objCmd
objCn.Open()
objAdapter.Fill(objDs, "tblGuestProfile")
objDataRow = objDs.Tables("tblGuestProfile").Rows(0)
Dim objCGuestProfile As New CGuestProfile
objCGuestProfile.GuestName = objDataRow.Item("strGuestNameTI")
objCGuestProfile.GuestCompany = objDataRow.Item("strGuestCompanyTI")
objCGuestProfile.ArrivalDate = objDataRow.Item("strArrivalDateTI")
objCGuestProfile.DepartureDate = objDataRow.Item("strDepartureDateTI")
objCGuestProfile.IncidentDate = objDataRow.Item("strIncidentDateTI")
objCGuestProfile.AddedDate = objDataRow.Item("strAddedDateTI")
objCGuestProfile.RoomRate = objDataRow.Item("strRoomRateTI")
objCGuestProfile.RoomId = objDataRow.Item("intRoomTI")
objCGuestProfile.PreparedBy = objDataRow.Item("strPreparedByTI")
objCGuestProfile.Department = objDataRow.Item("strDepartmentTI")
objCGuestProfile.GuestTime = objDataRow.Item("strTimeTI")
objCGuestProfile.IncidentName = objDataRow.Item("strIncidentNameTI")
objCGuestProfile.IncidentSummary = objDataRow.Item("strIncidentSummaryTI")
objCGuestProfile.Hotel = objDataRow.Item("intHotelTI")
objCGuestProfile.Action = objDataRow.Item("strActionTI")
objCGuestProfile.Cost = objDataRow.Item("strCostTI")
objCGuestProfile.Complimentary = objDataRow.Item("strComplimentaryTI")
objCGuestProfile.Amount = objDataRow.Item("strAmountTI")
objCGuestProfile.GlitchStatus = objDataRow.Item("intGlitchStatusTI")
objCGuestProfile.GuestHistory = objDataRow.Item("intGuestHistoryTI")
objCGuestProfile.Comments = objDataRow.Item("strCommentsTI")
objCn.Close()
Return objCGuestProfile
End Function
CGuestProfile
Imports Microsoft.VisualBasic
Public Class CGuestProfile
Private strGuestCodeTI As String
Private strGuestNameTI As String
Private strGuestCompanyTI As String
Private strArrivalDateTI As String
Private strDepartureDateTI As String
Private strIncidentDateTI As String
Private strAddedDateTI As String
Private intRoomTI As String
Private strRoomRateTI As String
Private strPreparedByTI As String
Private strDepartmentTI As String
Private strTimeTI As String
Private strIncidentNameTI As String
Private strIncidentSummaryTI As String
Private intHotelTI As Integer
Private intGuestHistoryTI As Integer
Private strComplimentaryTI As String
Private intGlitchStatusTI As Integer
Private strActionTI As String
Private strCostTI As String
Private strAmountTI As String
Private strCommentsTI As String
Public Property GuestId() As String
Get
Return strGuestCodeTI
End Get
Set(ByVal value As String)
strGuestCodeTI = value
End Set
End Property
Public Property GuestName() As String
Get
Return strGuestNameTI
End Get
Set(ByVal value As String)
strGuestNameTI = value
End Set
End Property
Public Property GuestCompany() As String
Get
Return strGuestCompanyTI
End Get
Set(ByVal value As String)
strGuestCompanyTI = value
End Set
End Property
Public Property ArrivalDate() As String
Get
Return strArrivalDateTI
End Get
Set(ByVal value As String)
strArrivalDateTI = value
End Set
End Property
Public Property DepartureDate() As String
Get
Return strDepartureDateTI
End Get
Set(ByVal value As String)
strDepartureDateTI = value
End Set
End Property
Public Property IncidentDate() As String
Get
Return strIncidentDateTI
End Get
Set(ByVal value As String)
strIncidentDateTI = value
End Set
End Property
Public Property AddedDate() As String
Get
Return strAddedDateTI
End Get
Set(ByVal value As String)
strAddedDateTI = value
End Set
End Property
Public Property RoomId() As Integer
Get
Return intRoomTI
End Get
Set(ByVal value As Integer)
intRoomTI = value
End Set
End Property
Public Property RoomRate() As String
Get
Return strRoomRateTI
End Get
Set(ByVal value As String)
strRoomRateTI = value
End Set
End Property
Public Property PreparedBy() As String
Get
Return strPreparedByTI
End Get
Set(ByVal value As String)
strPreparedByTI = value
End Set
End Property
Public Property Department() As String
Get
Return strDepartmentTI
End Get
Set(ByVal value As String)
strDepartmentTI = value
End Set
End Property
Public Property GuestTime() As String
Get
Return strTimeTI
End Get
Set(ByVal value As String)
strTimeTI = value
End Set
End Property
Public Property IncidentName() As String
Get
Return strIncidentNameTI
End Get
Set(ByVal value As String)
strIncidentNameTI = value
End Set
End Property
Public Property IncidentSummary() As String
Get
Return strIncidentSummaryTI
End Get
Set(ByVal value As String)
strIncidentSummaryTI = value
End Set
End Property
Public Property Hotel() As Integer
Get
Return intHotelTI
End Get
Set(ByVal value As Integer)
intHotelTI = value
End Set
End Property
Public Property Action() As String
Get
Return strActionTI
End Get
Set(ByVal value As String)
strActionTI = value
End Set
End Property
Public Property Cost() As String
Get
Return strCostTI
End Get
Set(ByVal value As String)
strCostTI = value
End Set
End Property
Public Property Complimentary() As String
Get
Return strComplimentaryTI
End Get
Set(ByVal value As String)
strComplimentaryTI = value
End Set
End Property
Public Property Amount() As String
Get
Return strAmountTI
End Get
Set(ByVal value As String)
strAmountTI = value
End Set
End Property
Public Property GlitchStatus() As Integer
Get
Return intGlitchStatusTI
End Get
Set(ByVal value As Integer)
intGlitchStatusTI = value
End Set
End Property
Public Property GuestHistory() As Integer
Get
Return intGuestHistoryTI
End Get
Set(ByVal value As Integer)
intGuestHistoryTI = value
End Set
End Property
Public Property Comments() As String
Get
Return strCommentsTI
End Get
Set(ByVal value As String)
strCommentsTI = value
End Set
End Property
End Class
如果我带走了
lstHotel.SelectedItem.Value = objCGuest.Hotel
lstGlitchStatus.SelectedItem.Value = objCGuest.GlitchStatus
lstGuestHistory.SelectedItem.Value = objCGuest.GuestHistory
lstHotel.SelectedItem.Enabled = False
lstGlitchStatus.SelectedItem.Enabled = False
lstGuestHistory.SelectedItem.Enabled = False
然后我可以加载页面,但是下拉列表是空的。
酒店功能
Public Function getAllHotelDropDownList() As ArrayList
Dim objCn As New MySqlConnection(connectionString)
Dim objCmd As New MySqlCommand
Dim objAd As New MySqlDataAdapter
Dim objDs As New DataSet
Dim objDr As DataRow
Dim strSQL As String = "SELECT * FROM tblHotel"
Dim objArrayList As New ArrayList
Dim objHotel As New CHotel
objCmd.Connection = objCn
objAd.SelectCommand = objCmd
objCmd.CommandText = strSQL
objCn.Open()
objAd.Fill(objDs, "tblHotelData")
For Each objDr In objDs.Tables("tblHotelData").Rows
objHotel = New CHotel
objHotel.Hotel = objDr.Item("intHotelHO")
objHotel.strHotel = objDr.Item("strHotelHO")
objArrayList.Add(objHotel)
Next
Return objArrayList
End Function
Public Function getOneHotel(ByVal pstrHotelTI As Integer) As CHotel
Dim objCmd As New MySqlCommand
Dim objCn As New MySqlConnection(connectionString)
Dim objAdapter As New MySqlDataAdapter
Dim strSQL As String = ""
Dim objDs As New DataSet
Dim objDataRow As DataRow
strSQL = "SELECT * FROM tblHotel WHERE intHotelHO=" & pstrHotelTI
objCmd.CommandText = strSQL
objCmd.Connection = objCn
objAdapter.SelectCommand = objCmd
objCn.Open()
objAdapter.Fill(objDs, "tblHotel")
objDataRow = objDs.Tables("tblHotel").Rows(0)
Dim objCHotel As New CHotel
objCHotel.strHotel = objDataRow.Item("strHotelHO")
objCn.Close()
Return objCHotel
End Function
End Class
答案 0 :(得分:1)
我不是100%确定您要使用ddlist做什么,但如果我理解它,您希望将列表的值更新为当前信息。
如果是这种情况,那么你不应该改变SelectedItem,你应该设置SelectedValue:
lstHotel.SelectedValue = objCGuest.Hotel
lstGlitchStatus.SelectedValue = objCGuest.GlitchStatus
lstGuestHistory.SelectedValue = objCGuest.GuestHistory
同样,如果您尝试禁用列表而不是列表中的项目,则需要:
lstHotel.Enabled = False
lstGlitchStatus.Enabled = False
lstGuestHistory.Enabled = False
您可能希望查看SelectedItem及其数据类型ListItem的MSDN文档。
如果事实证明您确实希望直接使用SelectedItem,则必须先测试它是否存在,如果需要,请在列表中选择一个项目或添加一个项目。
If lstHotel.SelectedItem Is Nothing Then
' Nothing in the list, so add it
If lstHotel.Items.Count = 0 Then
lstHotel.Items.Add(New ListItem)
End If
' Select the first item in the list
lstHotel.SelectedIndex = 0
End If
lstHotel.SelectedItem.Value = objCGuest.Hotel
lstHotel.SelectedItem.Enabled = False