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

  • Home
  • SEARCH
  • 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 8981189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:16:17+00:00 2026-06-15T20:16:17+00:00

I have this program, where i need to print out the attributes of multiple

  • 0

I have this program, where i need to print out the attributes of multiple goods (vare). The problem i am having is that it only prints out the attributes of a single item multiple times. I tried putting the attributes into a 2-dimensional array, but it still prints out the same attributes. My output file is a .txt file. What i want, is for the program to print out a list of goods, each with their respective attributes shown. (There are 4 different items, and 6 different attributes).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace Opgave_4._1
{
  class Program
{

        static void Main()
 {
    string connString;
    connString = "Data Source=(local);Initial Catalog=Occhi che guardano;Integrated Security=SSPI";

    string sqlstring = "SELECT * FROM Vare ORDER BY Vare.Varenavn";

    Console.WriteLine(sqlstring);

    SqlConnection conn = new SqlConnection(connString);
    SqlCommand cmd = new SqlCommand(sqlstring, conn);
    conn.Open();
    SqlDataReader Reader = cmd.ExecuteReader();

    string att1;
    decimal att2;
    int att3;
    string att4;
    int att5;
    string att6;
    string[,] vareliste = new string[10, 10];
    StreamWriter Stream = null;
    String UdFil = "C:\\Users\\Thomas\\Desktop\\text.txt";
    Stream = new StreamWriter(UdFil);
    try
    { 
        while (Reader.Read())
        {
            if (Reader.IsDBNull(0))
                att1 = "hej";
            else
                att1 = Reader.GetString(0);
            if (Reader.IsDBNull(1))
                att2 = 0.0006000M;
            else
                att2 = Reader.GetDecimal(1);
            if (Reader.IsDBNull(2))
                att3 = 12;
            else
                att3 = Reader.GetInt32(2);
            if (Reader.IsDBNull(3))
                att4 = "hej";
            else
                att4 = Reader.GetString(3);
            if (Reader.IsDBNull(4))
                att5 = 14;
            else
                att5 = Reader.GetInt32(4);
            if (Reader.IsDBNull(5))
                att6 = "hejsa";
            else
                att6 = Reader.GetString(5);




            Stream.WriteLine(att1 + " " + att2 + " " + att3 + " " + att4 + " " + att5 + " " + att6);

            try
            {

            }
            catch (Exception e)
            {
                Console.WriteLine("Fejl i sti  {0}", e.ToString());
                Console.ReadLine();
            }
            finally
            {
                if (Stream != null)
                    Stream.Close();
            }


        }

    }

    catch (System.Data.SqlClient.SqlException e)
    {
        Console.WriteLine(e.Message);
    }
    finally
    {
        Reader.Close(); // luk ResultSet
        conn.Close(); // luk connection igen
    }

}

    }
}

And here is the print output (edited (after removing finally clause)):

1234 1200,0000 5 Aviator 1111 Raiban
1234 1200,0000 5 Aviator 1111 Raiban
1234 1200,0000 5 Aviator 1111 Raiban
4321 110,0000 6 Femme 1111 Hindberg
4321 110,0000 6 Femme 1111 Hindberg
4321 110,0000 6 Femme 1111 Hindberg
3241 500,0000 2 Pilot-etui 3333 Raiban
3241 500,0000 2 Pilot-etui 3333 Raiban
3241 500,0000 2 Pilot-etui 3333 Raiban
1423 250,0000 30 Splash 4444 Lensway
1423

(If i use an integer less than 3 in the for loop, it does not print anything for some reason)

The data in the table is

Pris = price. Lager = storage. Antal = Amount. Gruppe = Group. Mærke = Brand

+---------+----------+-----------------+------------+---------------+-----------+
| Vare_id | Varepris | VareLager_antal |  Varenavn  | Varegruppe_nr | Varemærke |
+---------+----------+-----------------+------------+---------------+-----------+
|    1234 | 1200     |               5 | Aviator    |          1111 | Raiban    |
|    1423 | 250      |              30 | Splash     |          4444 | Lensway   |
|    3241 | 500      |               2 | Pilot-etui |          3333 | Raiban    |
|    4321 | 110      |               6 | Femme      |          1111 | Hindberg  |
+---------+----------+-----------------+------------+---------------+-----------+
  • 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-06-15T20:16:19+00:00Added an answer on June 15, 2026 at 8:16 pm

    Creating a new StreamWriter for each iteration means you overwrite your file each time. You’ll only end up with the last record in here.

    Reading the same field four times for each iteration is going to return the same thing each time. The recordset only moves on when you call Reader.Read()

    You should try and use using statements to control cleanup of your resources. For example

    using (SqlConnection conn = new SqlConnection(connString)) {
        using (StreamWriter s = new StreamWriter(UdFil)) {
            while (Reader.Read()) {
               ....
            }
        }
    }
    

    This will close the Connection and StreamWriter appropriately.

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

Sidebar

Related Questions

I have a program that looks like this. I need to consistently write something
I have this program that should execute a piece of code base on the
I have this program that I am working on for class, I think the
I have this simple program that computes salaries for four different worker types. It's
I have simplified an issue that I've been having trying to isolate the problem,
I have a program that I need to use signals and handlers with. I
The following code prints the message that I need it to print using the
I have a task that requires me to print out the following quantities: I
I have a college assignment where I need to print out items sold by
I have this program in Python which should save text files to a folder

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.