2013-04-08 214 views
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("[email protected]"); 
      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("[email protected]", "*****"); 
      smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
      smtp.Send(mail); 
     } 
    } 
    catch (Exception ex) 
    { 
     ex.ToString(); 
    } 
+3

什麼林你是否收到錯誤? – 2013-04-08 10:18:32

+0

你能把這個錯誤信息放在這裏嗎? – Habibillah 2013-04-08 10:18:45

+0

DateTime dt1 = Convert.ToDateTime(Request.QueryString [「period」]。ToString()); – 2013-04-08 10:19:04

回答

1

大概Request.QueryString["period"]爲空,因此在調用ToString()時,您會將對象引用設置爲對象異常的實例。

檢查調用ToString前的空引用:

var period = Request.QueryString["period"]; 
if (period != null) { 
    DateTime dt1 = Convert.ToDateTime(period.ToString()); 
} 
+0

那麼應該如何解決我如何填補空值? – 2013-04-08 10:24:43

+0

既然是查詢字符串,那麼這個值應該在你的url中:'/index.html?period = whatever'。 – 2013-04-08 10:25:19

1

檢查該參數是有你的查詢字符串。

TimeSpan ts; 
if(null != Request.QueryString["period"]) 
{ 
    DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString()); 
    DateTime dt2 = DateTime.Now; 
    TimeSpan ts = dt2 - dt1; 
} 
1

如果DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString())拋出錯誤,那麼比意味着你不必period在你的查詢字符串。更好地檢查

if(Request.QueryString["period"]!=null) 
{ 
    DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString()); 
. . 
. . 
} 
2

正如你所提到的錯誤是在該行

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

所以建議大家在

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

首先檢查是否爲空值有null值,則將其轉換爲日期時間

if(Request.QueryString["period"]!=null) 
{ 
    DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString()); 
} 
相關問題