C# 单击按钮时发生SqlDateTime溢出错误

C# 单击按钮时发生SqlDateTime溢出错误,c#,sql,stored-procedures,buttonclick,C#,Sql,Stored Procedures,Buttonclick,当我执行应用程序时,应用程序运行。。。然后,当我点击按钮时,我得到了错误 SqlDateTime溢出。必须在1753年1月1日12:00:00 AM和9999年12月31日11:59:59 PM之间 我不知道如何着手解决这个问题,因为互联网上的大多数解决方案似乎都不适合我。这是SQL方面的问题吗 private void button1_Click(object sender, EventArgs e) { SqlConnection sqlcn1 = new SqlConnection

当我执行应用程序时,应用程序运行。。。然后,当我点击按钮时,我得到了错误

SqlDateTime溢出。必须在1753年1月1日12:00:00 AM和9999年12月31日11:59:59 PM之间

我不知道如何着手解决这个问题,因为互联网上的大多数解决方案似乎都不适合我。这是SQL方面的问题吗

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection sqlcn1 = new SqlConnection("My Connection String");
    sqlcn1.Open();

    //Stored Procedure
    SqlCommand sqlcmddel = new SqlCommand("My_Stored_Procedure", sqlcn1);
    sqlcmddel.CommandType = CommandType.StoredProcedure;
    sqlcmddel.ExecuteNonQuery();

    sqlcn1 = new SqlConnection("My Connection String");
    sqlcn1.Open();

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        SqlCommand sqlcmdins = new SqlCommand("My_Stored_Procedure", sqlcn1);

        sqlcmdins.CommandType = CommandType.StoredProcedure;

        //Stored Procedure
        sqlcmdins.Parameters.Add("@sugnum", SqlDbType.Int).Value = Convert.ToInt64 (row.Cells[0].Value);
        sqlcmdins.Parameters.Add("@sugtype", SqlDbType.NVarChar, 50).Value = Convert.ToString(row.Cells[1].Value);
        sqlcmdins.Parameters.Add("@buyerid", SqlDbType.NVarChar, 50).Value = Convert.ToString (row.Cells[2].Value);
        sqlcmdins.Parameters.Add("@duedate", SqlDbType.DateTime).Value = Convert.ToDateTime (row.Cells[3].Value);
        sqlcmdins.Parameters.Add("@xrelqty", SqlDbType.Float).Value = Convert.ToDouble (row.Cells[4].Value);
        sqlcmdins.Parameters.Add("@purchasingfactor", SqlDbType.Float).Value = Convert.ToDouble (row.Cells[5].Value);
    }
}

在SQL端将数据类型更改为datetime2(很抱歉回答得这么短,无法发表评论)

尝试传递字符串值,参数将找出正确的数据类型示例
qlcmdins.parameters.Add(“@duedate”,row.Cells[3].value.ToString())

您试图在sql中输入的日期时间值是什么?我试图获取sql表中的值我猜
行。单元格[3]。值
不包含您认为它包含的内容!正如@ChrisDunaway所说,您收到的错误通常表明该值不是格式正确的日期时间,或者根本不是日期时间。@Sudsy1002哈哈,请您看看……它在我的单元格中显示为“M/DD/yyyy HH:MM:SS”[3]。。。现在解决这个问题的办法是什么?试过了,然后。。。。“将datetime2数据类型转换为datetime数据类型导致值超出范围”:/@hrutk1ta如前所述,我猜该列中的值不是您期望的值,请尝试调试并找出该行的值类型和值“NullReferenceException未处理”。。。。“对象引用未设置为对象的实例”:(