Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9038577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:31:16+00:00 2026-06-16T09:31:16+00:00

I have below code in code behind for btnShowReport_Click : SqlDataSource1.SelectParameters.Clear(); SqlDataSource1.SelectParameters.Add(username, txtUsername.Text); SqlDataSource1.SelectParameters.Add(printer,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:31:18+00:00Added an answer on June 16, 2026 at 9:31 am

    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:

      protected void Page_Load(object sender, EventArgs e)
    {
        Dictionary<string, string> parameters = new Dictionary<string, string>();
        SqlDataSource1.SelectParameters.Clear();
    
        string name = txtName.Text;
        string surname = txtSurname.Text;
    
        if (name.Length > 0)
        {
            SqlDataSource1.SelectParameters.Add("Name", txtName.Text);
            parameters.Add("Name", name);
        }
    
        if (surname.Length > 0)
        {
            SqlDataSource1.SelectParameters.Add("Surname", txtSurname.Text);
            parameters.Add("Surname", surname);
        }
    
        SqlDataSource1.SelectCommand = GetSelectQuery("Customers", parameters);
        //Bind SqlDataSource1 to gridview etc...
    }
    
    private string GetSelectQuery(string table, Dictionary<string, string> parameters)
    {
        var query = new System.Text.StringBuilder();
        query.Append(String.Format("SELECT * FROM {0}", table));
    
        for (int i = 0; i < parameters.Count; i++)
        {
            var param = parameters.ElementAt(i);
            if (i == 0)
                //Add the first parameter
                query.Append(String.Format(" WHERE {0}='{1}' ",param.Key, param.Value));
            else
                query.Append(String.Format(" AND {0}='{1}'", param.Key, param.Value));
        }
        return query.ToString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have below code behind in c# if (Session[cmpDictionaryTitle]!= null) { downloadLinks.Text += @<li><a
i am using a JavaScript and it have the below code: <script type=text/javascript> var
I have the below code: CODE BEHIND: using System; using System.Collections.Generic; using System.Linq; using
I have the below code: <asp:Content ID=HeadContent runat=server ContentPlaceHolderID=HeadContent> <script type=text/javascript> function SetText(id) {
In my code behind I have the code below to execute an insert command.
I have the below code-behind: public partial class MainWindow : Window { public MainWindow()
I have below code: <a href=# id=@item.Id name=vote ><img src=/Content/images/021.png style=float:left alt= /></a> which
I have below code: class Program { static void Main(string[] args) { Task[] tasks
I have below code in html. <li class=selected runat=server id=lihome><a href=/ISS/home.aspx title=Home><span>Home</span></a></li> Now I
I have below code to insert a style into DOM (there is a use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.