I want to get the value of key with dynamic where clause in appSettings
portion in web.config
project (ASP.NET and C#) like this:
key="test" value="Select * from table where id=Textbox1.Text"
How can I achieve this?
I want to get the value of key with dynamic where clause in appSettings
portion in web.config
project (ASP.NET and C#) like this:
key="test" value="Select * from table where id=Textbox1.Text"
How can I achieve this?
You can do it like this:
// Get sql query and add where clause to it.
string sqlString = System.Configuration.ConfigurationManager.AppSettings["test"] + " where id=@id";
// Execute sqlString
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlParameter param = new SqlParameter();
param.ParameterName = "@id";
param.Value = Textbox1.Text;
cmd.Parameters.Add(param);
SqlDataReader reader;
cmd.CommandText = sqlString;
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
sqlConnection1.Close();
Edit
C# for prevent SQL injection, stop executing commands that do this. You should use SqlParameter
.