hi
i m making a c# app which reads data from ms accesss & display it in textbox i have tried the following method but that didn't worked suggest me smthing or correct my code pls

 myconn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\nature bliss\Desktop\Stock\section1.accdb;Persist Security Info=False");
                        try
                        {
                            OleDbCommand cmd = new OleDbCommand();
                            cmd.Connection = myconn;
                            cmd.CommandText = "select * from section1";

                            myconn.Open();
                            //MessageBox.Show("Sucessfully Connected to " + myconn.DataSource.ToString());
                            var reader = cmd.ExecuteReader();
                            while (reader.Read())
                            {
                                textBox1.Text = reader["Name"].ToString();
                            }


                            myconn.Close();


                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(""+ex);
                        }
                        finally
                        {
                            myconn.Close();
                        }

Dani AI

Generated

For : a few focused checks and fixes based on the code shown and 's comment. Common culprits are provider/bitness mismatches, an incorrect path/permissions for the .accdb file, a query that returns no rows, or using a loop that repeatedly overwrites the TextBox so only the last value is visible. Also avoid relying on SELECT * and be careful if the column name is a reserved word — bracket it as [Name].

A simple, safe pattern to get a single value (first row) and avoid manual Close/Dispose is to use using and ExecuteScalar. This returns one value or null and avoids the overwrite issue:

using (var conn = new OleDbConnection(connString))
{
    conn.Open();
    using (var cmd = new OleDbCommand("SELECT TOP 1 [Name] FROM section1", conn))
    {
        var val = cmd.ExecuteScalar();
        textBox1.Text = val != null ? val.ToString() : String.Empty;
    }
}

If multiple rows are intended, build the text and set the TextBox once (set textBox1.Multiline = true first). This avoids repeatedly assigning inside the loop and handles nulls safely:

var sb = new StringBuilder();
using (var conn = new OleDbConnection(connString))
{
    conn.Open();
    using (var cmd = new OleDbCommand("SELECT [Name] FROM section1", conn))
    using (var rdr = cmd.ExecuteReader())
    {
        while (rdr.Read())
        {
            if (!rdr.IsDBNull(0)) sb.AppendLine(rdr.GetString(0));
        }
    }
}
textBox1.Multiline = true;
textBox1.Text = sb.ToString();

Notes and troubleshooting: if the ACE provider error appears, match the project Platform target (x86 vs x64) to the installed Access Engine or install the correct Access Database Engine. Use reader.HasRows or ExecuteScalar to confirm the query returns data. Wrap column names in brackets when in doubt and check rdr.IsDBNull before reading.

How did it not work? Need a bit more information to go on.

The only suggestion I can make now is that your query isn't returning any rows.

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.