I am trying to get a value from one function so that I can use that value in another function. I am trying to send out a mail with a code and I want to get the email from my database so I wrote a function to get the email but now I want to use that email in another function.
public static bool RegisterNewUser(string username, string email, string password)
{
if (CheckReg(email) == false)
{
//establish conection
using var con = new MySqlConnection(serverConfiguration);
con.Open();
//sql query
string sql = "INSERT INTO \`users\`(\`Username\`, \`Password\`, \`Email\`, \`Caps\`) VALUES (@username,@password,@email,1000)";
Console.WriteLine(sql);
using var cmd = new MySqlCommand(sql, con);
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@password", password);
cmd.Prepare();
cmd.ExecuteNonQuery();
SendEmail();
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("My Email");
message.To.Add(new MailAddress(email));
message.Subject = "Fallout-Tec authentication code";
message.IsBodyHtml = true; //to make message body as html
message.Body = "Hello there";
smtp.Port = 587;
smtp.Host = "smtp.gmail.com"; //for gmail host
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("My Email", "My Password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
return true;
}
else
{
Console.WriteLine("Email Taken");
return false;
}
}
This is the function I wrote to send the mail when a new user registers.
public static void SendEmail()
{
using var con = new MySqlConnection(serverConfiguration);
con.Open();
string sql = "SELECT SENDemail FROM admin";
using var cmd = new MySqlCommand(sql, con);
using MySqlDataReader reader = cmd.ExecuteReader();
var databaseemail = "";
while (reader.Read())
{
databaseemail = reader.GetString(0);
}
con.Close();
Console.WriteLine("Database verify code --------------" databaseemail);
}
Here is the code where I get my email from the database
CodePudding user response:
You can do something like, without testing it:
private static string value;
public static void Method1(){
value = "set value";
}
public static void AnotherMethod2(){
string getValue = value;
}
This requires that Method1 is called first then Method2 can consume it. You can also put an if statement to check if the value is set or not so it becomes a conditional step.
Another way is to let your method returns the value, so you can do like above but by returning the value. In this case, the value we returning is string type:
public static string Method1()
{
return "set value";
}
public static void AnotherMethod2()
{
string getValue = Method1();
}
The reason I added this is so you have both options, I prefer using return.