private void btnGenerateCertificate_Click(object sender, EventArgs e) { var yearFrom = cmbBoxValidFromYear.SelectedItem; var yearTill = cmbBoxValidTillYear.SelectedItem; string monthFrom = ""; string monthTill = ""; var dayFrom = cmbBoxValidFromDay.SelectedItem; var dayTill = cmbBoxValidTillDay.SelectedItem; try { DateTime validFrom; DateTime validTill; if (cmbBoxValidFromMonth.SelectedItem != null) { monthFrom = cmbBoxValidFromMonth.SelectedItem.ToString(); validFrom = new DateTime(Convert.ToInt32(yearFrom), Month[monthFrom], Convert.ToInt32(dayFrom)); } else { monthFrom = cmbBoxValidFromMonth.Text; validFrom = new DateTime(Convert.ToInt32(yearFrom), Convert.ToInt32(monthFrom), Convert.ToInt32(dayFrom)); } if (cmbBoxValidTillMonth.SelectedItem != null) { monthTill = cmbBoxValidTillMonth.SelectedItem.ToString(); validTill = new DateTime(Convert.ToInt32(yearTill), Month[monthTill], Convert.ToInt32(dayTill)); } else { monthTill = cmbBoxValidTillMonth.Text; validTill = new DateTime(Convert.ToInt32(yearTill), Convert.ToInt32(monthTill), Convert.ToInt32(dayTill)); } if ((validTill < DateTime.UtcNow.Date) || (validTill < DateTime.UtcNow.Date)) { lblMessage.Visible = true; lblMessage.Text = "Date is in the past!"; return; } if (validTill < validFrom) { lblMessage.Visible = true; lblMessage.Text = String.Format("{0} is before {1}! The certificate could not be created!", validTill, validFrom); } else { CertificateGenerator certificateGenerator = new CertificateGenerator(txtBoxSubjectName.Text, cmbBoxSignatureAlgorithm.SelectedItem.ToString(), validFrom, validTill); var cert = certificateGenerator.CreateCertificate(); certificateGenerator.StoreCertificate(cert, StoreName.My, StoreLocation.CurrentUser); lblMessage.Visible = true; lblMessage.Text = "The certificate was successfully created!"; btnView.Visible = true; } } catch (Exception ex) { lblMessage.Visible = true; lblMessage.Text = ex.Message; } }