[英]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.现在,如果isValid == true
您可以将tempDateTimeFecha
作为参数传递给BD.AddAcusado()
方法。
I had this same issue trying to parse the Date column of my datagridview.我在尝试解析 datagridview 的日期列时遇到了同样的问题。 The following procedure helped me resolve it: I checked my loop body and decreased the maximum number for loop termination by 1.以下过程帮助我解决了这个问题:我检查了我的循环体并将循环终止的最大数量减少了 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.请注意:解析日期取决于格式(查看此https://docs.microsoft.com/en-us/dotnet/standard/base-types/parsing-datetime了解更多信息。更重要的是,如果您的循环体试图传递一个null或empty ,那么它肯定会弹出错误。
Also, remember to catch the exception but not after checking for coding error.另外,请记住捕获异常,但不要在检查编码错误后捕获。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.