1

我目前正在开发一个应用程序,该应用程序将使用户能够从我们在他的电子邮件地址中提供的链接下载演示软件。24 小时后,现在我已经通过他的电子邮件地址中显示的链接完成了,他可以下载但在禁用此链接的编码中.. 我收到错误我的代码是..

protected void btn_sub_Click(object sender, EventArgs e)
{        
    cn.Open();
    objInquiry.Name = txt_name.Text.ToString().Trim();
    objInquiry.MobileNo = txtMobileNo.Text.ToString().Trim();
    objInquiry.EmailId = txt_eid.Text.ToString().Trim();
    objInquiry.InquiryFor = "Agriculture Product Marketing comity System".ToString().Trim();
    objInquiry.Message = txt_msg.Text.ToString().Trim();

    using (DataSet ds = objInquiry.InsertInquiry())
    {
        Msg.Visible = true;
        Msg.Text = "Thank U For Inquiry We Will Send Demo Link To Your Email Please Check Your Email Regularly";
    }

    try
    {
        DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString());
        DateTime dt2 = DateTime.Now;
        TimeSpan ts = dt2 - dt1;

        if (ts.TotalMinutes > 5)
        {
            Response.Write("Download time is over");
        }
        else
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("abc@gmail.com");
            mail.To.Add(txt_eid.Text);
            mail.Subject = txtInquiryFor.Text;
            mail.IsBodyHtml = true;
            mail.Body = "Welcome Mr." + txt_name.Text + "<br><br>";
            mail.Body += "To ShreeHans Webnology" + "<br><br>";
            mail.Body += "Thank u for putting inquiry for" + txtInquiryFor.Text + "<br><br>";
            mail.Body += "Please Click on Following Link To Download Your Demo" + "<br><br>";
            mail.Body += "<a href=\"http://www.test.co.in/ConatctUs.aspx?period=" + DateTime.Now + "'\">Download Demo Software</a>";

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "*****");
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Send(mail);
        }
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
4

4 回答 4

2

正如您所提到的,错误就在这条线上

DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString());

所以它建议你有空值

Request.QueryString["period"].ToString()

首先检查空值,然后将其转换为日期时间

if(Request.QueryString["period"]!=null)
{
    DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString());
}
于 2013-04-08T10:22:28.720 回答
1

可能Request.QueryString["period"]是 null 所以在调用ToString()它时,您会得到未将对象引用设置为对象异常的实例。

在调用之前检查空引用ToString

var period = Request.QueryString["period"];
if (period != null) {
    DateTime dt1 = Convert.ToDateTime(period.ToString());
}
于 2013-04-08T10:20:05.827 回答
1

检查该参数是否存在于您的 QueryString 中。

TimeSpan ts;
if(null != Request.QueryString["period"])
{
  DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString());
  DateTime dt2 = DateTime.Now;
  TimeSpan ts = dt2 - dt1;
}
于 2013-04-08T10:21:25.050 回答
1

如果DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString())抛出错误,则意味着您period的查询字符串中没有。最好检查一下

if(Request.QueryString["period"]!=null)
{
  DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString());
 . .
 . .
}
于 2013-04-08T10:22:02.013 回答