无法将参数值从 DateTime 转换为 Byte[]
我在将参数从 DateTime 转换为 Byte[] 时出错。 这个想法是显示通过控件输入并显示在 GridView 上的 2 个指定日期之间的数据,并使用存储过程来访问数据。我不明白这个错误,但我猜测所有数据都放入数组中并传递给存储过程:
string sDateBegin = Request.Form["fromDate"];
DateTime dtDateBegin = Convert.ToDateTime(sDateBegin);
SqlParameter prmDateBegin = new SqlParameter("datebegin", SqlDbType.Timestamp);
prmDateBegin.Value = dtDateBegin;
cmdProc.Parameters.Add(prmDateBegin);
//same code for DateEnd
// data table
DataTable dataTable = new DataTable();
AGridView.DataSourceID = null;
// data adapter
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdProc);
AGridView.DataSource = dataTable;
//fill datatable
dataAdapter.Fill(dataTable);
I'm getting error converting parameters from DateTime to Byte[].
The idea is to show data between 2 specified dates that are entered via controls and displayed on GridView, and using a stored procedure to access data. I don't understand the error, but I'm guessing that all the data is put in an Array and passed on to stored procedure:
string sDateBegin = Request.Form["fromDate"];
DateTime dtDateBegin = Convert.ToDateTime(sDateBegin);
SqlParameter prmDateBegin = new SqlParameter("datebegin", SqlDbType.Timestamp);
prmDateBegin.Value = dtDateBegin;
cmdProc.Parameters.Add(prmDateBegin);
//same code for DateEnd
// data table
DataTable dataTable = new DataTable();
AGridView.DataSourceID = null;
// data adapter
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdProc);
AGridView.DataSource = dataTable;
//fill datatable
dataAdapter.Fill(dataTable);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已将 SqlParameter 定义为 Timestamp 数据类型(这是一个字节数组)而不是 DateTime。鉴于您代表的是日期范围,听起来您应该将参数数据类型更改为 DateTime。
You've defined the SqlParameter as a Timestamp data type (which is a byte array) rather than a DateTime. Given you're representing a date range it sounds like you should change the parameter data type to DateTime.
发生错误的原因是 Convert.ToDateTime 尝试执行从任何对象到 DateTime 的隐式转换。
来自控件的对象是一个字符串,但不能转换为 DateTime 对象。
它实际上是由字符串表示的日期和时间。
构造
DateTime
对象的正确方法是解析字符串The error occurred because Convert.ToDateTime tries to do an implicit conversion from any object to a DateTime.
The object coming from the control is a string, but not one that could be cast to a DateTime object.
It is in fact a date and time represented by a string.
The correct way to construct a
DateTime
object is by parsing the string with