2

I'm trying for a long time, but I can not do it.

I have this simple XAML:

<Window ....>
    <Grid>
        <Button x:Name="tlacitko" Content="Button" HorizontalAlignment="Left" Height="38" Margin="343,259,0,0" VerticalAlignment="Top" Width="149" Click="Button_Click"/>
        <TextBlock x:Name="vypisBlock" HorizontalAlignment="Left" Height="188" Margin="32,25,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="442"/>

    </Grid>
</Window>

and this .cs

.. usings ..

namespace Pokus
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string cs = @"server=localhost;userid=root;
            password=vertrigo;database=vzkaznik";

            MySqlConnection conn = null;
            MySqlDataReader rdr = null;

            try
            {
                conn = new MySqlConnection(cs);
                conn.Open();

                string stm = "SELECT * FROM elsvo_zpravy";
                MySqlCommand cmd = new MySqlCommand(stm, conn);
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                  string vypis = ""; 
                  vypis += (rdr.GetInt32(0) + " - " + rdr.GetString(1) + " - " + rdr.GetString(2) + " - " + rdr.GetString(3) + "\n");
                  vypisBlock.Text = vypis;
                }


            }
            catch (MySqlException ex)
            {
                string chyba = "Error: {0}" + ex.ToString(); // doplnit (if = cislo chyby -> hlaska) místo šílenosti co to zobrazuje ted
                vypisBlock.Text = chyba;

            }

        }

    }
}

I want the application indicated the contents of the entire table now shows only the last line.

How do I do that?

Thank you

4 Answers 4

2

For each record from the database you're setting vypisBlock.Text, thus only the last record will be shown.

Either create a single string and assign the XAML element once (after the loop). And use a StringBuilder rather than a string when performing lots of concaternation.

However better would be to use a DataGrid, which is there for tabular data. You define a "row template", and use data binding to populate. There are lots of examples, but the linked MSDN page has a reasonable example.

Sign up to request clarification or add additional context in comments.

Comments

1

You overwrite your result constantly in the while loop.

StringBuilder vypis = ""; 
while (rdr.Read())
{
 vypis.AppendLine(string.Format("{0} - {1} - {2} - {3}",
       rdr.GetInt32(0),
       rdr.GetString(1),
       rdr.GetString(2),
       rdr.GetString(3));    
}
vypisBlock.Text = vypis.ToString();

2 Comments

Now I have to figure it out also :D
if you don't understand it enough I can offer some extra info
1

There would be more possible improvements of your code, but the basic problem you are after is that you keep overwriting the output in the loop, which can be corrected e.g. like this:

var vypis = new StringBuilder();
while (rdr.Read())
{
   vypis.AppendFormat("{0} - {1} - {2} - {3}{4}", 
      rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), 
      rdr.GetString(3), Environment.NewLine);
}

Comments

0

Got it, but thank you for reply!

...
            try
            {
                conn = new MySqlConnection(cs);
                conn.Open();

                string stm = "SELECT * FROM elsvo_zpravy";
                MySqlCommand cmd = new MySqlCommand(stm, conn);
                rdr = cmd.ExecuteReader();
                string vypis = "";
                while (rdr.Read())
                {

                  vypis += (rdr.GetInt32(0) + " - " + rdr.GetString(1) + " - " + rdr.GetString(2) + " - " + rdr.GetString(3) + "\n");

                }
                vypisBlock.Text = vypis;

            }
...

Now displays all records, each on a new line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.