I have below code in code behind for btnShowReport_Click:
SqlDataSource1.SelectParameters.Clear();
SqlDataSource1.SelectParameters.Add("username", txtUsername.Text);
SqlDataSource1.SelectParameters.Add("printer", ddlPrinter.Text);
SqlDataSource1.SelectParameters.Add("to", Date.convertDateSolar2Gregorian(txtDateFrom_datepicker.Text));
SqlDataSource1.SelectParameters.Add("from", Date.convertDateSolar2Gregorian(txtDateTo_datepicker.Text));
SqlDataSource1.SelectCommand = "SELECT * FROM JobLog WHERE UserName=@username and PrinterName=@printer and TimeSubmitted between @to and @from";
I want when user doesn’t fill some text boxes, in the query, the criteria of the textbox was ignored in query.
I hope you understand what I want.
You can dynamically build the SQL query using a StringBuilder, however this makes your code prone to SQL Injection attacks and you should rather look at creating a stored procedure in SQL Server using Dynamic SQL
Here’s the StringBuilder example though if you absolutely have to build the query in code: