[英]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.我试图将字符串数据从一个 Web 表单发送到另一个,但是它抛出 System.NullReferenceException 错误。 I read in the Postgress documentation that this error means there is no data to work with.我在 Postgress 文档中读到这个错误意味着没有数据可以使用。 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.我已经尝试过 POST 和 Session 方法,这两种方法的问题是,尽管每次重新加载页面时数据都正确到达,但每次重新加载数据时都会发生这种情况,因此每次单击按钮时都会发生这种情况,因此需要更新信息的 SQL 语句为空,即执行它,但不更新数据库。 I use Visual Studio 2019, ASP.NET, and Postgres.我使用 Visual Studio 2019、ASP.NET 和 Postgres。
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.因此,如果您有一个值要在 HTTP 请求中使用,就像在这种情况下,当您有两种不同的表单时,您必须将它放在某个地方,以便在处理稍后的 http 请求时可用。 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.您可以通过多种方式做到这一点:会话、GET (url) 或 POST 参数,或者在第一个请求期间保存到数据库并在第二个请求中接收。
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.看在上帝的份上,修复那个明显的 SQL 注入问题。 You shouldn't be writing database code for the web without knowing about that.您不应该在不知道这一点的情况下为 Web 编写数据库代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.