System.ArgumentException: 'Password not Supported: 'trust server certificate'.'

Gonçalo Cruz 0 Reputation points
2024-03-27T15:36:45.3766667+00:00

SqlConnection conn = new SqlConnection(@"Data Source=<SERVERNAME>;User ID=erp;Password=***********;Encrypt=True;Trust Server Certificate=True");

This is the line of code where i get the error, i have tried taking off the Trust Server Certificate but that gives me another saying i need it, now that error goes away if I put Encrypt=False but then i get a different saying that it cant get the user 'erp' if anyone could help it would be very much appreciated

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,829 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,718 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 48,281 Reputation points
    2024-03-27T16:01:45.64+00:00

    Your connection string is malformed. My gut instinct is that your password contains a quote, equal or semicolon which is throwing off the connection string parser resulting in the odd error. However I also notice you don't specify a database name which is odd to me but maybe you're relying on the default database connection??

    Do the following in Visual Studio.

    1. Go to SQL Server Object Explorer under the View menu.
    2. Add a SQL Server instance.
    3. Enter the database server name, database you're connecting to and the credentials to use. Also check the option to encrypt the data.
    4. Note that many SQL databases are using self-signed certs. If that is your case then you need to set Trust Server Certificate to true otherwise it will fail to connect. If you actually have a certificate that you managed then set it to false.
    5. Ensure you can connect successfully to the database.
    6. Open the properties of the database in Object Explorer window and find the Connection String property.
    7. Copy the value and paste it into your code. Note that it'll add extra stuff around MARS and whatnot that you can remove if you like. The only fields that are generally needed are: server, database, User Id/Password or Integrated Security, Encrypt and Trust Server Certificate.