Search code examples
sqlsqlitereportviewer

How to write an sqlite query for the following scenario?


Here is my query for wpf form

Query = "select Cust_Id,Card_Number,Clients_Title,Address_Current,Phone_Number,Mobile_Number from Customer_New Where 1=1";
try
{
    if (txt_title.Text != "")
        Query += " and Clients_Title Like '%" + txt_title.Text + "%'";
    if (txt_address.Text != "")
        Query += " and Address_Current Like '%" + txt_address.Text + "%'";
    if (txt_phone.Text != "")
        Query += " and Phone_Number Like '%" + txt_phone.Text + "%'";
    if (txt_mobile.Text != "")
        Query += " and Mobile_Number Like '%" + txt_mobile.Text + "%'";
    if (cbo_location.Text != "")
        Query += " and AreaLocation Like '%" + cbo_location.Text + "%'";
}

catch { }

I want to report viewer query data like my wpf form . Here is the query that i am trying in report viewer

SELECT        Cust_Id, Clients_Title, Card_Number, Key_Person, Address_Current, Phone_Number, Mobile_Number, AreaLocation
FROM            Customer_New
WHERE        (Clients_Title = @Clients_Title) OR
                         (Address_Current = @Address_Current) OR
                         (Phone_Number = @Phone_Number) OR
                         (Mobile_Number = @Mobile_Number) OR
                         (AreaLocation = @AreaLocation) 

Can anyone tell me query for report viewer like wpf form . Note :-

  1. I cant use cant use C# controls in report viewer.Here in report viewer i can only use sql
  2. What is needed in report viewers query is:

    • When are all string of where clause are null then my report viewer
      should select query is:

      Query = "select Cust_Id,Card_Number,Clients_Title,Address_Current,Phone_Number,Mobile_Number from Customer_New ";

    • When any two string of where clause are not matching corresponding row db then nothing

    will be displayed

    • Lastly, selection will be made if just one condition of where clause is provided

Solution

  • SELECT        Cust_Id, Clients_Title, Card_Number, Key_Person, Address_Current, Phone_Number, Mobile_Number, AreaLocation
    FROM            Customer_New
        where        (CASE
            WHEN @Clients_Title != ''   THEN Clients_Title=@Clients_Title
          ELSE
             NULL IS NULL
          END)
          AND(CASE
            WHEN @Address_Current != '' THEN Address_Current =@Address_Current
          ELSE
             NULL IS NULL
          END)
                  AND(CASE
            WHEN @Phone_Number != '' THEN Phone_Number=@Phone_Number
          ELSE
             NULL IS NULL
          END)
          AND(CASE
            WHEN @Mobile_Number != '' THEN Mobile_Number=@Mobile_Number
          ELSE
             NULL IS NULL
          END)
          AND(CASE
            WHEN @AreaLocation != '' THEN AreaLocation =@AreaLocation
          ELSE
             NULL IS NULL
          END)