1

I am trying to save my connection string to a configuration file but the password is not being submitted.

Here is the code I am using:

    using (var con = new SqlConnection(string.Format("Data Source={0};Initial Catalog={3};User ID={1};Password={2};",
           textBox_SqlServer.Text, textBox_Username.Text, textBox_Password.Text, comboBox_DatabaseName.Text)))
    {
      // test connection before continuing
      con.Open();
      configs.ApplicationConfiguration.SetConnectionString(con.ConnectionString);
    }

All is fine until con.ConnectionString and it does not include the password. What do I need to do to So, naturally, when I go to retrieve it, the password is not there.

How do I get the password to set also?

3
  • code is fine , just break point and debug and see if Text has value at that point, mostly this happen due to Postback , Commented Apr 7, 2014 at 16:05
  • When I hit con.ConnectionString the password isn't even in the string anymore... I guess it is stripping it out. Commented Apr 7, 2014 at 16:10
  • @ErocM, IT IS stripping it out. See my answer. Commented Apr 7, 2014 at 16:11

1 Answer 1

5

The ConnectionString is similar to an OLE DB connection string, but is not identical. Unlike OLE DB or ADO, the connection string that is returned is the same as the user-set ConnectionString, minus security information if the Persist Security Info value is set to false (default). The .NET Framework Data Provider for SQL Server does not persist or return the password in a connection string unless you set Persist Security Info to true.

From the MSDN documentation. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! I added Persist Security Info=True; and it gives me what I need. TYVM!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.