【发布时间】:2012-04-04 00:20:03
【问题描述】:
我正在尝试插入今天的日期并尝试递归地增加日期。但我收到了转换错误消息。
private void InsertTimesheetWeek(string timeSheetID)
{
int row = GViewTimeSheet.Rows.Count;//get the row count
int counter = 0;
string[] txtDate = new string[row];// date column
foreach (GridViewRow gRow in GViewTimeSheet.Rows)
{
txtDate[counter] = "GetDATE()+"+counter;
counter++;
}
//Intializing sql statement
string fields = "(TimeSheetID, Date)";
string parm = "(@TimeSheetID, @Date)";
string sqlStatement = "insert into TimeSheetWeeks" + fields + "Values" + parm;
SqlCommand comm = new SqlCommand();
comm.CommandText = sqlStatement;//assing sql statement as command
SqlConnection connection = DataAccess.getConnection();
comm.Connection = connection;
try
{
connection.Open();
for (int i = 0; i < row; i++)
{
comm.Parameters.AddWithValue("@TimeSheetID", timeSheetID);
comm.Parameters.AddWithValue("@Date", txtDate[i]);
comm.ExecuteNonQuery();
comm.Parameters.Clear();
}
}
catch (Exception ex)
{
Utilities.LogError(ex);
throw ex;
}
finally
{
if (connection.State == ConnectionState.Open)// if the connection opened then
{
connection.Close();//just close the connection in any way
}
}
}
为什么会导致错误?
【问题讨论】:
-
您收到的错误信息是什么?还可以尝试使用断点运行并检查您传递的值是什么 "@Date", txtDate[i],可能是 txtDate[i] 中的值导致错误跨度>
-
错误消息:“从字符串转换日期和/或时间时转换失败。”。我尝试调试,一切似乎都很好(值“GetDATE()+0”、“GetDATE()+1”、“GetDATE()+2”....)
标签: asp.net sql-server database