简体   繁体   English

输入字符串不是日期时间的正确格式字符串

[英]Input String Was Not In a Correct Format String to Datetime

using (SqlCommand sc = new SqlCommand("Purchase_Order_History", con))
{
    MessageBox.Show("Agya");
    sc.CommandType = CommandType.StoredProcedure;
    sc.Parameters.Add("@Invoice_no", SqlDbType.Int).Value = Convert.ToInt32(dgv.Rows[i].Cells["Column1"].Value);
    sc.Parameters.Add("@Date_of_Purchase", SqlDbType.Date).Value = Date_of_Purchase.Value.ToString("yyyy/MM/dd");
    sc.Parameters.Add("@Item_Name", SqlDbType.NVarChar).Value = dgv.Rows[i].Cells["Column2"].Value;
    sc.Parameters.Add("@Seller_Name", SqlDbType.NVarChar).Value = dgv.Rows[i].Cells["Column3"].Value;
    sc.Parameters.Add("@Company_Name", SqlDbType.NVarChar).Value = dgv.Rows[i].Cells["Column4"].Value;
    sc.Parameters.Add("@Quantity", SqlDbType.Int).Value = Convert.ToInt32(dgv.Rows[i].Cells["Column5"].Value);
    sc.Parameters.Add("@Unit_Price", SqlDbType.Int).Value = Convert.ToInt32(dgv.Rows[i].Cells["Column6"].Value);
    sc.Parameters.Add("@Total_Price", SqlDbType.Int).Value = Convert.ToInt32(dgv.Rows[i].Cells["Column7"].Value);
    sc.Parameters.Add("@Discount", SqlDbType.Int).Value = Convert.ToInt32(Discount.Text);
    sc.Parameters.Add("@Paid_Amount", SqlDbType.Int).Value = Convert.ToInt32(Paid_Amount.Text);
    sc.Parameters.Add("@Remaining", SqlDbType.Int).Value = Convert.ToInt32(lbl_Remaining.Text);
    sc.Parameters.Add("@Sub_Total", SqlDbType.Int).Value = Convert.ToInt32(lbl_Subtotal.Text);
    sc.Parameters.Add("@Total", SqlDbType.Int).Value = Convert.ToInt32(lbl_Total.Text);
    con.Open();
    sc.ExecuteNonQuery();
    con.Close();
}

I don't know what is happening here I am getting this exception Input String Was Not In a Correct Format我不知道这里发生了什么我收到了这个异常 Input String Was Not In a Correct Format

检查错误图像

You are converting the date to a string with Date_of_Purchase.Value.ToString("yyyy/MM/dd") .您正在使用Date_of_Purchase.Value.ToString("yyyy/MM/dd")将日期转换为字符串。 But the parameter @Date_of_Purchase is of type SqlDbType.Date , not of a text type.但是参数@Date_of_PurchaseSqlDbType.Date类型,而不是文本类型。 Therefore, change the code line to因此,将代码行更改为

sc.Parameters.Add("@Date_of_Purchase", SqlDbType.Date).Value = Date_of_Purchase.Value;

(Assuming that Date_of_Purchase is a DateTime? and you checked that it is not null.) (假设Date_of_PurchaseDateTime?并且您检查它不为空。)

Note: You cannot format a date when storing it in a date or date/time column.注意:将日期存储在日期或日期/时间列中时,您无法格式化日期。 You have to format it when displaying it.显示时必须对其进行格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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