𝔳𝔞𝔪𝔭𝔲𝔯𝔯
𝔳𝔞𝔪𝔭𝔲𝔯𝔯
CC#
Created by 𝔳𝔞𝔪𝔭𝔲𝔯𝔯 on 7/28/2023 in #help
❔ Error on my try and catch
i forget about it @>@
[HttpPost]
public IActionResult AppUserLogin(AppUser user)
{
string? connectionString = _configuration.GetConnectionString(ConnectionStringKey);
MySqlConnection connection = new MySqlConnection(connectionString);
try
{
connection.Open();
string sqlLogin = "SELECT Email,Password FROM AppUser WHERE Email= @Email AND password @Password ";
MySqlCommand command = new MySqlCommand(sqlLogin, connection);
command.Parameters.AddWithValue("@Email", user.Email);
command.Parameters.AddWithValue("@Password", user.Password);
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
if (ModelState.IsValid)
{
user.Id = reader["Id"].ToString();
//user.Email = usr.Email;
TempData["Msg"] = "Successfully login!";
}
}
}
else
{
TempData["msg"] = "Unexpected Database Error";
}
connection.Close();
}
catch (Exception ex)
{
TempData["Msg"] = "Incorrect email or password" + ex.Message;
}
//if (ModelState.IsValid)
//{

//}
//else
//{
// TempData["msg"] = "Invalid information entered!";
//}


return RedirectToAction("Main");

}
[HttpPost]
public IActionResult AppUserLogin(AppUser user)
{
string? connectionString = _configuration.GetConnectionString(ConnectionStringKey);
MySqlConnection connection = new MySqlConnection(connectionString);
try
{
connection.Open();
string sqlLogin = "SELECT Email,Password FROM AppUser WHERE Email= @Email AND password @Password ";
MySqlCommand command = new MySqlCommand(sqlLogin, connection);
command.Parameters.AddWithValue("@Email", user.Email);
command.Parameters.AddWithValue("@Password", user.Password);
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
if (ModelState.IsValid)
{
user.Id = reader["Id"].ToString();
//user.Email = usr.Email;
TempData["Msg"] = "Successfully login!";
}
}
}
else
{
TempData["msg"] = "Unexpected Database Error";
}
connection.Close();
}
catch (Exception ex)
{
TempData["Msg"] = "Incorrect email or password" + ex.Message;
}
//if (ModelState.IsValid)
//{

//}
//else
//{
// TempData["msg"] = "Invalid information entered!";
//}


return RedirectToAction("Main");

}
but the reader["Id"].ToString() and command.ExecuteReader() having red underline
33 replies
CC#
Created by 𝔳𝔞𝔪𝔭𝔲𝔯𝔯 on 7/28/2023 in #help
❔ Error on my try and catch
33 replies
CC#
Created by 𝔳𝔞𝔪𝔭𝔲𝔯𝔯 on 7/28/2023 in #help
❔ Error on my try and catch
33 replies
CC#
Created by 𝔳𝔞𝔪𝔭𝔲𝔯𝔯 on 7/28/2023 in #help
❔ Error on my try and catch
like this? but there is an error at varchar. checked my database datatype is varchar(50) but before i change anything. my code doesnt work. how can i troubleshoot?
[HttpPost]
public IActionResult AppUserLogin(AppUser Emails, AppUser Passwords)
{
string? connectionString = _configuration.GetConnectionString(ConnectionStringKey);
MySqlConnection connection = new MySqlConnection(connectionString);
try
{
connection.Open();
//string sql = "SELECT Email,Password FROM AppUser WHERE email= '" + Emails + "' AND password = '" + Passwords + "'";
string sql = "SELECT Email,Password FROM AppUser WHERE email = @Emails AND password = @Passwords";
MySqlCommand command = new MySqlCommand(sql, connection);
command.Parameters.Add("@Emails",SqlDbType.VarChar());
command.Parameters["@Emails"].Value = Emails;
command.Parameters.Add("@Passwords", SqlDbType.VarChar());
command.Parameters["@Passwords"].Value = Passwords;
command.ExecuteNonQuery();
TempData["Msg"] = "Successfully login!";
connection.Close();
}
catch (Exception ex)
{
TempData["Msg"] = "Incorrect email or password" + ex.Message;
}
return View("AppUserLogin");
[HttpPost]
public IActionResult AppUserLogin(AppUser Emails, AppUser Passwords)
{
string? connectionString = _configuration.GetConnectionString(ConnectionStringKey);
MySqlConnection connection = new MySqlConnection(connectionString);
try
{
connection.Open();
//string sql = "SELECT Email,Password FROM AppUser WHERE email= '" + Emails + "' AND password = '" + Passwords + "'";
string sql = "SELECT Email,Password FROM AppUser WHERE email = @Emails AND password = @Passwords";
MySqlCommand command = new MySqlCommand(sql, connection);
command.Parameters.Add("@Emails",SqlDbType.VarChar());
command.Parameters["@Emails"].Value = Emails;
command.Parameters.Add("@Passwords", SqlDbType.VarChar());
command.Parameters["@Passwords"].Value = Passwords;
command.ExecuteNonQuery();
TempData["Msg"] = "Successfully login!";
connection.Close();
}
catch (Exception ex)
{
TempData["Msg"] = "Incorrect email or password" + ex.Message;
}
return View("AppUserLogin");
33 replies
CC#
Created by 𝔳𝔞𝔪𝔭𝔲𝔯𝔯 on 7/28/2023 in #help
❔ Error on my try and catch
Using the way you say I should code it using this way
string query = "SELECT * FROM users WHERE username =' " @ username +" ';
string query = "SELECT * FROM users WHERE username =' " @ username +" ';
The @ username is from the html asp-for I think
33 replies
CC#
Created by 𝔳𝔞𝔪𝔭𝔲𝔯𝔯 on 7/28/2023 in #help
❔ Error on my try and catch
i will work on my code to prevent sql injection thank you so much ^^
33 replies
CC#
Created by 𝔳𝔞𝔪𝔭𝔲𝔯𝔯 on 7/28/2023 in #help
❔ Error on my try and catch
thanks it works now the applications is able to run blobthumbsup
33 replies
CC#
Created by 𝔳𝔞𝔪𝔭𝔲𝔯𝔯 on 7/28/2023 in #help
❔ Error on my try and catch
i remember my teacher covered that but i always seems to forget
33 replies
CC#
Created by 𝔳𝔞𝔪𝔭𝔲𝔯𝔯 on 7/28/2023 in #help
❔ Error on my try and catch
rip the youtube i follow is bad @>@
33 replies