我有一个 if 语句来检查我的数据库中是否存在一个条目。该代码正确检查电子邮件地址是否已经存在,但是如果电子邮件不存在,我会收到错误消息:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding
on a null reference at CallSite.Target(Closure , CallSite , Object ) at
ASP._Page__footer_cshtml.Execute()
但是,如果我删除获取现有电子邮件的代码,这些行:
var getrecord = db.QuerySingle("Select Email from Registrations where email = @0", email);
emailexists = getrecord.email;
数据将毫无问题地保存回数据库。不确定是不是因为我使用的是相同的 Db 助手?
try
{
var db = Database.Open("MiniDB");
var getrecord = db.QuerySingle("Select Email from Registrations where email = @0", email);
emailexists = getrecord.email;
if (emailexists != ""){
msg = "This Email address is already registed";
saved = false;
}
else {
var insertCommand = "INSERT INTO Registrations (Email, Name, regDate) VALUES(@0, @1, @2)";
db.Execute(insertCommand, email, name, regDate);
saved = true;
}
}
catch (Exception ex)
{
msg = ex.ToString();
saved = false;
}