错误提示如下: String was not recognized as a valid DateTime. 可能是myCommand.Parameters.Add(new SqlParameter("@CoTime",SqlDbType.DateTime,8)); myCommand.Parameters.Add(new SqlParameter("@ITTime",SqlDbType.DateTime,8)); myCommand.Parameters[cols[i]].Value=colvalue; 而colvalue是String 类型,各位大虾,我如何才能把datagrid上更新以后的datatime类型的值传回数据库呢?
for (int i=0;i<numCols-2;i++) { String colvalue=((TextBox)E.Item.Cells[i].Controls[0]).Text.ToString(); if (4<i && i<7) myCommand.Parameters[cols[i]].Value=DateTime.Parse(colvalue); else myCommand.Parameters[cols[i]].Value=colvalue; } 我都如此改了,为什么还报同样的错误?谢谢!
错误提示: String was not recognized as a valid DateTime. 当执行到datetime类型时, myCommand.Parameters[cols[i]].Value=DateTime.Parse(colvalue); 报错! 另:小弟很菜,不知道调试的时候如何看等号两边的值,请教了!
string ConnString="data source=jzj;database=DataPlatform;user id=rs;pwd=rs";你可以
catch(Exception ex)
{
Response.Write(ex.Message);
}
{
myCommand.ExecuteNonQuery();
DGPerson.EditItemIndex=-1;
}
catch
{
throw;
Response.Redirect("error.aspx");
}
这样会把异常抛出来,等你调试好了再把这句删了,如果看了异常还不知是什么错,请你把异常信息贴出来。
String was not recognized as a valid DateTime. 可能是myCommand.Parameters.Add(new SqlParameter("@CoTime",SqlDbType.DateTime,8));
myCommand.Parameters.Add(new SqlParameter("@ITTime",SqlDbType.DateTime,8)); myCommand.Parameters[cols[i]].Value=colvalue;
而colvalue是String 类型,各位大虾,我如何才能把datagrid上更新以后的datatime类型的值传回数据库呢?
所以你的代码里面凡是不是VarChar类型的参数,都不能用:
myCommand.Parameters[cols[i]].Value=colvalue;
这样赋值如果要赋DateTime的值,可以这样:
myCommand.Parameters[X].Value = DateTime.Parsr( colvalue ); //X为某一参数索引
{
String colvalue=((TextBox)E.Item.Cells[i].Controls[0]).Text.ToString();
if (4<i || i<7)
myCommand.Parameters[cols[i]].Value=DateTime.Parse(colvalue);
else
myCommand.Parameters[cols[i]].Value=colvalue;
}
我都如此改了,为什么还报同样的错误?谢谢!
你在
myCommand.ExecuteNonQuery();这一句上下个断点调试看看,参数的值都设对了没有
{
String colvalue=((TextBox)E.Item.Cells[i].Controls[0]).Text.ToString();
if (4<i && i<7)
myCommand.Parameters[cols[i]].Value=DateTime.Parse(colvalue);
else
myCommand.Parameters[cols[i]].Value=colvalue;
}
我都如此改了,为什么还报同样的错误?谢谢!
String was not recognized as a valid DateTime.
当执行到datetime类型时,
myCommand.Parameters[cols[i]].Value=DateTime.Parse(colvalue);
报错!
另:小弟很菜,不知道调试的时候如何看等号两边的值,请教了!
问题可能是这样,我在数据库中的datime类型的两个字段皆为null,也就是执行到下面语句的时候colvalue为空!
myCommand.Parameters[cols[i]].Value=DateTime.Parse(colvalue);是不是当colvalue为空的时候,就能不转换为DateTime类型??
myCommand.Parameters[cols[i]].Value=DateTime.Parse(colvalue);
myCommand.Parameters[cols[i]].Value=DBNull.Value;
else
myCommand.Parameters[cols[i]].Value=DateTime.Parse(colvalue);
if( colvalue=="" )
myCommand.Parameters[cols[i]].Value=DBNull.Value;
else
myCommand.Parameters[cols[i]].Value=DateTime.Parse(colvalue);程序终于通过了了!
再次谢谢你 timmy3310(tim) !