locked
How to shorten the query code. RRS feed

  • Question

  • User-1872583635 posted

    Hi All,

    Can you advise me how can I shorten my query code below.

    protected void Button1_Click(object sender, EventArgs e)
            {
    
                count = 0;
                con.Open();
                OleDbCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "SELECT [A],[B],[B],[C],[D] FROM [tableE] where A='" + TextBox1.Text + "' and B='" + DropDownList3.Text + "'or A='" + TextBox2.Text + "'and B='" + DropDownList3.Text + "'or A='" + TextBox3.Text + "'and B='" + DropDownList3.Text + "'or A='" + TextBox4.Text + "'and B='" + DropDownList3.Text + "'or A='" + TextBox5.Text + "'and B='" + DropDownList3.Text + "'or A='" + TextBox6.Text + "'and B='" + DropDownList3.Text + "'or A='" + TextBox7.Text + "'and B='" + DropDownList3.Text + "'or A='" + TextBox8.Text + "'and B='" + DropDownList3.Text + "'or A='" + TextBox9.Text + "''and B='" + DropDownList3.Text + "'or A='" + TextBox10.Text + "'and B='" + DropDownList3.Text + "'";
                cmd.ExecuteNonQuery();
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                da.Fill(dt);
                count = Convert.ToInt32(dt.Rows.Count.ToString());
                GridView1.DataSource = dt;
                GridView1.DataBind();
    
                con.Close();
    
            }

    I want to search multiple data at TableE where there is 10 input of A and each data must have status I set in B or not the search is error.

    or can I do this in if else condition?

    I mean

    if Data B is equal to Status, search result okay. Else result NG.(I know the concept but don't know how to do it)

    Thank you in advance

    Thursday, October 26, 2017 5:41 AM

Answers

  • User-335504541 posted

    Hi yura.s,

    Please try to use the following code:

    cmd.CommandType = CommandType.Text;
                            cmd.CommandText = "SELECT [A],[B],[B],[C],[D] FROM [tableE] where B=@DropDownList3 and (A=@t1 or A=@t2 or A=@t3 or A=@t4 or A=@t5 or A=@t6 or A=@t7 or A=@t8 or A=@t9 or A=@t10)";
                            cmd.Parameters.AddWithValue("@DropDownList3", DropDownList3.Text);
                            cmd.Parameters.AddWithValue("@t1", TextBox1.Text);
                            cmd.Parameters.AddWithValue("@t2", TextBox2.Text);
                            cmd.Parameters.AddWithValue("@t3", TextBox3.Text);
                            cmd.Parameters.AddWithValue("@t4", TextBox4.Text);
                            cmd.Parameters.AddWithValue("@t5", TextBox5.Text);
                            cmd.Parameters.AddWithValue("@t6", TextBox6.Text);
                            cmd.Parameters.AddWithValue("@t7", TextBox7.Text);
                            cmd.Parameters.AddWithValue("@t8", TextBox8.Text);
                            cmd.Parameters.AddWithValue("@t9", TextBox9.Text);
                            cmd.Parameters.AddWithValue("@t10", TextBox10.Text);

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 27, 2017 6:51 AM

All replies

  • User-335504541 posted

    Hi yura.s,

    Please try to use the following code:

    cmd.CommandType = CommandType.Text;
                            cmd.CommandText = "SELECT [A],[B],[B],[C],[D] FROM [tableE] where B=@DropDownList3 and (A=@t1 or A=@t2 or A=@t3 or A=@t4 or A=@t5 or A=@t6 or A=@t7 or A=@t8 or A=@t9 or A=@t10)";
                            cmd.Parameters.AddWithValue("@DropDownList3", DropDownList3.Text);
                            cmd.Parameters.AddWithValue("@t1", TextBox1.Text);
                            cmd.Parameters.AddWithValue("@t2", TextBox2.Text);
                            cmd.Parameters.AddWithValue("@t3", TextBox3.Text);
                            cmd.Parameters.AddWithValue("@t4", TextBox4.Text);
                            cmd.Parameters.AddWithValue("@t5", TextBox5.Text);
                            cmd.Parameters.AddWithValue("@t6", TextBox6.Text);
                            cmd.Parameters.AddWithValue("@t7", TextBox7.Text);
                            cmd.Parameters.AddWithValue("@t8", TextBox8.Text);
                            cmd.Parameters.AddWithValue("@t9", TextBox9.Text);
                            cmd.Parameters.AddWithValue("@t10", TextBox10.Text);

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 27, 2017 6:51 AM
  • User-1872583635 posted

    hi Billy San,

    Thank you so much

    Wednesday, November 8, 2017 12:57 AM