尝试将日期从gridview单元格保存到SQL [英] Try to save date from gridview cell to SQL
问题描述
我通过progammicaly将gridview单元格作为日期时间选择器
现在我想在SQl中保存网格视图单元格日期。但我面临一个错误
请帮助我如何解决它一千次我从谷歌搜索它但没有得到任何解决方案
我收到此错误信息
I make gridview cell as a date time picker through progammicaly
now i want to save the grid view cell date in SQl . But i'm facing an error
please help me how can i solve it thousand time i search it from google but not get any solution
I'm getting this error message
"
System.Data.SqlTypes.SqlTypeException: 'SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.'
我是什么尝试过:
"
What I have tried:
foreach (DataGridViewRow row in StockdataGridView.Rows)
{
using (SqlCommand cmd = new SqlCommand("USP_Insert_Stock_Info", conn1))
{
cmd.CommandType = CommandType.StoredProcedure;
DateTime expdate = Convert.ToDateTime(row.Cells[5].Value );
cmd.Parameters.AddWithValue("@ExpDate", expdate);
}
} {
推荐答案
我认为您需要在该行放置一个断点:
I think you need to place a breakpoint at the line:
DateTime expdate = Convert.ToDateTime(row.Cells[5].Value );
并查看 expdate
看起来像是用鼠标悬停在它上面,你的存储过程的格式可能不正确。
你还应该检查row.Cells [5] .Value是否先不是NULL。 br />
可能还需要明确指定 SqlDbType
,如下所示:
and look what expdate
looks like by hovering over it with the mouse, the format is probably not correct for your stored procedure.
You should also check if row.Cells[5].Value is not NULL first.
It might also be needed to explicity specify the SqlDbType
, like this:
command.Parameters.Add(new SqlParameter("@ExpDate", SqlDbType.DateTime2) { Scale = 3, Value = expdate });
此处有更多信息:AddWithValue是邪恶的| Dan Guzman的博客 [ ^ ]
请参阅此示例 DateTimePicker
用法:如何:Windows窗体中的主机控件DataGridView单元| Microsoft Docs [ ^ ]
More info here: AddWithValue is Evil | Dan Guzman's Blog[^]
See example here for DateTimePicker
usage: How to: Host Controls in Windows Forms DataGridView Cells | Microsoft Docs[^]
这篇关于尝试将日期从gridview单元格保存到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!