Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, February 1, 2007 1:27 PM
Hi,
I'm having a "NullReferenceException was unhandled by the code" Error when I try and process the code below to pass the info from 4 text boxes from one page to another. Can anybody please help?
Sending Page code
rotected void Button1_Click(object sender, EventArgs e)
{
// Forward the user to the information page,
// with the query string data.
string url = "QueryStringReceive2.aspx?";
url += "FirstName=" + Textbox1.Text + "&";
url += "Surname=" + Textbox2.Text + "&";
url += "Mobile=" + Textbox3.Text + "&";
url += "Country=" + Textbox4.Text;
Response.Redirect(url);
}
Receiving page code:
protected void Page_Load(object sender, EventArgs e)
{
Textbox1.Text = Request.QueryString["FirstName"];
Textbox2.Text = Request.QueryString["Surname"];
Textbox3.Text = Request.QueryString["Mobile"];
Textbox4.Text = Request.QueryString["Country"];
}
Does anybody know how I can get round this error- thanks
All replies (15)
Friday, February 2, 2007 4:50 AM ✅Answered
Did you write anything in the textbox's TextChanged event? You have AutoPostBack set to true for the TextBox's. Here is a sample which is working
Sendin:
1 <body>
2 <form id="form1" runat="server">
3 <div>
4 <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true"></asp:TextBox>
5 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
6 </div>
7 </form>
8 </body>
1 public partial class Default4 : System.Web.UI.Page
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5
6 }
7 protected void Button1_Click(object sender, EventArgs e)
8 {
9 string _temp = TextBox1.Text;
10 Response.Redirect("Default.aspx?text=" + _temp);
11 }
12 }
Receiving:
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (Request.QueryString["text"] != null)
4 Response.Write(Request.QueryString["text"].ToString());
5 }
Thanks
Friday, February 2, 2007 7:55 AM ✅Answered
Everybody his problem isn't that post back is set because he is accounting for moving between the fields with JavaScript. Also if he sets the new keyword on the text boxes at the top of the page he is not going to get it to show in the page, because the page handles the creation of the controls in .NET 2.0/ASP.NET, that is why the partial keyword is there.
Honeslty just give up on this page and create a new one. Then on the new page don't touch any of the code at all in the C# file. Drage the 4 text boxes on to your page. Then go back to the code and just start writing. Type "this." to find the text boxes that you have just created.
I think you have really confused VS.NET and that is why you are having problems. And since you don't have much code here, just start over.
Thursday, February 1, 2007 2:21 PM
Do you know what line it is occuring on? Because all of these look valid, but it could be because you have not initialized one of the textboxes.
Thursday, February 1, 2007 2:27 PM
The error is coming from the first text box on debug?
But if I comment out the first line the same error occurs on the second line??
// url += "FirstName=" + Textbox1.Text + "&";
url += "Surname=" + Textbox2.Text + "&";
url += "Mobile=" + Textbox3.Text + "&";
url += "Country=" + Textbox4.Text;
Response.Redirect(url);
Thursday, February 1, 2007 2:36 PM
This probably means that none of your text boxes have been intialized. Can you post the whole file if possible, as long as it is not too long.
Thursday, February 1, 2007 2:49 PM
sending:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class QueryStringSend2 : System.Web.UI.Page
{
protected TextBox Textbox1;
protected TextBox Textbox2;
protected TextBox Textbox3;
protected TextBox Textbox4;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// Forward the user to the information page,
// with the query string data.
string url = "QueryStringReceive2.aspx?";
// url += "FirstName=" + Textbox1.Text + "&";
url += "Surname=" + Textbox2.Text + "&";
url += "Mobile=" + Textbox3.Text + "&";
url += "Country=" + Textbox4.Text;
Response.Redirect(url);
}
}
Receiving:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class QueryStringReceive2 : System.Web.UI.Page
{
protected TextBox Textbox1;
protected TextBox Textbox2;
protected TextBox Textbox3;
protected TextBox Textbox4;
protected void Page_Load(object sender, EventArgs e)
{
Textbox1.Text = Request.QueryString["FirstName"];
Textbox2.Text = Request.QueryString["Surname"];
Textbox3.Text = Request.QueryString["Mobile"];
Textbox4.Text = Request.QueryString["Country"];
}
}
many thanks!
Thursday, February 1, 2007 3:15 PM
Is this a web site? Or a web application? Are the four text boxes already declared on your ASPX page? If so you shouldn't need to declare them at the top of the class. Because the ASPX page is taking care of this for you.
Thursday, February 1, 2007 3:29 PM
If I comment out the declarations for the text boxes I get the error "The name 'Textbox1 does not exist in the current context"??
Thursday, February 1, 2007 3:31 PM
Are they created in your ASPX page? Because if they are not all that you have is TextBox objects that are null, thus your error.
Thursday, February 1, 2007 4:22 PM
They were created are are there in the aspx page, is it time to start again, I can't see whats wrong i'ii attach the aspx code. Sorry if its seems a mess!
/head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Style="z-index: 104; left: 215px; ;
top: 49px" AutoPostBack="True"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Style="z-index: 101; left: 215px; ;
top: 116px" Width="155px" AutoPostBack="True"></asp:TextBox>
<br />
First Name<br />
<br />
<br />
<br />
Surname<br />
<br />
<br />
mobile number<br />
<br />
<br />
Country
<asp:TextBox ID="TextBox3" runat="server" Style="z-index: 102; left: 215px; ;
top: 176px" Width="155px" AutoPostBack="True"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server" Style="z-index: 103; left: 215px; ;
top: 233px" Width="155px" AutoPostBack="True"></asp:TextBox>
<br />
<br />
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Transfer values via querystring"
Width="248px" />
</div>
</form>
</body>
</html>
recieve aspx:
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
This is first name<br />
<br />
<br />
this is surname<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
<br />
mobile<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<br />
<br />
country born<br />
<asp:TextBox ID="TextBox4" runat="server" OnTextChanged="TextBox4_TextChanged"></asp:TextBox>
</form>
</body>
</html>
Thursday, February 1, 2007 4:22 PM
They were created are are there in the aspx page, is it time to start again, I can't see whats wrong i'ii attach the aspx code. Sorry if its seems a mess!
/head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Style="z-index: 104; left: 215px; ;
top: 49px" AutoPostBack="True"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Style="z-index: 101; left: 215px; ;
top: 116px" Width="155px" AutoPostBack="True"></asp:TextBox>
<br />
First Name<br />
<br />
<br />
<br />
Surname<br />
<br />
<br />
mobile number<br />
<br />
<br />
Country
<asp:TextBox ID="TextBox3" runat="server" Style="z-index: 102; left: 215px; ;
top: 176px" Width="155px" AutoPostBack="True"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server" Style="z-index: 103; left: 215px; ;
top: 233px" Width="155px" AutoPostBack="True"></asp:TextBox>
<br />
<br />
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Transfer values via querystring"
Width="248px" />
</div>
</form>
</body>
</html>
recieve aspx:
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
This is first name<br />
<br />
<br />
this is surname<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
<br />
mobile<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<br />
<br />
country born<br />
<asp:TextBox ID="TextBox4" runat="server" OnTextChanged="TextBox4_TextChanged"></asp:TextBox>
</form>
</body>
</html>
Friday, February 2, 2007 5:10 AM
Hi, I didn,t have anything in the textbox change event, but autopistback was set to too!
thanks, I 'ii try your sample.
Friday, February 2, 2007 5:10 AM
Hi, I didn,t have anything in the textbox change event, but autopostback was set to too!
thanks, I 'ii try your sample.
Friday, February 2, 2007 6:39 AM
public partial class QueryStringSend2 : System.Web.UI.Page
{
protected TextBox Textbox1;
protected TextBox Textbox2;
protected TextBox Textbox3;
protected TextBox Textbox4;
protected void Page_Load(object sender, EventArgs e)
{
}
Hi Camper, i think that you have created these textboxes in the code..... so u need to use the new keyword like.
TextBox txt1=new TextBox
Hope that might helps
Monday, September 22, 2008 2:21 PM
Hi,
Is there any way to send the textbox itself in the querystring instead of just the value in it. If so please can any one send me an example!.