Saturday, March 24, 2012

Please Help - Where is the mistake in my code

Hi all

When I run this app, select the checkbox and press the calculate button i get the following error:

Server Error in '/' 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.

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

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.] nihe.SimpleRent.calculate(Object source, EventArgs e) +173 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +83 System.Web.UI.WebControls.Button.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) +33 System.Web.UI.Page.ProcessRequestMain() +1292

My code is as follows, does anyone know what i have done wrong, any help/advice would be very much appreciated.

<%@dotnet.itags.org. Page Language="VB" Debug="true"Inherits="nihe.SimpleRent" ContentType="text/html" ResponseEncoding="iso-8859-1" %><html><body> <form runat="server"> Bathroom with W/C: <asp:CheckBox id="bath_wc" runat="server"></asp:CheckBox> <p> <asp:button id="calcbutton" onclick="calculate" runat="server" text="calculate"></asp:button> </p> </form></body></html>
' SimpleRent.vb'Imports SystemImports System.WebImports System.Web.UIImports System.Web.UI.WebControlsNamespace nihePublic Class SimpleRent : inherits page protected withevents bath_wcAs system.web.ui.webcontrols.checkbox protected withevents row_bwcAs system.web.ui.webcontrols.TableRowPublic sub calculate(source asObject, e as EventArgs) if bath_wc.checked then bath_wc.text ="+3" Context.Items.Add("bath_wc", bath_wc.text) row_bwc.Visible = bath_wc.checked end ifserver.transfer("SimpleResult.aspx")End SubEnd ClassEnd Namespace
<%@dotnet.itags.org. Page Language="vb" %><script runat="server">Sub Page_Load(Source asObject, E as EventArgs) cell_bwc.text ="Bathroom With WC" value_bwc.text = Context.Items("bath_wc")End Sub</script><html><body><form> <asp:Table runat="server"> <asp:TableRow id="row_bwc"> <asp:TableCell id="cell_bwc"></asp:TableCell> <asp:TableCell id ="value_bwc"></asp:TableCell> </asp:TableRow> </asp:Table></form></body></html>

Where did you get this code from?

<html>
<body>
<form>
<asp:Table runat="server">
<asp:TableRow id="row_bwc">
<asp:TableCell id="cell_bwc"></asp:TableCell>
<asp:TableCell id ="value_bwc"></asp:TableCell>
</asp:TableRow>
</asp:Table>
</form>
</body>
</html>

I see 2 html parts in the code you posted.

Thanks


Because your Calculate method is encapsulated in a page that does not exist. Therefore, "row_bwc" does not exist.

For some reason, it seems you have attempted to create an inheritable base page that is actually a "real" page?


Sorry should have been more specific.

The code show is actaully three seperate pages

page one, codebehind for page one and results page.

Hope this clarifies things

Sorry

Kevin


The last line of your Calculate method attempts to executerow_bwc.Visible = bath_wc.checked. No instance of row_bc has been created. Therefore,row_bc is a null reference.

Remember, there's no row_bc on page one. It's only on the results page.


Lee thanks for reply

So where do i need to reference row_bwc and how do it do it?

Thanks in advance

Kevin


It seems you mix 2 web pages.You have 2 webpages A and B,you can only access controls of A in codebehind of A.

"So where do i need to reference row_bwc and how do it do it?"

You have 2 choice .

1: Combine page one and result page to a single page.That means add:

 <asp:Table runat="server"> <asp:TableRow id="row_bwc"> <asp:TableCell id="cell_bwc"></asp:TableCell> <asp:TableCell id ="value_bwc"></asp:TableCell> </asp:TableRow> </asp:Table>

to page one and don't forget add code in pageload to page one. Then use YourControl.visible to display or hide controls.

2: pass parameter (like row_bwc.Visible,etc )to SimpleResult.aspx then do the action within SimpleResult.aspx.


Kipster:

Lee thanks for reply

So where do i need to reference row_bwc and how do it do it?

Thanks in advance

Kevin

You need to pass the value to set the visibility of row_bwc to the SimpleResult page. You could do this in one of the many ways that you would normally pass values between pages -- like in a QueryString, or drop the Server.Transfer and do a crosspost instead. Let me know if you need an example.

0 comments:

Post a Comment