title: Blazor, Dapper - A connection was successfully established with the server, but then an error occurred during the pre-login handshake (Error)
body: <p>I'm fallowing on of the udemy tutorials to connect to MS SQL with Blazor and Dapper. Tutorial connects to localdb but I want to do it with online server that I have. My connection strings are all fine. I can connect to DB with my VS Studio. I dont understand where the problem might be. Here is error code what I have in Console when I try to insert into database. It also says something about line 39 and line 31. I have pasted the C# code and marked with line it is with comment. Was trying to find solution on web but could not find any. Maybe someone have an idea why I cant insert data into my database and what is causing this error.</p>
  <p>I was thinking that my connection string might be the issue but I'm sure it's fine I have tested it in .net framework and I could insert into SQL without any problem with exactly the same string connection.</p>
  <pre><code>fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
        Unhandled exception in circuit 'TXbqG0ewGPtD7iEweWor5mR-q0P-zPZJ8EdkuoqLCkQ'.
  Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: HTTP Provider, error: 0 - )
     at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
     at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
     at Microsoft.Data.SqlClient.TdsParser.ConsumePreLoginHandshake(Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean&amp; marsCapable, Boolean&amp; fedAuthRequired)
     at Microsoft.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover, SqlAuthenticationMethod authType)
     at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
     at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
     at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
     at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool)
     at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
     at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
     at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
     at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
     at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)
     at Microsoft.Data.ProviderBase.DbConnectionPool.WaitForPendingOpen()
  --- End of stack trace from previous location where exception was thrown ---
     at Dapper.SqlMapper.ExecuteImplAsync(IDbConnection cnn, CommandDefinition command, Object param) in /_/Dapper/SqlMapper.Async.cs:line 678
     at BlazorDapperCRUD.Data.VideoService.VideoInsert(Video video) in D:\VSrepos\repos\BlazorDapperCRUD\Data\VideoService.cs:line 31
     at BlazorDapperCRUD.Pages.VideoAdd.VideoInsert() in D:\VSrepos\repos\BlazorDapperCRUD\Pages\VideoAdd.razor:line 39
     at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
     at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
     at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
     at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
  ClientConnectionId:fad02929-0736-43c4-a7da-573a3219cd0e
  </code></pre>
  <pre><code>@using BlazorDapperCRUD.Data
  @page &quot;/videoadd&quot;
  @inject IVideoService VideoService
  @inject NavigationManager NavigationManager
  
  &lt;h1&gt;Add Video&lt;/h1&gt;
  
  &lt;body&gt;
      &lt;EditForm Model=&quot;@video&quot; OnValidSubmit=&quot;@VideoInsert&quot;&gt;
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;td&gt;Video Title:&lt;/td&gt;
                  &lt;td&gt;&lt;input type=&quot;text&quot; @bind=&quot;video.Title&quot; required /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;Date Published:&lt;/td&gt;
                  &lt;td&gt;&lt;input type=&quot;date&quot; @bind=&quot;video.DatePublished&quot; required /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;Is Active:&lt;/td&gt;
                  &lt;td&gt;&lt;input type=&quot;checkbox&quot; @bind=&quot;video.IsActive&quot; /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td colspan=&quot;2&quot; style=&quot;text-align:center&quot;&gt;
                      &lt;input type=&quot;submit&quot; value=&quot;Add&quot; /&gt;
                      &lt;input type=&quot;button&quot; value=&quot;Cancel&quot; @onclick=&quot;@Cancel&quot; /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/EditForm&gt;
  &lt;/body&gt;
  
  @code {
      // Create new, empty video object
      Video video = new Video();
  
      protected async Task VideoInsert()
      {
  //this is line 39
          await VideoService.VideoInsert(video);
          NavigationManager.NavigateTo(&quot;/&quot;);
      }
      
      void Cancel()
      {
          NavigationManager.NavigateTo(&quot;/&quot;);
      }
  }
  </code></pre>
  <pre><code>{
      public class VideoService : IVideoService
      {
          //Database connection
          private readonly SqlConnectionConfiguration _configuration;
          public VideoService(SqlConnectionConfiguration configuration)
          {
              _configuration = configuration;
          }
  
          public async Task&lt;bool&gt; VideoInsert(Video video)
          {
              using (var conn = new SqlConnection(_configuration.Value))
              {
                  var parameters = new DynamicParameters();
                  parameters.Add(&quot;Title&quot;, video.Title, DbType.String);
                  parameters.Add(&quot;DatePublished&quot;, video.DatePublished, DbType.Date);
                  parameters.Add(&quot;IsActive&quot;, video.IsActive, DbType.Boolean);
                  const string query = @&quot;INSERT INTO Video VALUES (@Title, @DatePublished, @IsActive)&quot;;
                  await conn.ExecuteAsync(query, new { video.Title, video.DatePublished, video.IsActive }, commandType: CommandType.Text);
              }
  //this is line 31
              return true;
          }
      }
  }
  </code></pre>
  <p>My connection string is in appsettings.json
  I have copy pasted it after I connected with MSSQL via SQL Server Object Explorer in VS Studio 2019.</p>
  <pre><code>{
    &quot;Logging&quot;: {
      &quot;LogLevel&quot;: {
        &quot;Default&quot;: &quot;Information&quot;,
        &quot;Microsoft&quot;: &quot;Warning&quot;,
        &quot;Microsoft.Hosting.Lifetime&quot;: &quot;Information&quot;
      }
    },
    &quot;AllowedHosts&quot;: &quot;*&quot;,
    &quot;ConnectionStrings&quot;: {
      &quot;SqlDBcontextBlazorDapperCRUD&quot;: &quot;Data Source=hostname;Initial Catalog=mycatalog;User ID=myusername;Password=mypw;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False&quot;
    }
  }
  </code></pre>