In my application, that connecting to MS Sql database, I am using Microsoft.Data.ConnectionUI
And my application work in my computer. If i run this application in another computer, when i open connection dialog, i see that error:
That is my code for this:
try
{
connectionString = ShowDialogConnection();
SqlConnection connect = new SqlConnection(connectionString);
connect.Open();
backgroundWorker1.RunWorkerAsync();
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
string ShowDialogConnection()
{
string conn = "";
DataConnectionDialog dlg = new DataConnectionDialog();
DataSource.AddStandardDataSources(dlg);
dlg.SelectedDataSource = DataSource.SqlDataSource;
dlg.SelectedDataProvider = DataProvider.SqlDataProvider;
if (ConfigurationManager.ConnectionStrings["ConStr"] != null)
{
dlg.ConnectionString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
}
if (DataConnectionDialog.Show(dlg) == DialogResult.OK)
{
if (dlg.ConnectionString != null && dlg.ConnectionString != "")
{
conn = dlg.ConnectionString;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection csSection = config.ConnectionStrings;
csSection.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
csSection.SectionInformation.ForceSave = true;
ConnectionStringSettings csSettings = new ConnectionStringSettings("ConStr", dlg.ConnectionString, "System.Data.SqlClient");
if (csSection.ConnectionStrings["ConStr"] != null)
csSection.ConnectionStrings.Remove("ConStr");
csSection.ConnectionStrings.Add(csSettings);
config.Save(ConfigurationSaveMode.Modified);
}
}
return conn;
}
What I need to do with this?
Bad Data is usually caused by using the wrong key. It sounds like you are encrypting the .config file on one machine (your dev machine?) and the attempting to decrypt on a different machine. This will not work as the decryption key is missing.
The encrypted config section should be encrypted on the machine where the application is running so that it uses the appropriate key.
Yes, what comes to my mind is that you have enabled encryption
in your Web.Config to the <connectionStrings>
section. This type of problem occurs when you deploy your application on a machine on which encryption was not performed. Encryption uses Machine level key which would be missing on your development machine. Following options come to my mind
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With