Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a table with products i am seeking a product according to product name, type and size, how to write a SQL statement for that?

C#
try
          {

              string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\WStoreSystem\\WStoreSystem\\WStoreSystem\\bin\\Debug\\StoreSys.mdb";
              OleDbConnection con = new OleDbConnection(str);
              con.Open();
              OleDbCommand comm = new OleDbCommand();
              comm.Connection = con;
              string qu = "SELECT StockID FROM Stock where ProName '" + comboBox1.Text + "'" || ProType like '" + comboBox2.Text + "'" || ProSize like '" + comboBox3.Text + "'";

             comm.CommandText = qu;
              OleDbDataReader reder = comm.ExecuteReader();
              while (reder.Read())
              {
                  txtStockID.Text =(reder["StockID"].ToString());
              }
              con.Close();
          }
          catch (Exception ex)
          {
              MessageBox.Show("Error " + ex);
          }
      }
Posted
Comments
virusstorm 19-May-15 14:17pm    
We need to know what your table looks like to help you write the SQL needed.
MeftahDAKHEEL 20-May-15 12:49pm    
1 1 Bridgestone TC10 B 175/70R13
2 1 Bridgestone MY02 B 185/65R14
3 1 Bridgestone TC10 B 185/70R14
4 1 Bridgestone AR20 B 215/60R16
5 1 Bridgestone AR20 B 225/55R16
6 1 Bridgestone TG90 B 235/60R16
7 1 Bridgestone TC10 B185/65R15

1 solution

Your code is vulnerable to SQL Injection[^].

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

Rather than specifying the full path to the database file, use |DataDirectory| in your connection string. This will automatically point to the application directory for a Windows application, and the App_Data directory for a web application.

Wrap disposable objects in a using block to ensure that their resources are always cleaned up.

There's no need to use ExecuteReader when you're only reading a single value. Use ExecuteScalar instead.

And SQL uses the operators AND and OR, not the C-style && and ||.

C#
const string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|StoreSys.mdb";
const string Query = "SELECT TOP 1 StockID FROM Stock WHERE ProName = ? Or ProType = ? Or ProSize = ?";

using (var connection = new OleDbConnection(ConnectionString))
using (var command = new OleDbCommand(Query, connection))
{
    // OleDb doesn't use named parameters, so only the order matters here:
    command.Parameters.AddWithValue("p0", comboBox1.Text);
    command.Parameters.AddWithValue("p1", comboBox2.Text);
    command.Parameters.AddWithValue("p2", comboBox3.Text);

    connection.Open();
    txtStockID.Text = Convert.ToString(command.ExecuteScalar());
}


Also, do yourself a favour and give your controls meaningful names. Accepting the default name assigned by the designer will only cause confusion later.



EDIT:
Based on the comments, it sounds like you want to match all of the selected parameters, rather than matching any of the selected parameters. If so, change the code to:

C#
const string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|StoreSys.mdb";
const string Query = "SELECT TOP 1 StockID FROM Stock WHERE (ProName = ? Or ? = '') And (ProType = ? Or ? = '') And (ProSize = ? Or ? = '')";

using (var connection = new OleDbConnection(ConnectionString))
using (var command = new OleDbCommand(Query, connection))
{
    // OleDb doesn't use named parameters, so only the order matters here:
    command.Parameters.AddWithValue("p0", comboBox1.Text);
    command.Parameters.AddWithValue("p1", comboBox1.Text);
    command.Parameters.AddWithValue("p2", comboBox2.Text);
    command.Parameters.AddWithValue("p3", comboBox2.Text);
    command.Parameters.AddWithValue("p4", comboBox3.Text);
    command.Parameters.AddWithValue("p5", comboBox3.Text);

    connection.Open();
    txtStockID.Text = Convert.ToString(command.ExecuteScalar());
}

(You have to add each parameter twice, since OleDb doesn't used named parameters.)
 
Share this answer
 
v3
Comments
MeftahDAKHEEL 19-May-15 15:39pm    
i really appreciate you kind help, thanks a lot, also your advice about the controls...
MeftahDAKHEEL 20-May-15 12:39pm    
const string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|StoreSys.mdb";
const string Query = "SELECT TOP 1 StockID FROM Store WHERE ProductID = ? OR ProName = ? OR ProType = ? OR ProSize = ?";

using (var connection = new OleDbConnection(ConnectionString))
using (var command = new OleDbCommand(Query, connection))
{
// OleDb doesn't use named parameters, so only the order matters here:
command.Parameters.AddWithValue("p0", textProdID.Text);
command.Parameters.AddWithValue("p1", combProdName.Text);
command.Parameters.AddWithValue("p2", combProdSize.Text);
command.Parameters.AddWithValue("p3", combProdType.Text);

connection.Open();
txtStockID.Text = Convert.ToString(command.ExecuteScalar());
}


the result is the first StockID, and is not always the same
MeftahDAKHEEL 20-May-15 12:49pm    
for example:
StockID ProductID ProName ProType ProSize
1 1 Bridgestone TC10 B 175/70R13
2 1 Bridgestone MY02 B 185/65R14
3 1 Bridgestone TC10 B 185/70R14
4 1 Bridgestone AR20 B 215/60R16
5 1 Bridgestone AR20 B 225/55R16
6 1 Bridgestone TG90 B 235/60R16
7 1 Bridgestone TC10 B185/65R15
8 4 GoodYear GY101 GY 205/60R16


As per the data in Table (Store), but the result is always StockID = 1, the first catch, until i change product name from comboBox "combProdName", then it change with the first StockID related to the chosen productName.
Richard Deeming 20-May-15 12:50pm    
You're displaying the selected ID in a TextBox. A TextBox can only display a single value.
MeftahDAKHEEL 20-May-15 13:14pm    
It's showing only the first Stock ID, even if i select the row 6

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