Showing posts with label click. Show all posts
Showing posts with label click. Show all posts

Thursday, March 29, 2012

Object reference error

Private Sub cmdLeftRight_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles cmdLeftRight.Click, cmdCenterRight.Click, cmdCenterLeft.Click, cmdRightLeft.Click

Dim sourcePane As String = CType(sender, ImageButton).Attributes("sourcepane")
Dim targetPane As String = CType(sender, ImageButton).Attributes("targetpane")
Dim sourceBox As ListBox = CType(Page.FindControl(sourcePane), ListBox)
Dim targetBox As ListBox = CType(Page.FindControl(targetPane), ListBox)

end sub

why i can not find my control with the "Page.FindControl" method?
the string name is correcte, the control exist on my page. I still have that error "object reference not set to an instance of..object"

what do i do wrong here !!

thanxYou haven't provided enough information for us to answer your question. Whether Page.FindControl will actually find a control depends on where the control is on your page.

To see what I mean - and hopefully to allow you to solve your own problem - please read:
In Search Of Controls

As an aside, you should always test whether an object has a value before you start working with it.

For example, you should do something like:

' Find the target pane
Dim FoundTargetPane As Control
FoundTargetPane = Page.FindControl( targetPane )

' If found, cast the target pane to a ListBox control
Dim targetBox As Listbox
If Not FoundTargetPane Is Nothing Then
targetBox = CType( FoundTargetPane, ListBox )
End If

' Work with the targetBox
If Not targetBox Is Nothing Then
' we can work with targetBox, knowing that's we've found it
End If

Friday, March 16, 2012

Object reference not set to an instance of an object.

When I click button the following error occurs. What does this mean and how to remove it. Please help.

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.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 25: MsgBox("Hi there")
Line 26: 'Put user code to initialize the page here
Line 27: End Sub
Line 28:
Line 29: Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ServerClick

Source File: c:\inetpub\wwwroot\WebForm1.aspx.vb Line: 27

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
localhost.WebForm1.Button1_ServerClick(Object sender, EventArgs e) in c:\inetpub\wwwroot\WebForm1.aspx.vb:27
System.Web.UI.HtmlControls.HtmlInputButton.OnServerClick(EventArgs e) +108
System.Web.UI.HtmlControls.HtmlInputButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1263Can you post lines 1 to 30 of WebForm1.aspx.vb? Because your error message suggests that you've mistakenly deleted some lines of code. Before you post the code, pleaseread this.

Even beyond that, there seems to be a number of problems with your code. I don't think that you can use the MsgBox function in ASP.NET. And having WebForm1.aspx.vb directly within your wwwroot folder seems problematic. Are you working through a book that introduces ASP.NET, or gone through theonline tutorials? If not, you should, aseveryone new to ASP.NET needs to go through the basics.
There is something in your code behind file that is told to do something. The problem is, it is not on your aspx page to do it. It has something to do with that MsgBox. I am not sure of the entire procedure though.
See, Since this is the direct VB code there is no question of "<>" code blocks coming into picture. Since I have been working on VB6 for last 2 years so I find VB code very easy. When I was going through the Help File of ASP.net on creating web page I came across this code. It says that when You double click on the control the "...aspx.vb" file opens and You can write your code for button click or any such events there.
When I run the Page the code behind (in the aspx.vb module) doesn't run but when I put them in html it runs.
So my problem is how to run the code from aspx.vb module of Visualstudio.net package which seems so easy to code and understand for me.
Sounds like you are not telling the aspx page where the codebehind source is located. Top line should read something like this:
<%@. Page language="vb" CodeBehind="DesktopDefault.aspx.vb" AutoEventWireup="false" Explicit="True"Inherits="DotNetNuke.DesktopDefault" %>
which is thenamespace.classname