Object reference not set to an instance of an object. Dropdownlist
-
I'm getting the following error: 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. Here is my 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 The Function 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 The 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 If i take away the 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 Then i can load the page, however the dropdownlist is empty. Hotel function 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
-
Answer:
I am not 100% sure what you are trying to do with the ddlist, but if I understand it, you want to update the value of the list to be the current information. If this is the case, then you should not be altering the SelectedItem, you should be setting the SelectedValue: lstHotel.SelectedValue = objCGuest.Hotel lstGlitchStatus.SelectedValue = objCGuest.GlitchStatus lstGuestHistory.SelectedValue = objCGuest.GuestHistory Likewise, if you are trying to disable the list rather than the item in the list, then you need: lstHotel.Enabled = False lstGlitchStatus.Enabled = False lstGuestHistory.Enabled = False You might want to look through the MSDN documentation for http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selecteditem.aspx and its data type http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitem.aspx. If it turns out that you do want to work directly with a SelectedItem, you will have to test it for existence first and, if needed, select an item in the list or add one. 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
waterbottle at Stack Overflow Visit the source
Related Q & A:
- How to pass the SelectedValue in html.Dropdownlist MVC?Best solution by codeproject.com
- How to solve "Object reference not set to an instance of an object?Best solution by Stack Overflow
- How to close instance of an XML DOCUMENT?Best solution by Stack Overflow
- What is difference between instance class and static class in c#?Best solution by stackoverflow.com
- How to refresh dropdownlist in C#?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.