Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:00:15+00:00 2026-05-26T23:00:15+00:00

As title states, my piece of code which was from an example provided by

  • 0

As title states, my piece of code which was from an example provided by my professor.

My output is:

MagazineID Title Publisher Price SubscriptionRate 
1 People Times Inc. 4.95 19.95 
2 Car and Driver Hachetter Inc. 3.95 19.99 

Code:

    private void btnShowMags_Click(object sender, EventArgs e)
    {
        // Creating new instance of the DisplayMags form.
        DisplayMags displayMags = new DisplayMags();

        // find the path where the executable resides
        string dbPath = Application.StartupPath;

        // Providing a path to the MS Access file.
        string connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
            + dbPath + @"\..\..\..\..\Magazines.mdb; User Id=admin; Password=";

        // Creating a new connection and assigning it to a variable.
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = connString;

        // Creating a new instance for a command which we will use later.
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = conn;

        // declare and instantiate the command
        OleDbCommand cmdMagazines = new OleDbCommand();
        cmdMagazines.CommandText = "select * from magazine";
        cmdMagazines.Connection = conn;

        OleDbDataReader drMagazines;

        try
        {
            // open the connection
            conn.Open();

            // retrieve data from the data source to the data reader
            drMagazines = cmdMagazines.ExecuteReader();

            if (drMagazines.HasRows)
            {
                // populate the column headings
                for (int i = 0; i < drMagazines.FieldCount; i++)
                    displayMags.txtDisplayMags.Text += drMagazines.GetName(i) + " ";
                displayMags.txtDisplayMags.Text += "\r\n";

                // populate the data by row
                while (drMagazines.Read())
                {
                    for (int i = 0; i < drMagazines.FieldCount; i++)
                        displayMags.txtDisplayMags.Text += drMagazines.GetValue(i) + " ";
                    displayMags.txtDisplayMags.Text += "\r\n";
                }
            }
        }
        catch (Exception ex)
        {
            // Displaying any errors that might have occured.
            MessageBox.Show("Error opening the connection: " + ex.Message + "\r\n");
        }
        finally
        {
            // Closing connection after task was completed.
            conn.Close();
        }

        // Displaying DisplayMags form, assuring that earlier form
        // will not be accessible. Show() let us access all forms.
        displayMags.ShowDialog();
    }

And, I am trying to make it look like this:

enter image description here

EDIT. This work, but does this represent correct programming practices?

            if (drMagazines.HasRows)
            {
                while (drMagazines.Read())
                {
                    displayMags.txtDisplayMags.Text += "=== Magazine " + 
                        drMagazines.GetValue(0) + " ===" + Environment.NewLine + 
                        drMagazines.GetValue(1) + Environment.NewLine +
                        drMagazines.GetValue(2) + Environment.NewLine +
                        drMagazines.GetValue(3) + Environment.NewLine +
                        drMagazines.GetValue(4) + Environment.NewLine + 
                        Environment.NewLine;
                }
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:00:16+00:00Added an answer on May 26, 2026 at 11:00 pm

    I’m not giving you the entire answer, because that would defeat the purpose of the assignment and you wouldn’t learn anything. However, I can give you ideas on where to start to solve the problem.

    Your output starts in the block starting with if (drMagazines.HasRows). The code within that block is where you need to make your changes.

    You need to change it so that instead of printing out the column headings and then the content of each row, you print a separator that includes the magazine number, a line ending (Environment.NewLine) and then separate rows that have titles and then content.

    You have the necessary information in your code now – you have comments where you populate the column headings and then populate the data by row. Change that so that you populate a single heading and then populate the row content for that heading. A suggestion would be to change it from two loops to one – read a column heading, and then the row content that it contains. You can add any additional content or formatting during that loop for each item.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty much as the title states, I'm grabbing some values from a Db, which
As the title states, is there a way to prevent extra elements from showing
As the title states, does it matter - will it make any difference? Example:
As the title states, I made an Ajax call to a .html file which
so as the title states, using the following code I have got it populating
Debugging the following piece of code I get Source not found notification which probably
As the title states how can I with Google API 3 open Infowindow from
As the title states, I have a C# console app which uses interop to
As the title states, I can connect to another machine from my server via
Simple as the title states: Can you use only Java commands to take a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.