I want to find the time at which Data is entered into my database. I'm using SQL Server as my database.
public class OtpchangeController : Controller
{
DateTime dbTime;
// GET: Otpchange
public ActionResult Generateotp()
{
return View();
}
[HttpPost]
public ActionResult Generateotp(tblPassword obj)
{
Random r = new Random();
using (PasswordGenerationEntities db = new PasswordGenerationEntities())
{
tblPassword newObj = (from c in db.tblPasswords
where c.Username == obj.Username
select c).First();
if(newObj != null)
{
int num = r.Next();
string newOtp = num.ToString();
newObj.Otp = newOtp;
db.SaveChanges();
dbTime = DateTime.Now;
Session["Username"] = newObj.Username.ToString();
return RedirectToAction("ChangePassword");
}
}
return View();
}
public ActionResult ChangePassword()
{
return View();
}
[HttpPost]
public ActionResult ChangePassword(tblPassword obj)
{
string name = @Session["Username"].ToString();
using (PasswordGenerationEntities db = new PasswordGenerationEntities())
{
tblPassword newObj = (from c in db.tblPasswords
where c.Otp == obj.Otp && c.Username == name
select c).First();
DateTime formTime = DateTime.Now;
TimeSpan result = dbTime.Subtract(formTime);
newObj.Password = obj.Password;
db.SaveChanges();
return RedirectToAction("Success");
}
//return View();
}
public ActionResult Success()
{
return View();
}
Here I want to find difference between the time when otp is added into database and the time when that otp is entered by the user.
But dbTime comes as 01/01/2001
But formTime
is correct, also I want to find the difference between those time.
It seems the code you provided is not compiling or some parts are missing.
Anyway said I would suggest checking the msdn documentation about dbContext and its features here
You can delegate the database log to a console log or another more suitable provider(log4net or a simple custom log file). Those logs contain information about database execution times.
In your case you could do :
using (PasswordGenerationEntities db = new PasswordGenerationEntities())
{
db.Database.Log = Console.Write;
tblPassword newObj = (from c in db.tblPasswords
where c.Username == obj.Username
select c).First();
if(newObj != null)
{
int num = r.Next();
string newOtp = num.ToString();
newObj.Otp = newOtp;
db.SaveChanges();
dbTime = DateTime.Now;
Session["Username"] = newObj.Username.ToString();
return RedirectToAction("ChangePassword");
}
}
Check the console ouput.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.