This is a common error that .Net developers often face at some time or another while developing their applications. This is to be used as a guide for diagnosing the common causes of this error.
Prior to .Net 2.0, you had to declare every control in your code behind. For example:
.aspx
.aspx.vbCode:<asp:TextBox ID="FirstName" runat="server" />
Without the bolded line above, you would receive the error mentioned in the title of this thread. This is ONLY in versions of .Net prior to 2.0.Code:Public Class MyPage Inherits System.Web.UI.Page Protected WithEvents FirstName As System.Web.UI.WebControls.TextBox
Variable scoping is another common issue. For example, you can't declare a variable in one sub and then expect it to work in another:
The above will result in the error because the variable "myVariable" is out of scope in the Button1_Click sub.Code:Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Dim myVariable As String = "Test" End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Response.Write(myVariable) End Sub
Bad inits and constructs are another cause. Take the following example:
Above, we have defined dirInfo as a DirectoryInfo object but haven't actually created it:Code:Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Dim dirInfo As System.IO.DirectoryInfo dirInfo.GetDirectory("C:\") End Sub
Another possible cause of this error is if you do not give a user control an ID:Code:Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Dim dirInfo As System.IO.DirectoryInfo dirInfo = New System.IO.DirectoryInfo("C:\") End Sub
The correct way:Code:<YourPrefix:YourControl runat="server" />
If anyone has any other causes, feel free to add them here.Code:<YourPrefix:YourControl ID="myControlID" runat="server" />



LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks