尝试从SQLite数据库中检索数据时出现错误“ 指定的转换无效”。
认为它与DeviceRecord.ip_address有关。ToString认为sqlite不喜欢它,有人可以帮忙吗?
我是编码和SQLite的新手,所以希望所有这一切都有意义,我的代码可能遍地都是,很抱歉...
来自浏览器的错误消息
[InvalidCastException: Specified cast is not valid.]
System.Data.SQLite.SQLiteDataReader.VerifyType(Int32 i, DbType typ) +327
System.Data.SQLite.SQLiteDataReader.GetBoolean(Int32 i) +263
System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.Initialize(DbDataReader reader, DbSpatialDataReader spatialDataReader, Type[] columnTypes, Boolean[] nullableColumns) +420
System.Data.Entity.Core.Objects.Internal.BufferedDataReader.Initialize(String providerManifestToken, DbProviderServices providerServices, Type[] columnTypes, Boolean[] nullableColumns) +156
System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context, ObjectParameterCollection parameterValues) +1014
System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +512
System.Data.Entity.Core.Objects.<>c__DisplayClassb.<GetResults>b__9() +203
System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +341
System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() +38
System.Lazy`1.CreateValue() +727
System.Lazy`1.LazyInitValue() +184
System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() +15
System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +202
_Default.getIP() +982
System.Web.UI.Control.OnLoad(EventArgs e) +103
System.Web.UI.Control.LoadRecursive() +68
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1381
C#代码
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Configuration;
using System.Windows;
using System.Net.Sockets;
public partial class _Default : System.Web.UI.Page
{
string serviceDesk = "email address";
string emailSubject = "MyPc";
string IPaddr = "";
string deviceName = "";
private readonly object reader;
public DateTime DateStamp { get; private set; }
protected void Page_Load(object sender, EventArgs e)
{
getIP();
}
protected void getIP()
{
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDER_FOR"] != null)
{
IPaddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDER_FOR"].ToString();
}
if (HttpContext.Current.Request.UserHostAddress.Length != 0)
{
IPaddr = HttpContext.Current.Request.UserHostAddress;
DateStamp = DateTime.Now;
try
{
deviceName = Dns.GetHostEntry(IPAddress.Parse(IPaddr)).HostName;
}
catch (SocketException)
{
deviceName = "could not be resolved";
}
}
Label1.Text = "IP Address: " + " " + IPaddr;
Label2.Text = "PC Name: " + " " + deviceName;
Label3.Text = "Date and Time: " + " " + DateStamp;
using (mainEntities dbcontent = new mainEntities())
{
var DeviceRecord = dbcontent.devices.SingleOrDefault(x => x.ip_address == IPaddr);
if(DeviceRecord !=null)
{
string ipaddress = DeviceRecord.ip_address.ToString();
string name = DeviceRecord.name.ToString();
Label4.Text = ipaddress;
Label5.Text = name;
}
}
}
private void EmailVerificationRequest(string recepientEmail, string subject)
{
try
{
using (MailMessage mailMessage = new MailMessage())
{
StringBuilder sbEmailBody = new StringBuilder();
sbEmailBody.Append("IP address " + IPaddr + "<br/>");
sbEmailBody.Append("Hostname " + " " + deviceName + "<br/>");
sbEmailBody.Append("Date and Time " + " " + DateStamp);
mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["FromEmail"]);
mailMessage.Subject = subject;
mailMessage.Body = sbEmailBody.ToString();
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(recepientEmail));
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["Host"];
smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
if (string.IsNullOrWhiteSpace(NetworkCred.UserName))
{
smtp.UseDefaultCredentials = true;
}
else
{
smtp.Credentials = NetworkCred;
smtp.UseDefaultCredentials = false;
}
smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
try
{
smtp.Send(mailMessage);
}
catch (SmtpException e)
{
}
}
}
catch
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
EmailVerificationRequest(serviceDesk, emailSubject);
Response.Redirect("~/message.aspx");
}
}