The parameterized query to the SQL-Server database does not work. For the current TreeView node, I pull the data from the related table.
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { string cur = treeView1.SelectedNode.Text; string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TestBase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; string commandText = "SELECT Employees.Name, Employees.Experience FROM Departments INNER JOIN Employees ON Departments.id=Employees.Department WHERE Departments.Name=" + cur + ";"; SqlConnection conn = new SqlConnection(); conn.ConnectionString = connectionString; conn.Open(); SqlDataAdapter dataAdapter = new SqlDataAdapter(commandText, conn); DataSet ds = new DataSet(); dataAdapter.Fill(ds); dataGridView1.DataSource = ds.Tables[0].DefaultView; }
If I remove WHERE in the query, then everything works, but all the records are displayed, but I need only certain ones. On the line dataAdapter.Fill (ds), a SQLException exception is thrown: Invalid column name 'Accounting'.