0

I'm using a textbox to filter some money values but instead of getting me all the fields of gridview it refreshes the webpage. I want to only filter values from gridview where I can only filter values > 0. How can I fix my problem here?

protected void Button3_Click(object sender, EventArgs e)
        {
            //Filter data using textbox//
            string filter = "";
            string command = "SELECT * FROM NaoMatch WHERE Cliente is not null And";
            if (TextPesquisarDataMovimento.Text != "")
            {
                filter = filter + "  [Data Movimento ] LIKE '%" + TextPesquisarDataMovimento.Text + "%' AND";
                Debug.Write(filter);
            }
            if (TextPesquisarDataValor.Text != "")
            {
                filter = filter + " [Data Valor] LIKE '%" + TextPesquisarDataValor.Text + "%' AND";
                Debug.Write(filter);
            }
            if (TextPesquisarDescricao.Text != "")
            {
                filter = filter + " [Descricao] LIKE '%" + TextPesquisarDescricao.Text + "%' AND";
                Debug.Write(filter);
            }
            if (TextPesquisarCliente.Text != "")
            {
                filter = filter + " [Cliente] LIKE '%" + TextPesquisarCliente.Text + "%' AND";
                Debug.Write(filter);
            }
            if (textValorcIva.Text != "")
            {
                filter = filter + " Valor > 0 LIKE '%" + textValorcIva.Text + "%' AND";
                Debug.Write(filter);

            }
            if (filter.Length > 0)
            {
                Sqldata.DataSource = SqlDataSource1;
                string FinalFilter = filter.Remove(filter.Length - 3);
                SqlDataSource1.SelectCommand = command + FinalFilter;
                Sqldata.DataBind();
            }
            else
            {
                Sqldata.DataBind();
            }
        }
BPDESILVA
  • 2,040
  • 5
  • 15
  • 35
  • 3
    For security reasons you need to use parameterized SQL queries, see [Why do we always prefer using parameters in SQL statements?](https://stackoverflow.com/q/7505808). – dbc Jul 03 '19 at 08:25
  • I'm working doing stage for my school and I didnt start this program , it was half done by someone else , I'm new in this language so it´s hard for me to understand it and I must add new methods and contents and I can't change the previous code – Diogo Peixoto Jul 03 '19 at 08:30
  • @dbc is there any help you could give to me just to fix it ? – Diogo Peixoto Jul 03 '19 at 08:31
  • what do you check here " Valor > 0 LIKE '%" + textValorcIva.Text + "%' AND" – Mangesh Auti Jul 03 '19 at 08:48
  • Compare the value with textbox if it the same it appears in gridview – Diogo Peixoto Jul 03 '19 at 08:49
  • as per my understand valor is your column name and if you given textValorcIva.Text values as 0 then it should return recordes which value is greter than 0 correct?? – Mangesh Auti Jul 03 '19 at 08:50
  • @MangeshAuti yes but it is not giving me that values , i don't know why! – Diogo Peixoto Jul 03 '19 at 08:54
  • @MangeshAuti maybe my problem here is the way i set textboxvalues as 0 can u say to me the easiest way to put it – Diogo Peixoto Jul 03 '19 at 08:57
  • check my answer .if not working paste Debug.Write(filter); value – Mangesh Auti Jul 03 '19 at 08:58

2 Answers2

1

Change if block textValorcIva.Text to this (your query look like wrong)

if (textValorcIva.Text != "")
 {
     filter = filter + " Valor >"+ textValorcIva.Text + " AND";
     Debug.Write(filter);

 }
Mangesh Auti
  • 1,123
  • 1
  • 7
  • 12
1

Change

filter = filter + " Valor > 0 LIKE '%" + textValorcIva.Text + "%' AND";

to

filter = filter + " Valor > 0 AND Valor LIKE '%" + textValorcIva.Text + "%' AND";
Hans Kapitein
  • 174
  • 10