2012-01-03 94 views
0

,我发现了以下错误:未将对象引用设置为对象的实例。 DROPDOWNLIST

Object reference not set to an instance of an object.

Server Error in '/WebSite3' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

这里是我的代码

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

请找到错误的行,然后张贴只有code..Nobody的相关部分会觉得就像读这个大代码.. – dotNETbeginner 2012-01-03 06:56:34

回答

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 

您可能希望通过MSDN文档,以寻找SelectedItemListItem其数据类型。

如果事实证明您确实想直接使用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 
+0

嗨competent_tech。 我试过你的方法与SelectedValue,它最后加载,但现在下拉列表是空的,如 ![Valid XHTML](http://tinypic.com/r/za7g5/5) – waterbottle 2012-01-03 04:36:10

+0

好吧,这是应该从另一个位置加载,还是应该从访客记录中的数据填充?如果它应该来自客人记录,则使用答案中的第二组代码。 – 2012-01-03 04:45:14

+0

在我的数据库中,我有4个客人记录,一个是旅馆,一个是古怪的,一个是客人。 在客人记录inthotelTI是1或2.至于表酒店其intHotelHO 1和strHotelHO FHS 功能获取酒店 我会发布上面 – waterbottle 2012-01-03 06:28:44

相关问题