i use the access and find product but if there is many same product name how can ı list these
please write for access database in c# my normaly codes are below

baglan.Open();

            // Sorgu içinde urun isimli bir değişken kullanmışsın ancak değişkene değer atamamışsın. Sorguya null olarak giriyor. Alttaki satırı eklemen lazım
            urun = textBox1.Text;

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



            while (oku.Read())
            {

              textBox1.Text= oku["urun"].ToString();
              textBox2.Text = oku["model"].ToString();
                textBox3.Text = oku["adet"].ToString();
                textBox4.Text = oku["boyut"].ToString();
                textBox5.Text = oku["aciklama"].ToString();




            }


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

Dani AI

Generated

Main problem and quick diagnosis: the current code both streams rows with a DataReader (writing each row into the same set of textboxes) and also attempts an adapter/Fill operation, which leads to overwritten results and to Access file/connection conflicts (the "already exist didnt close file" symptom reported by ). correctly pointed out the reader/adapter conflict; 's DISTINCT suggestion would remove duplicate names and therefore is not what’s wanted when the goal is to list every matching row.

Recommended workflow and rules of thumb: pick one pattern — either stream rows with a DataReader (close it when done) or load results into a DataTable and data-bind the table to a grid control. For multi-row results, bind to a DataGridView (or ListView) and use the textboxes only to show details for the selected grid row. Use parameterized SQL (OleDb uses positional ? placeholders) to avoid SQL injection and to handle special characters. Always scope connections/commands/adapters with using blocks so connections and readers are closed automatically; this removes most "file not closed" errors. DISTINCT is only for when unique names are required; do not use it if every matching record must be shown.

Practical example (bind all matching rows to a grid, then show details when a row is selected):

string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\path\to\program.accdb";

using(var conn = new OleDbConnection(connString))
using(var cmd = new OleDbCommand("SELECT id, urun, model, sayi, boyut, aciklama FROM stok WHERE urun = ?", conn))
{
    cmd.Parameters.AddWithValue("?", comboBox1.Text);
    var dt = new DataTable();
    using(var da = new OleDbDataAdapter(cmd))
    {
        da.Fill(dt);          // Fill opens/closes the connection as needed
    }
    dataGridView1.DataSource = dt; // show all matching rows
}

Notes: OleDb parameter order is significant; do not call ExecuteReader and Fill simultaneously on the same open connection; close readers before filling a DataTable. If the Access file still appears locked, check for Microsoft Access being open, background processes, or previously leaked connections and inspect the exception details for the exact cause.

Recommended Answers

All 8 Replies

Use a datagrid, a repeater or possibly a literal control dependng on how you want to display the results (and whether this is for a web page or form application). Catch your results from the database in a dataTable via a dataAdapter and link that to your chosen output control.

OleDbDataAdapter da = new OleDbDataAdapter(veri);
DataTable dt = new DataTable();
da.Fill(dt); // load results into the dataTable

Then, depending on your chosen display method, use the dataTable to show the results on page.

thanks but where we fix this doesnt work

Why not? Explain your situation and we might be able to help you better.

the error message there is already exist didnt close file

these codes are below

using System;
using System.Data.OleDb;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace som
{
    public partial class ara : Form
    {
        public ara()
        {
            InitializeComponent();
        } OleDbConnection baglan = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\öztürk\Desktop\som\som\b.accdb");

        OleDbCommand veri; OleDbDataReader oku;

        string urun;



        private void button1_Click(object sender, EventArgs e)
        {

                baglan.Open();

                // Sorgu içinde urun isimli bir değişken kullanmışsın ancak değişkene değer atamamışsın. Sorguya null olarak giriyor. Alttaki satırı eklemen lazım
                urun = textBox1.Text;

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

                 OleDbDataAdapter da = new OleDbDataAdapter(veri); DataTable dt = new DataTable(); da.Fill(dt); // load results into the dataTableOleDbDataAdapter da = new OleDbDataAdapter(veri);







                try
                {




                while (oku.Read())
                {

                                        textBox1.Text = oku["urun"].ToString();
                                        textBox2.Text = oku["model"].ToString();
                                        textBox3.Text = oku["adet"].ToString();
                                        textBox4.Text = oku["boyut"].ToString();
                                        textBox5.Text = oku["aciklama"].ToString();




                }



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

            }
            catch
            {
                MessageBox.Show(":D");
            }


        }

        private void ara_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'bDataSet1.ogrenci' table. You can move, or remove it, as needed.
            this.ogrenciTableAdapter.Fill(this.bDataSet1.ogrenci);

        }




    }
}

use "distinct" on your query

He wants to list the products not select just one so DISTINCT won't help.
The problem is with your dataAdapter.

OleDbDataAdapter da = new OleDbDataAdapter(veri);
 DataTable dt = new DataTable();
 da.Fill(dt); // attempt to open an already open connection.

You have already opened the connection with baglan.Open(). da.Fill() then tries to do the same thing but can't because the connection is already open.
You need to decide if you want to use the reader or the dataTable, at the moment you have both and they're just getting in each others way.
Your while(oku.Read()) code makes no sense either. If more than one record is returned the textboxes will simply display the last one in the reader (all records will populate the textboxes but will be instantly overwritten by the next one).

what must ı do on this can you write all off codes for me please
ım written again

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace STOK
{
    public partial class araa : Form
    {
        public araa()
        {
            InitializeComponent();
        }
        OleDbConnection baglan = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\öztürk\Desktop\STOK1\program.accdb");

        OleDbCommand veri;
 OleDbDataReader oku;

        string urun;

        private void button1_Click(object sender, EventArgs e)
        {
             baglan.Open();

            // Sorgu içinde urun isimli bir değişken kullanmışsın ancak değişkene değer atamamışsın. Sorguya null olarak giriyor. Alttaki satırı eklemen lazım
             urun = comboBox1.Text;

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



            while (oku.Read())
            {

                comboBox1.Items.Add(oku["urun"].ToString());

              textBox2.Text = oku["model"].ToString();
                textBox3.Text = oku["sayi"].ToString();
                textBox4.Text = oku["boyut"].ToString();
                textBox5.Text = oku["aciklama"].ToString();




            }


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




    }

        private void araa_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("KANEPE");
            comboBox1.Items.Add("OTURMA GRUBU");
            comboBox1.Items.Add("YATAK ODASI");
            comboBox1.Items.Add("YEMEK ODASI");
            comboBox1.Items.Add("MASA");
            comboBox1.Items.Add("SANDALYE");
            comboBox1.Items.Add("YATAK");
            comboBox1.Items.Add("YASTIK");
            comboBox1.Items.Add("YORGAN");
            comboBox1.Items.Add("UYKU SETİ");
            comboBox1.Items.Add("HALI");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        }
    }
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.