2012-01-03 26 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

相關問題