Difference between revisions of "Create register for ASP.Net Web App"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with " <syntaxhighlight lang=c#> public IActionResult OnPost() { User temp = new User(); string username = Request.Form["user"]; string pass1 = Request.Form["password...")
 
Line 3: Line 3:
 
public IActionResult OnPost()
 
public IActionResult OnPost()
 
{
 
{
    User temp = new User();
+
string username = Request.Form["user"];
    string username = Request.Form["user"];
+
string pass1 = Request.Form["password1"];
    string pass1 = Request.Form["password1"];
+
string pass2 = Request.Form["password2"];
    string pass2 = Request.Form["password2"];
+
int status = 0;
  
    temp.status = Convert.ToInt32(Request.Form["status"]);
+
if (pass1 == pass2)
 +
{
 +
using var connection = GetConnection;
  
    using var connection = GetConnection;
+
connection.Open();
 +
string sql = "insert into test values(@p1, @p2, @p3);";
 +
using var Command = new MySqlCommand(sql, connection);
 +
Command.Parameters.AddWithValue("@p1", username);
 +
Command.Parameters.AddWithValue("@p2", pass1);
 +
Command.Parameters.AddWithValue("@p3", status);
  
    connection.Open();
+
Command.ExecuteNonQuery();
    string sql = "insert into test values(@p1, @p2, @p3);";
+
connection.Close();
    using var Command = new MySqlCommand(sql, connection);
+
}
    Command.Parameters.AddWithValue("@p1", temp.username);
 
    Command.Parameters.AddWithValue("@p2", temp.password);
 
    Command.Parameters.AddWithValue("@p3", temp.status);
 
  
    Command.ExecuteNonQuery();
+
return Page();
    connection.Close();
 
 
    users.Add(temp);
 
 
 
    return Page();
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 14:29, 19 October 2024

public IActionResult OnPost()
{
	string username = Request.Form["user"];
	string pass1 = Request.Form["password1"];
	string pass2 = Request.Form["password2"];
	int status = 0;

	if (pass1 == pass2)
	{
		using var connection = GetConnection;

		connection.Open();
		string sql = "insert into test values(@p1, @p2, @p3);";
		using var Command = new MySqlCommand(sql, connection);
		Command.Parameters.AddWithValue("@p1", username);
		Command.Parameters.AddWithValue("@p2", pass1);
		Command.Parameters.AddWithValue("@p3", status);

		Command.ExecuteNonQuery();
		connection.Close();
	}

	return Page();
}