Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Good day.
I am getting this error, I've tried everything suggested on the forum whithout success.
My application creates an SQL connection file when the save button is clicked. This is the error I am getting:
Could not load file or Assembly 'Microsoft.SqlServer.SqlClrProvider, Version = 13.100.0.0, Culture = neutral, PublickeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.

Code:
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;


private void btnSave_Click(object sender, EventArgs e)
       {
           try
          {
               if (cmbServerName.Text == "")
               {
                   MessageBox.Show("Please Select/Enter Server Name","SQL Server Settings", MessageBoxButtons.OK,MessageBoxIcon.Information);
                   cmbServerName.Focus();
                   return;
               }
               if (cmbAuthentication.SelectedIndex == 1)
               {
                   if (txtUserName.Text == "")
                   {
                       MessageBox.Show("please enter user name", "SQL Server Settings", MessageBoxButtons.OK, MessageBoxIcon.Information);
                      txtUserName.Focus();
                       return;
                   }
                   if (txtPassword.Text == "")
                   {
                       MessageBox.Show("please enter password", "SQL Server Settings", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       txtPassword.Focus();
                       return;
                   }
               }
               Cursor = Cursors.WaitCursor;
               timer1.Enabled = true;
               if (cmbAuthentication.SelectedIndex == 0)
               {
                  con = new SqlConnection(@"Data source= '" + cmbServerName.Text.ToString() + "';Initial Catalog=master;Integrated Security=True;");
               }
               if (cmbAuthentication.SelectedIndex == 1)
               {
                   con = new SqlConnection(@"Data Source=  '" + cmbServerName.Text.ToString() + "';Initial Catalog=master;User ID=  '" + txtUserName.Text.ToString() + "';Password='" + txtPassword.Text.ToString() + "'");
               }
               con.Open();
               if ((con.State == ConnectionState.Open))
               {
                 //  MessageBox.Show("please enter password", "SQL Server Settings", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   if (MessageBox.Show("It will create the Database and configure the sql server, Do you want to proceed?", "SQL Server Settings", MessageBoxButtons.YesNo , MessageBoxIcon.Information) == DialogResult.Yes)
                   {
                       using (StreamWriter sw = new StreamWriter(Application.StartupPath + "\\SQLSettings.dat"))
                       {
                           if (cmbAuthentication.SelectedIndex == 0)
                           {
                               sw.WriteLine(@"Data Source= '" + cmbServerName.Text.ToString() +"';Initial Catalog=DigitalCourtRollDB;Integrated Security=True");
                               sw.Close();
                           }
                           if (cmbAuthentication.SelectedIndex == 1)
                           {
                               sw.WriteLine(@"Data Source= '" + cmbServerName.Text.ToString() + "';Initial Catalog=DigitalCourtRollDB;User ID='" + txtUserName.Text.ToString()+"' ;Password= '" + txtPassword.Text.ToString() + "'");
                               sw.Close();
                           }
                           CreateDB();
                       }
                   }
                   else {
                       System.Environment.Exit(0);
                   }
               }
               MessageBox.Show("The Database has been created and SQL Server setting has been saved successfully..." + "\r\n" + "Application will be closed,Please start it again", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
               System.Environment.Exit(0);
          }
           catch (Exception ex)
          {
               MessageBox.Show("Unable to connect to sql server" + "\r\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
           finally
           {
              if ((con.State == ConnectionState.Open))
              {
                  con.Close();
               }
           }
       }
       void CreateDB()
       {
           try
           {
               con = new SqlConnection(@"Data source= '"+ cmbServerName.Text.ToString() + "';Initial Catalog=master;Integrated Security=True;");
               con.Open();
               string cb2 = "Select * from sysdatabases where name='DigitalCourtRollDB'";
               cmd = new SqlCommand(cb2);
               cmd.Connection = con;
               rdr = cmd.ExecuteReader();
               if (rdr.Read())
               {
                   con = new SqlConnection(@"Data source='" + cmbServerName.Text.ToString() + "';Initial Catalog=master;Integrated Security=True;");
                   con.Open();
                   string cb1 = "Drop Database DigitalCourtRollDB";
                   cmd = new SqlCommand(cb1);
                   cmd.Connection = con;
                   cmd.ExecuteNonQuery();
                   con.Close();
                   con = new SqlConnection(@"Data source='" + cmbServerName.Text.ToString() + "';Initial Catalog=master;Integrated Security=True;");
                   con.Open();
                   string cb = "Create Database DigitalCourtRollDB";
                   cmd = new SqlCommand(cb);
                   cmd.Connection = con;
                   cmd.ExecuteNonQuery();
                   con.Close();
                   using (StreamReader sr = new StreamReader(Application.StartupPath + "\\DBScript.sql"))
                   {
                       st = sr.ReadToEnd();
                       Server server = new Server(new ServerConnection(con));
                       server.ConnectionContext.ExecuteNonQuery(st);
                   }
               }
               else {
                   con = new SqlConnection(@"Data source='" + cmbServerName.Text.ToString() + "';Initial Catalog=master;Integrated Security=True;");
                   con.Open();
                   string cb3 = "Create Database DigitalCourtRollDB";
                   cmd = new SqlCommand(cb3);
                   cmd.Connection = con;
                   cmd.ExecuteNonQuery();
                   con.Close();
                   using (StreamReader sr = new StreamReader(Application.StartupPath + "\\DBScript.sql"))
                   {
                       st = sr.ReadToEnd();
                       Server server = new Server(new ServerConnection(con));
                       server.ConnectionContext.ExecuteNonQuery(st);
                   }
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
           finally
           {
               if ((con.State == ConnectionState.Open))
               {
                   con.Close();
               }
           }
       }


Has anybody experience this problem before?
I am using c# Visual Studio 2015. SQL server 2016.

Thank you very much in advance.

What I have tried:

I have tried on different SQL instance version, SQL 2016 and 2014.
I have tried to run the code on different PC, on virtual machine but still the same problem
Posted
Updated 6-Feb-17 22:44pm

1 solution

 
Share this answer
 
Comments
katela 7-Feb-17 8:09am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900