title: Error when executing a stored proc in asp.net
body: <p>I am attempting to execute a stored proc in asp.net in the code behind. The parameter I am trying to pass is <strong>strErrorMessage</strong> that contains a value of <code>"The transport failed to connect to the server.; "</code>.</p>
  
  <p>The error message when the query gets executed is: <code>The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 1 ("@errMessage"): Data type 0xE7 has an invalid data length or metadata length.</code></p>
  
  <p><strong>Update with code</strong>  </p>
  
  <pre><code>    try
      {
          ...
          ...
          ...
      }
          catch (Exception ex)
          {
              ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email was not sent - " + ex.Message + "');", true);
  
              string strMessage = ex.Message;
              string strStackTrace = ex.StackTrace;
  
              strMessage = strMessage.Replace("\r\n", "; ");
              strMessage = strMessage.Replace("   ", "");
  
              strStackTrace = strStackTrace.Replace("\r\n", "; ");
              strStackTrace = strStackTrace.Replace("   ", "");
              AppErrorLog(strMessage, strStackTrace);
              return false;
          }
  
  
      protected void AppErrorLog(string strErrorMessage, string strErrorStackTrace)
      {
          SqlConnection conErrLog = new SqlConnection(strConn);
          string sql = "usp_AppErrorLog_AddRecord";
          SqlCommand cmdErrLog = new SqlCommand(sql, conErrLog);
          conErrLog.Open();
          try
          {
              cmdErrLog.CommandType = CommandType.StoredProcedure;
  
              cmdErrLog.Parameters.Add(new SqlParameter("@errMessage", SqlDbType.NVarChar, 8000));
              cmdErrLog.Parameters["@errMessage"].Value = strErrorMessage;
  
              cmdErrLog.Parameters.Add(new SqlParameter("@errStackTrace", SqlDbType.NVarChar, 8000));
              cmdErrLog.Parameters["@errStackTrace"].Value = strErrorStackTrace;
  
              cmdErrLog.Parameters.Add(new SqlParameter("@userID", SqlDbType.VarChar, 12));
              cmdErrLog.Parameters["@userID"].Value = User.Identity.Name;
  
              SqlDataAdapter ada = new SqlDataAdapter(cmdErrLog);
              cmdErrLog.ExecuteNonQuery();
          }
          catch(Exception e)
          {
              ClientScript.RegisterStartupScript(GetType(), "alert", "alert('AppErrorLog - " + e.Message + "');", true);
          }
          finally
          {
              conErrLog.Close();
          }
      }
  </code></pre>
  
  <p>The column datatype in the table is nvarchar(MAX).<br>
  Any ideas how to resolve this?</p>