1

I'm currently developing an application that will give user capability of downloading demo software from link provided by us in his email address. After 24 hours, now I have done through link shown in his email address, and he can download but in the coding of disable of this link .. I'm getting error my code is ..

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();
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
purvang pandya
  • 123
  • 1
  • 4
  • 10

4 Answers4

2

As you have mentioned that the error is in the line

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

So it suggest that you have null value in

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

First check for null value then convert it to date time

if(Request.QueryString["period"]!=null)
{
    DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString());
}
शेखर
  • 17,412
  • 13
  • 61
  • 117
1

Probably Request.QueryString["period"] is null so when calling ToString() on it, you would get the Object Reference not set to an instance of an object exception.

Check for a null reference before calling ToString:

var period = Request.QueryString["period"];
if (period != null) {
    DateTime dt1 = Convert.ToDateTime(period.ToString());
}
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
1

Check if that parameter is there in your QueryString.

TimeSpan ts;
if(null != Request.QueryString["period"])
{
  DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString());
  DateTime dt2 = DateTime.Now;
  TimeSpan ts = dt2 - dt1;
}
nunespascal
  • 17,584
  • 2
  • 43
  • 46
1

If DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString()) is throwing error then than means you don't have period in your querystring. Better to check

if(Request.QueryString["period"]!=null)
{
  DateTime dt1 = Convert.ToDateTime(Request.QueryString["period"].ToString());
 . .
 . .
}
Ken Clark
  • 2,500
  • 15
  • 15