How pass a values from a web form to another in ASP.NET?

I am trying to send string data from one web form to another, however it throws the System.NullReferenceException error. I read in the Postgress documentation that this error means there is no data to work with. This is the code for the first form:

protected void Button2_Click(object sender, EventArgs e)
{
    editar ed = new editar(tname.Text.ToString());
    //ed.putId(tname.Text.ToString());
    String url = "editar.aspx";
    Response.Redirect(url);
}

Creating the object of the second form was one of the ways I found to send the data, but it keeps throwing the error. In the second form you should receive it.
public editar(string name)
{
    this.tname.Text = name;
}

The basic idea is that the string is received in the second form to put it in a text box and there, by means of a button, the information of a database can be updated.
protected void Button2_Click(object sender, EventArgs e)
{
    upt = new NpgsqlCommand("update usuario set cedula = '" + tcedula.Text.ToString() + "' where nombre = '" + tname.Text.ToString() + "'", conn);
    upt.ExecuteNonQuery();
}

I have already tried through the POST and Session method, the problem with these two methods is that although the data arrives correctly every time the page is reloaded the data is reloaded and that happens each time the button, therefore the SQL statement to update the information is null, that is, it executes it, but it does not update the database. I use Visual Studio 2019, ASP.NET, and Postgres.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There are two things to understand here:

  1. Web Forms still use HTTP requests for each page view and even for each server event like button clicks.
  2. Form class instances do not persist between requests. Your
    code runs for long enough to send some html or javascript to the
    browser, and then everything is immediately thrown away; nothing
    stays in memory. Even if the same user requests the same page again
    (ie, clicks refresh in their browser), you still get an entirely new
    instance of your page class that doesn’t know anything of what has come before.

Therefore, if you have a value you want to use across HTTP requests, as in this case when you have two different forms, you have to put it somewhere that will be available when the later http request is processed. You can do that several ways: the session, a GET (url) or POST parameter, or save to a database during the first request and receive on the second.

In this case, a session value seems most appropriate, but it’s hard to be sure without knowing more of what you’re doing.


And for God’s sake, fix that glaring SQL injection issue. You shouldn’t be writing database code for the web without knowing about that.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x