应用程序关闭时出现Linq SQL日期时间调用错误

本文关键字:日期 SQL 时间 调用 错误 Linq 应用程序 | 更新日期: 2023-09-27 18:20:57

我很难弄清楚如何解决这个问题。它运行得很好,但在上引发TargetInvocationException错误

var person = (from c in DB.Persons 
                          where c.ID == Convert.ToInt32(cmbID.Text)
                          select c).First();

关闭应用程序时。错误出现在一个名为dob的DateTime字段中,但我不确定如何正确获取它。

private void cmbID_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Fill out the other boxes in relation to the person ID selected.
        var person = (from c in DB.Persons 
                      where c.ID == Convert.ToInt32(cmbID.Text)
                      select c).First();
        txtFName.Text = person.FName.ToString();
        txtLName.Text = person.LName;
        dateTimePicker1.Text = person.DOB.ToShortDateString();
        txtPhone.Text = person.Phone;
        txtAdd1.Text = person.Add1;
        txtAdd2.Text = person.Add2;
        txtSuburb.Text = person.Suburb;
        cmbState.Text = person.State;
        txtPostcode.Text = person.Postcode;
        cmbType.Text = person.Type;
    }

我是Linq和SQL的新手,如果错误明显,我很抱歉。

应用程序关闭时出现Linq SQL日期时间调用错误

请尝试以下删除事件的操作,这样它就不会被触发。

使用系统;使用System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.FormClosing += formClosing;
        }
        private void formClosing(object sender, FormClosingEventArgs e)
        {
            comboBox1.SelectedIndexChanged -= comboBox1SelectedIndexChanged;
        }
        private void comboBox1SelectedIndexChanged(object sender, EventArgs e)
        {
            // your code
        }
    }
}