简体   繁体   中英

Parse the string to take the date before putting each variable into datetime object

I get this warning when I try to execute the attaching code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace prueb2 {
   public partial class Acusado: Form {
       public Acusado() {
           InitializeComponent();
       }

       private void Acusado_Load(object sender, EventArgs e) {
           listView1.Items.Clear();
           List < ACUSADOcLAS > listaAcusado;

           try {
               listaAcusado = BD.GetAcusado();
               if (listaAcusado.Count > 0) {
                   ACUSADOcLAS acusado;
                   for (int i = 0; i < listaAcusado.Count; i++) {
                       acusado = listaAcusado[i];
                       listView1.Items[i].SubItems.Add(acusado.NumeroSeguroSocial.ToString());
                       listView1.Items[i].SubItems.Add(acusado.NombreAcusado);
                       listView1.Items[i].SubItems.Add(acusado.ApellidoPaternoAcusado);
                       listView1.Items[i].SubItems.Add(acusado.ApellidoMaternoAcusado);
                       listView1.Items[i].SubItems.Add(acusado.FechaNacimientoAcusado.ToString());

                       listView1.Items[i].SubItems.Add(acusado.GeneroAcusado);
                       listView1.Items[i].SubItems.Add(acusado.DireccionAcusado);
                       listView1.Items[i].SubItems.Add(acusado.NivelMaximoEstudiosAcusado);
                       listView1.Items[i].SubItems.Add(acusado.EtnicidadAcusado);
                       listView1.Items[i].SubItems.Add(acusado.ReligionAcusado);
                   }
               } else {
                   MessageBox.Show("No hay acusados", "Alerta");
               }
           } catch (Exception ex) {
               MessageBox.Show(ex.Message, ex.GetType().ToString());
           }
       }

       private void button1_Click(object sender, EventArgs e) {
           BD.AddAcusado(int.Parse(txtNumSeg.Text), txtNombreA.Text, txtApPat.Text, txtApMat.Text, DateTime.Parse(dateTimeFecha.Text), comboGenero.Text, txtDireccion.Text, comboEstudios.Text, txtEtnicidad.Text, txtReligion.Text);
           txtNumSeg.Text = "";
           txtNombreA.Text = "";
           txtApPat.Text = "";
           txtApMat.Text = "";
           dateTimeFecha.Text = "";
           comboGenero.Text = "";
           txtDireccion.Text = "";
           comboEstudios.Text = "";
           txtEtnicidad.Text = "";
           txtReligion.Text = "";
           this.Acusado_Load(this, null);
       }

   }
}

Can you help me please?

You are probably getting exception because of the invalid date string. You need to validate it first.

Try this:

DateTime tempDateTimeFecha;
bool isValid = DateTime.TryParse(dateTimeFecha.Text, out tempDateTimeFecha);

Now if isValid == true you can pass tempDateTimeFecha as argument to the BD.AddAcusado() method.

I had this same issue trying to parse the Date column of my datagridview. The following procedure helped me resolve it: I checked my loop body and decreased the maximum number for loop termination by 1.

    int days = 0;
    Datetime d;
    for (int i = 1; i < pastedCells -1; i++){ 
            d = Convert.ToDateTime(dgv_Copy.Rows[i].Cells[1].Value);
            days = DateTime.DaysInMonth(d.Year, d.Month);
            Console.WriteLine(days + "\t" + d);
       }
    //result:
    //31    2002-05-31 0:00
    //30    2002-11-30 0:00
    //31    2003-05-31 0:00
    //30    2003-06-30 0:00
    //31    2003-10-31 0:00
    //30    2003-11-30 0:00
    //29    2004-02-29 0:00
    //31    2004-10-31 0:00

Pls Note: Parsing Dates depend on the format (check this https://docs.microsoft.com/en-us/dotnet/standard/base-types/parsing-datetime for more info. MORE IMPORTANTLY, if your loop body tries to pass a null or empty , then it will definitely pop up error.

Also, remember to catch the exception but not after checking for coding error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM