Reputation: 311
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.
Upvotes: 0
Views: 805
Reputation: 416049
There are two things to understand here:
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.
Upvotes: 1