Error opening SqlConnection

2

This is the following I have a gridviewer that is connected to a Query with a  Table named Server Table.
When I want to add some things to the columns it gives the following error

  

Instance failure.

In this part of the code

connection.Open();

This is part of the code adds the values to colnas

  var nothing = Resources.Nothing;
        string domain2 = Domain + "\" + TxtBoxUpload.Text;
        if (TxtBoxUpload.Text == "" || TxtBoxUpload.Text == "Upload Name" || System.IO.Directory.Exists(domain2))
        {
            MessageBox.Show("Please Insert a valid name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                if (System.IO.Directory.Exists(Domain))
                {
                    if (System.IO.Directory.Exists(domain2))
                    {
                        MessageBox.Show("You have a project that you didnt give a name please give it a name and try again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                            System.IO.Directory.Move(folderBrowserDialog1.SelectedPath, domain2);
                            string connectionString = @"Data Source=VBSS019\ZONESOFTSQL;Initial Catalog=ServerDown;Integrated Security=True";
                            SqlConnection connection = new SqlConnection(connectionString);
                            cmd.CommandText = "INSERT INTO ServerTable(Icon, [Project Name], Directory) VALUES (@Icon, @[Project Name], @Directory)";
                            cmd.Parameters.Add("@Icon", nothing);
                            cmd.Parameters.Add("@[Project Name]", TxtBoxUpload.Text);
                            cmd.Parameters.Add("@Directory", domain2);
                            connection.Open(); //Esta é a parte do erro
                            cmd.ExecuteNonQuery();

                    }
                }
                else
                {
                    System.IO.Directory.CreateDirectory(Domain);
                    try
                    {
                        System.IO.Directory.Move(folderBrowserDialog1.SelectedPath, domain2);
                        Grid.Rows.Add(new object[] { nothing, "Edit", domain2 });
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Faild to move the file maybe it already exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {

            }

        }
    }

This part is what shows the gridviewer

        private void Form1_Load(object sender, EventArgs e)
    {

        Data();
    }

public void Data()
        {
            SqlConnection con = new SqlConnection("Data Source=VBSS019\ZONESOFTSQL;Initial Catalog=ServerDown;Integrated Security=True");
            sda = new SqlDataAdapter(@"SELECT Icon, [Project Name], Directory
                                       FROM ServerTable", con);
            dt = new DataTable();
            sda.Fill(dt);
            Grid.DataSource = dt;
        }
    
asked by anonymous 24.08.2016 / 16:42

1 answer

3
string connectionString = @"Data Source=VBSS019\ZONESOFTSQL;Initial Catalog=ServerDown;Integrated Security=True";

The prefix @ indicates that the string should be literally interpreted , it is not necessary to escape the backslash \ , use only \ .

string connectionString = @"Data Source=VBSS019\ZONESOFTSQL;Initial Catalog=ServerDown;Integrated Security=True";

In the Data method, you have the following line:

SqlConnection con = new SqlConnection("Data Source=VBSS019\ZONESOFTSQL;Initial Catalog=ServerDown;Integrated Security=True");

In this line escape the backslash is necessary because the string does not have the prefix @ to make it literal.

    
24.08.2016 / 16:53