sj1187534 Posted June 21, 2003 Posted June 21, 2003 Hi.....This part of the code does not work for some reason...It is always giving me the same error that: Object reference not set to an instance of an object this is the whole code for the user control which loads the drop down list for states... ========================================== Public MustInherit Class ddlState Inherits System.Web.UI.UserControl Protected WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection Protected WithEvents SQLCommand1 As System.Data.SqlClient.SqlCommand Protected WithEvents ddlStates As System.Web.UI.WebControls.DropDownList Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim connect As SetUpDBConnect, dset As New DataSet connect = New SetUpDBConnect("webGetState") dset = connect.Get_State() connect.Close() Dim count As Integer, noOfRows As Integer noOfRows = dset.Tables(0).Rows.Count For count = 0 To noOfRows - 1 ddlStates.Items.Add(dset.Tables(0).Rows(count).Item(1)) Next count End Sub Function GetState() As String Return ddlStates.SelectedItem.Value End Function Function SetListValue(ByVal curState As String) ddlStates.Items.FindByText(curState).Selected = True End Function End Class ======================================= It is giving me error on the 3 rd line from the bottom....I dont have a clue why it is doing so.....I really need some thought from someone else...I am out of ideas....Thanks.. SJ Quote
sj1187534 Posted June 21, 2003 Author Posted June 21, 2003 One more question related to this Does a user control on an .aspx page load when the main page is loaded? SJ Quote
sj1187534 Posted June 21, 2003 Author Posted June 21, 2003 I was able to fix that issue myself.... Actually, the problem was something else...I mean the error is still in that line....The fact is when a .aspx page has a user control in it, the main page loads first and then the user control is loaded...So, if we are trying to access the usercontrol within the Page_Load() of the main page...you are bound to get the NullPointerException...You cannot escape that...... I think the best way to handle this is to write the code (no matter what we are trying to do) in the Page_Init() method rather than the Page_Load() method of the user control....Thats how I was able to solve my problem..... For example...I am trying to load the dropdownlist in the user control when the user control is loaded...in other words, when the user control is initiated...... Hope this helps.... Thanks anyway. SJ Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.