ı created a project in c # and it loaded to data in access but searching button doesnt work every time find the last loaded please find the problem

baglan.Open();
            veri = new OleDbCommand("select urun,model,adet,boyut,aciklama from ogrenci where 'urun='" + urun + ";", baglan);
            oku = veri.ExecuteReader();

            while (oku.Read())
            {
                 comboBox1.Items.Add(oku["urun"].ToString());
                textBox1.Text = oku["model"].ToString();
                textBox2.Text = oku["adet"].ToString();
                comboBox1.Items.Add(oku["boyut"].ToString());
                textBox3.Text = oku["aciklama"].ToString();
               

            }


            oku.Close();
            baglan.Close();

Dani AI

Generated

Visible issues in the snippet posted by explain the symptoms. The WHERE clause is written so the column name is treated as a string literal and the search value is not correctly quoted, which makes the SQL either invalid or not filter rows as intended. In the UI loop you assign TextBox values on every reader iteration, so after the loop finishes the controls show the last row read. There is also an apparent mistake adding two different fields into the same ComboBox, which will confuse selection logic.

Fixes to apply (practical, low-risk steps)

  • Stop concatenating values into SQL. Use parameterized commands so you do not have to worry about quoting or embedded characters and to avoid SQL injection. For OleDb you use positional parameters (placeholders) rather than named parameters.
  • Decide whether you want the first match or a list of matches. If only one record is expected read once (for example if(reader.Read())) or break after the first assignment. If multiple matches are expected, populate a list or bind a DataTable to the ComboBox and let the user pick which record to view. Always clear the ComboBox before adding new items.
  • Use C# using blocks to ensure the connection and reader are closed even on error.

Example pattern (generic placeholders)

using(var conn = new OleDbConnection(connString))
using(var cmd = new OleDbCommand("SELECT ColA, ColB FROM TableName WHERE KeyCol = ?", conn))
{
  cmd.Parameters.AddWithValue("?", searchValue);
  conn.Open();
  using(var rdr = cmd.ExecuteReader())
  {
    comboBox.Items.Clear();
    if (rdr.Read())
    {
      comboBox.Items.Add(rdr.GetString(0));
      textBoxA.Text = rdr.GetString(1);
    }
  }
}

Extra tips and references

  • If the filter column is numeric, still use parameters but do not wrap the value in quotes.
  • Test the SQL string directly in the database UI to verify behavior before wiring UI code.
  • See Microsoft guidance on ADO.NET commands and parameters and on the C# using statement for disposal patterns: Commands and Parameters, OleDbCommand.Parameters, using statement.

heres a sample. if you like to search in your databse i prefer to use "LIKE"... eg select * from tablenme where column_name LIKE "variable"..

CN = new SqlConnection(concString);
                
                String qry = "update BookMaster set Cat_code=" + int.Parse(Cat) + " Title='" + title + "' Publisher='" + Pub + "' No_Of_Pages=" + int.Parse(Page) + " No_Of_Cops=" + int.Parse(Copes) + " Date='" + dt.ToString() + "' price=" + int.Parse(Price) + " year_of_pub=" + int.Parse(Year) + " where Book_ID='" + GetBookId() + "'";
 
                SqlCommand com = new SqlCommand(qry, CN);
CN.Open();/// open cnnction string here
                int executed = com.ExecuteNonQuery();

thank but ı found the error

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.