Error opening Sqlconnection

Asked

Viewed 899 times

2

The following I have a gridviewer that is connected to a Query with a Table called Server Table.
I want to add some things to the columns gives the following error

Instance failure.

In this part of the code

connection.Open();

This is part of the code adds the values to the 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 that 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;
        }

1 answer

3


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

The prefix @ indicates that the string must 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 method Data, you have the following line:

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

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

  • thanks but now I’m having the second mistake ExecuteNonQuery: A propriedade Connection não foi inicializada.

  • @Pedro You have defined the connection of Sqlcommand? cmd.Connection = connection before connection.Open.

  • I have now appeared your error There is no mapping of the System.Drawing.Icon object type to a native type of known managed vendor. this here the code

  • 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);
cmd.Connection = connection;
connection.Open();
cmd.ExecuteNonQuery();

  • @Pedro If you can, open a question again with this doubt, because this error is different from the main error of the question!

  • 1

    If I’ve figured out the mistake is the about the icon I’ll try to fix if I can’t tomorrow I open a new question

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.