How do I go about showing data in a database in a table or rich text box, so far I have this:

public static void showDogs()
        {
            OleDbConnection myConnection = GetConnection();

            string command2 = "SELECT * FROM dogs";
            OleDbCommand myCmd2 = new OleDbCommand(command2, myConnection);

            try
            {
                myConnection.Open();
                OleDbDataReader myReader = myCmd2.ExecuteReader();
            }
            catch (Exception)
            {
                Console.WriteLine("Exception in DBHandler");
            }
            finally
            {
                myConnection.Close();
            }

and in my form I have:

private void showDogsBtn_Click(object sender, EventArgs e)
        {
            DBConnection.showDogs();
        }

I don't know how to link the method to my rich text box to get it to print the values in the database.

Recommended Answers

All 3 Replies

Why don't you use a grid? You can load up the results of your query in to a DataTable and bind the grid to the database. Then you could hook up events to the grid so when the user clicks on a row in the grid it displays each of the fields in databound textboxes, rich text boxes, etc.

using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmOleDbGrid : Form
  {
    private DataTable dt;

    public frmOleDbGrid()
    {
      InitializeComponent();
    }

    private void BuildSql()
    {
      const string connStr = @"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Scott;Data Source=apex2006sql";
      const string query = "Select * From Experiment";
      using (OleDbConnection conn = new OleDbConnection(connStr))
      {
        conn.Open();
        using (OleDbCommand cmd = new OleDbCommand(query, conn))
        {
          using (OleDbDataReader dr = cmd.ExecuteReader())
          {
            if (dt != null)
              dt.Dispose();
            dt = new DataTable();
            dt.Load(dr);
            dataGridView1.DataSource = dt;
          }
        }
        conn.Close();
      }
    }

    private void frmOleDbGrid_Load(object sender, EventArgs e)
    {
      BuildSql();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      BuildSql();
    }

  }
}

What if I just want to display data from the databse into a listbox? I wont be editing the values so dont need to use a datatable.

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.