I have a code behind method that uses SqlDataSource object to retrieve data from database, then is used as a data source for my grid view to display the records. I need to conditionally add another condition to my WHERE clause of my SelectCommand query, and need to pass the data to the query through parameters. How do I do this?
SqlDataSource sds = new SqlDataSource();
sds.SelectCommand = "Select CustomerName, LoanNumber, LoanDate FROM Loan WHERE IsActive = 1 ";
if (filterRecord != "All") { // DDL filter on page, all records by default
sds.SelectCommand += "AND LoanType = ??"; //this is where I need to parameterize my query
}
I thought something like
if (filterRecord != "All") {
sds.SelectCommand += "AND LoanType = @LoanType";
sds.SelectParameters.Add("@LoanType", "Mortgage");
}
But that does not appear to work.
But that does not appear to work.What doesn't work? Is there an exception?LoanType = ??or theLoanType = @LoanType?sds.SelectParameters.AddWithValue?