Laalu Ar

Laalu Ar

  • NA
  • 20
  • 7.8k

System.NullReferenceException issue in Random();

Jun 28 2016 6:37 AM

Hi,

Am trying to generate random number and pass it from class a to class b with session

 Class A
public static void SendPwd(string Email)
{
using (var client = new SmtpClient())
{
using (var message = new MailMessage(EmailFrom, Email))
{
Random rnd = new Random();
var random = rnd.Next();
if (random != null)
{
System.Web.HttpContext.Current.Session["random"] = random;
}
message.Subject = "Please Verify Your Account";
message.Body = "<html><head><meta content=\"text/html; charset=utf-8\" /></head><body><p>Dear " + Email +
", </p><p>Please note the random Number and type in the Text box</p>" + "<p> Your Random Number is </p>" + random
+ "</a></p><div>Best regards,</div><div>Team</div><p>Do not forward "
+ "this email. The verify link is private.</p></body></html>";
message.IsBodyHtml = true;
client.EnableSsl = true;
client.Send(message);
};
};
}

 Class B

var password = con.tblEmployees.Where(a => a.Emp_Email == user.EmailId).FirstOrDefault();
password.Emp_TempPwd = (int)HttpContext.Current.Session["random"];
con.SaveChanges();
return "success";
 
 
while am doing this I got this error
 
An exception of type 'System.NullReferenceException' occurred in ServerControl.dll but was not handled in user code 
 
I have searched in Google to find it but I can’t find solution so kindly help me to solve this issue

thanks in advance


Answers (3)