Conversion failed when converting datetime from character string

Posted by salvationishere on Stack Overflow See other posts from Stack Overflow or by salvationishere
Published on 2010-06-09T17:36:29Z Indexed on 2010/06/09 18:32 UTC
Read the original article Hit count: 225

Filed under:
|
|

I am developing a C# VS 2008 / SQL Server 2005 Express website application. I have tried some of the fixes for this problem but my call stack differs from others. And these fixes did not fix my problem. What steps can I take to troubleshoot this?

Here is my error:

System.Data.SqlClient.SqlException was caught
  Message="Conversion failed when converting datetime from character string."
  Source=".Net SqlClient Data Provider"
  ErrorCode=-2146232060

  LineNumber=10
  Number=241
  Procedure="AppendDataCT"
  Server="\\\\.\\pipe\\772EF469-84F1-43\\tsql\\query"
  State=1
  StackTrace:
       at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
       at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at ADONET_namespace.ADONET_methods.AppendDataCT(DataTable dt, Dictionary`2 dic) in c:\Documents and Settings\Admin\My Documents\Visual Studio 2008\WebSites\Jerry\App_Code\ADONET methods.cs:line 102

And here is the related code. When I debugged this code, "dic" only looped through the 3 column names, but did not look into row values which are stored in "dt", the Data Table.

public static string AppendDataCT(DataTable dt, Dictionary<string, string> dic)
{
    if (dic.Count != 3)
        throw new ArgumentOutOfRangeException("dic can only have 3 parameters");

    string connString = ConfigurationManager.ConnectionStrings["AW3_string"].ConnectionString;
    string errorMsg;

    try
    {               
        using (SqlConnection conn2 = new SqlConnection(connString))
        {
            using (SqlCommand cmd = conn2.CreateCommand())
            {
                cmd.CommandText = "dbo.AppendDataCT";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn2;
                foreach (string s in dic.Keys)
                {
                    SqlParameter p = cmd.Parameters.AddWithValue(s, dic[s]);
                    p.SqlDbType = SqlDbType.VarChar;
                }

                conn2.Open();
                cmd.ExecuteNonQuery();
                conn2.Close();
                errorMsg = "The Person.ContactType table was successfully updated!";
            }
        }
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about sql-server-2005