Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button2_Click(object sender, EventArgs e)
        {
            connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Login App\SmartShop.accdb");
            command = connection.CreateCommand();

            txtbox2.Clear();

            try
            {
                connection.Open();
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = connection;
                string query = "Select * from Items WHERE ItemID = '"+strarray+"'";
                cmd.CommandText = query;

                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                DataTable dt = new DataTable();

                da.Fill(dt);

                connection.Close();

                dataGridView1.DataSource = dt;


            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex);

            }     
            
        }
Posted
Updated 7-May-15 0:39am
v2
Comments
sashje- 7-May-15 6:37am    
Given that strArray is of type string[], you need to add a clause foreach of the items.. so you can decide if you want to use OR or AND between the items in the array...
Dakota335 7-May-15 7:05am    
It says syntax error/ missing operator @ ItemID IN(,123,....)
Richard Deeming 7-May-15 8:09am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Change
SQL
string query = "Select * from Items WHERE ItemID = '"+strarray+"'";
to
SQL
string query = "Select * from Items WHERE ItemID IN (" + String.Join(",", strarray) + ")";


[Edit] - If the contents of strarray are strings then you need to surround them with single quotes e.g.
C#
string query = "Select * from Items WHERE ItemID IN (" + String.Join(",", Array.ConvertAll(strarray, z => "'" + z + "'")) + ")";


I note that your comment said
Quote:
syntax error/ missing operator @ ItemID IN(,123,900C,....)
That initial comma between the brackets is happening because you have not populated the first element in the array - remember arrays are zero-based so the first element is in [0], the second is in [1], etc. Take care when you are populating the array.
 
Share this answer
 
v2
Comments
Dakota335 7-May-15 6:52am    
This strarray is actually an array created by the user input through a textbox. There is always an error when I try to display the results. :(
CHill60 7-May-15 7:08am    
What error? Are you validating the text box input? - I presume ItemID is integer and not varchar.
Dakota335 7-May-15 7:11am    
Thanks for your help.
Nah, ItemID is a string like "099C12". The user inputs the id in a textbox at button clicks. I store that in an array. And therefore I want to display the rows matching the IDs in the array.
t says syntax error/ missing operator @ ItemID IN(,123,900C,....)
CHill60 7-May-15 7:34am    
I've updated my solution
Dakota335 7-May-15 7:49am    
Omg! That really solved my problem. Thanks a ton :)
Finally my project can progress now
Further to Chill60's answer, you need to use a parameterized query to avoid SQL Injection[^].

It's not quite as simple with an array parameter, but it's still fairly easy:
C#
private void button2_Click(object sender, EventArgs e)
{
    using (var connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Login App\SmartShop.accdb"))
    using (var command = connection.CreateCommand())
    {
        command.CommandType = CommandType.Text;

        var sb = new System.Text.StringBuilder("SELECT * FROM Items WHERE ItemID IN (");
        for (int index = 0; index < strarray.Length; index++)
        {
            if (index != 0) sb.Append(',');

            // OleDb doesn't use named parameters, so only the order matters here:
            command.Parameters.AddWithValue("@p" + index, strarray[index]);
            sb.Append('?');
        }
        sb.Append(')');

        command.CommandText = sb.ToString();

        var da = new OleDbDataAdapter(cmd);
        var dt = new DataTable();
        da.Fill(dt);

        dataGridView1.DataSource = dt;
    }
}
 
Share this answer
 
Comments
CHill60 8-May-15 3:39am    
:thumbsup: Have a 5!
Try using a IN clause:
SQL
SELECT * FROM Items WHERE ItemId IN (1, 2, 3, 4, 666, 777)
 
Share this answer
 

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