I have a column named ast_code
which I have already retrieved to a string
variable. I'm trying to run a select
query using a where
clause based on the string value that I stored in that variable.
Here's the code I tried:
public void grid()
{
datatable dt = new datatable();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand();
try
{
command.Connection = myConnection;
command.CommandText = "SELECT code, name from table.menu where code <> '"+ ast_code + "'";
adapter.SelectCommand = command;
adapter.Fill(ds);
adapter.Fill(dt);
myConnection.Open();
}
catch(Exception ex)
{
MessageBox.Show("error" + ex);
}
myConnection.Close();
gridControl1.DataSource = dt;
}
When I ran the query, it returned no results unless the value in the string variable only contained one value (e.g. 0110300
).
I then tried to transform the contents of the variable:
ast_code = a.Replace(';',',').Replace(' ','\'');
but it returned an error due a missing '
. Don't mind the a
; it was a parsing variable value. I have already tried to store them to a list
but that isn't working properly, either.
What I need to do is generate a where
clause that can handle multiple values.
UPDATE
iam using @rbr94 suggestion ast_code = a.Replace("; ", "', '");
command.CommandText = "SELECT code, name from table.menu where code not in '"+ ast_code + "'";
it works when iam using the top string value
, but for the second row `string value give me an error