Note that there are some explanatory texts on larger screens.

plurals
  1. POPrinting multiple attributes
    primarykey
    data
    text
    <p>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).</p> <pre><code>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 } </code></pre> <p>}</p> <pre><code> } } </code></pre> <p>And here is the print output (edited (after removing finally clause)): </p> <p>1234 1200,0000 5 Aviator 1111 Raiban<br> 1234 1200,0000 5 Aviator 1111 Raiban<br> 1234 1200,0000 5 Aviator 1111 Raiban<br> 4321 110,0000 6 Femme 1111 Hindberg<br> 4321 110,0000 6 Femme 1111 Hindberg<br> 4321 110,0000 6 Femme 1111 Hindberg<br> 3241 500,0000 2 Pilot-etui 3333 Raiban<br> 3241 500,0000 2 Pilot-etui 3333 Raiban<br> 3241 500,0000 2 Pilot-etui 3333 Raiban<br> 1423 250,0000 30 Splash 4444 Lensway<br> 1423 </p> <p>(If i use an integer less than 3 in the for loop, it does not print anything for some reason)</p> <p>The data in the table is</p> <p>Pris = price. Lager = storage. Antal = Amount. Gruppe = Group. Mærke = Brand</p> <pre><code>+---------+----------+-----------------+------------+---------------+-----------+ | 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 | +---------+----------+-----------------+------------+---------------+-----------+ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload