views:

170

answers:

1

Hello , another sql problem of mine ..

this time the reader doesn't work properly ( I think so ) I use this code and I get only 1 record added and my db has 100`s of records ..

 public void addPosts() 
    {

        string dbfile = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\msgdb.sdf";

        string sql;
        PictureBox  avaBox = new PictureBox();
        PictureBox pictureBox1 = new PictureBox();
        Button atBtn = new Button();
        RichTextBox msgBox = new RichTextBox();
        Panel panelz = new Panel();
        DateTime dt = DateTime.Now;
        SqlCeConnection connection = new SqlCeConnection("datasource=" + dbfile);
        // Read all rows from the table test_table into a dataset (note, the adapter automatically opens the connection)
        SqlCeDataAdapter adapter = new SqlCeDataAdapter("select * from posts", connection);
        DataSet data = new DataSet();
        adapter.Fill(data);

        SqlCeCommand cmd = new SqlCeCommand();
        cmd.Connection = connection;
        sql = "Select user_from,msg,avatar FROM posts";
        cmd.CommandText = sql;
        connection.Open();
        SqlCeDataReader reader = cmd.ExecuteReader();
        int i = 0;
       while (reader.Read())
            {
                i++;
                string ava = reader.GetString(2);
                string usrFrom = reader.GetString(0);
                string messige = reader.GetString(1);
                // 
                // groupBox1
                // 
                panelz.Controls.Add(pictureBox1);
                panelz.Controls.Add(atBtn);
                panelz.Controls.Add(avaBox);
                panelz.Controls.Add(msgBox);
                panelz.Location = new System.Drawing.Point(0, 335);
                panelz.Name = "panel"+ i;
                panelz.Size = new System.Drawing.Size(335, 90);

                panelz.TabIndex = 0;
                panelz.TabStop = false;


                // 
                // pictureBox1
                // 
                pictureBox1.Dock = System.Windows.Forms.DockStyle.Right;
                pictureBox1.Image = global::m23.Properties.Resources.post_area;
                pictureBox1.Location = new System.Drawing.Point(58, 0);
                pictureBox1.Name = "pictureBox1"+i;
                pictureBox1.Size = new System.Drawing.Size(281, 99);
                pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
                pictureBox1.TabIndex = 1;
                pictureBox1.TabStop = false;
                // 
                // atBtn
                // 
                atBtn.AutoSize = true;
                atBtn.BackgroundImage = global::m23.Properties.Resources.post_user;
                atBtn.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
                atBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                atBtn.Location = new System.Drawing.Point(0, 62);
                atBtn.Name = "atBtn"+i;
                atBtn.Size = new System.Drawing.Size(28, 25);
                atBtn.TabIndex = 2;
                atBtn.UseVisualStyleBackColor = true;

                //
                avaBox.Location = new System.Drawing.Point(0, 0);
                avaBox.Name = "avaBox"+i;
                avaBox.Size = new System.Drawing.Size(53, 53);
                avaBox.TabIndex = 4;
                avaBox.TabStop = false;
                avaBox.ImageLocation = "http://img.edno23.com/avatars/thumbs/" + ava;

                //
                msgBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
                msgBox.Location = new System.Drawing.Point(76, 10);

                msgBox.Name = "msgBox"+i;
                msgBox.ReadOnly = true;
                msgBox.BackColor = Color.White;
                msgBox.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None;
                msgBox.Size = new System.Drawing.Size(251, 68);
                msgBox.TabIndex = 3;
                msgBox.Text = messige;
                msgBox.BringToFront();
                //

                CommonFlowPanel.Controls.Add(panelz);


            }


        connection.Close(); 
    }

Thanks for the help in advance!

+2  A: 

Put the declarations for the Panel and all the controls that go onto each panel inside your while loop. You're basically re-adding your once instance of Panel ("panelz") over and over again. What you want to do is create a new panel for each row, inside your while loop, along with new instances of each control that sits on the panel.

MusiGenesis
AHH!! sometimes its so simple .. i gues im too tired to write code rigth now :) thanks for the answer !
Aviatrix