Click here to Skip to main content
15,886,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everyone ,dear i have question if you could please help me :)

i want to retrieve data from table with multiple filter that i chose from listbox
i want to use this query in my application
SQL
select * from product where firstname='sam' or firstname ='reachel'

and the name i select is loaded to listbox ,
i tried somehow but wasn't correct
and this is my code of c#
C#
List<string> selecteditmes = new List<string>();
            foreach (int i in listBox1.SelectedIndices)
            {
                selecteditmes.Add(listBox1.Items[i].ToString());

 string smdd = "select * from productvw  where Firstname ='"+ i +"'or Firstname="+i+"";

                SqlDataAdapter sqld = new SqlDataAdapter(smdd, cn);
                cn.Open();

                DataTable dt = new DataTable();
                sqld.Fill(dt);
                dataGridView1.DataSource = dt;
                //dataGridView1.Refresh();
                cn.Close();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("select Firstname from productvw ", cn);
            cn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                string smd = (string)dr["Firstname"].ToString();
                listBox1.Items.Add(smd);
            }
            cn.Close();
        }
Posted
Comments
[no name] 23-Sep-15 10:18am    
Here you are storing the selected value in a list. Once all the values are stored in the list you can able to get one by one values from there using a loop and compose your query inside that loop. Try to use this concept.
Richard Deeming 23-Sep-15 10:28am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
gggustafson 23-Sep-15 12:21pm    
I believe that sisir is correct

1 solution

You could try it like this:

C#
int selectedCounter = listBox1.SelectedItems.Count;

//the next few rows are for pre-constructing the Connection string WITH PARAMETERS
String sqlQuery = "select * from productvw where ";
for (int i = 0; i < selectedCounter; i++)
{
    if(i != 0)
    {
        sqlQuery += " or ";
    }
    sqlQuery += "Firstname = @Fn" + i.toString();
}

using (SqlConnection connection = new SqlConnection(connectionString))
{
    using (SqlCommand command = new SqlCommand(sqlQuery, Connection))
    {
        for (int i = 0; i < selectedCounter; i++)
        {
            command.Parameters.AddWithValue("@Fn"+i.toString();listBox1.SelectedItem[i].Text);
            Connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (Reader.Read())
            {
                //do stuff with the reader here
            }
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900