I need to populate datagridview2 after selecting a value in datagridview1. I do this by pulling key values from dgv1 and adding them to a SQL script that is fed to an adapter that fills a dataset which in turn shows in dgv2.
I have code that works 99.9% of the time. However, there are occasional values that get passed that break the code. Therefore, I want to improve my SQL query creation by adding parameters rather just dumping the values directly into the script. The added security is a nice side effect of this task.
I believe I am almost there, I am just unable to get the parameters to pass into my script.
Here is my current code which works fine, except when a bad value gets passed as one of the varValues...
string sql = @"select distinct Value1, Value2, Value3 from vSourceView where value1 = " + varValue1 + " and value2 = " + varValue2;
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
DataSet da = new DataSet();
da.Tables.Add(sql);
adapter.Fill(da, sql);
dataGridView2.DataSource = da;
dataGridView2.DataMember = sql;
Here is my "new" code that is not working...
string sql = "select distinct Value1, Value2, Value3 from vSourceView where Value1 = @value1 and Value2 = @value2";
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
adapter.SelectCommand.Parameters.AddWithValue("@value1", varValue1);
adapter.SelectCommand.Parameters.AddWithValue("@value2", varValue2);
//added for testing
textBox1.Text = sql;
DataSet da = new DataSet();
da.Tables.Add(sql);
adapter.Fill(da, sql);
dataGridView2.DataSource = da;
dataGridView2.DataMember = sql;
The parameters are not passing into the script. When I run the app, textBox1 shows "select distinct Value1, Value2, Value3 from vSourceView where Value1 = @value1 and Value2 = @value2" and dgv2 is not filled.
What am I missing?
Thanks in advance.
PS
The actual value that is "breaking" my current code is "VBA.MEDICAL". I suspect that "VBA." is a reserved string...