How to display images or information from database

2019-08-17 06:19发布

问题:

Hello Everyone! I have a problem displaying my images and information such as full name in my program. I have a for looped radiobutton and it counts as to how many candidates are running in a specific position. Example is "President".

Here is my code as of now :

    cmd = new SqlCommand("SELECT COUNT(Position) FROM TableVote WHERE Position='" + "President" + "'", sc);
            Int32 PresCount = (Int32)cmd.ExecuteScalar();

            TxtPresCount.Text = PresCount.ToString();

            //int lol = Convert.ToInt32(TxtPresCount.Text);

            var panel = new FlowLayoutPanel();
            panel.SuspendLayout();
            panel.Size = new Size(600, 150);
            panel.Location = new Point(50, 50);
            panel.FlowDirection = FlowDirection.LeftToRight;
            panel.AutoScroll = true;
            panel.WrapContents = false;
            this.Controls.Add(panel);

            for (int i = 0; i < PresCount; ++i)
            {
                var radioButton = new RadioButton();
                radioButton.Size = new Size(75, 75);
                radioButton.CheckAlign = ContentAlignment.MiddleCenter;
                radioButton.ImageAlign = ContentAlignment.MiddleCenter;
                panel.Controls.Add(radioButton);

                //radioButton.Image = Image.FromFile();
                radioButton.ImageAlign = ContentAlignment.MiddleCenter;
                radioButton.FlatStyle = FlatStyle.Flat;
            }
            panel.ResumeLayout();

as for the codes above, it counts all the President that is stored in the database. But my problem is, how do i put an image/Name that is stored in the database? Example i have 5 candidates running for the position of President, I want all the data to be in 5 radiobutton. How do i put the information into the radiobutton? Please help. :(

I add information using this code below :

    sc.Open();
        try
        {
        cmd = new SqlCommand("INSERT INTO TableVote (Position, FirstName, MiddleName, LastName, YearLevel, Course, SchoolYear, imgPath, imgImage) VALUES (@position, @firstname, @middlename, @lastname, @yearlevel, @course, @schoolyear, @imgpath, '" + _pb + "')", sc);
        cmd.Parameters.AddWithValue("@position", _position);
        cmd.Parameters.AddWithValue("@firstname", _firstname);
        cmd.Parameters.AddWithValue("@middlename", _middlename);
        cmd.Parameters.AddWithValue("@lastname", _lastname);
        cmd.Parameters.AddWithValue("@yearlevel", _yearlevel);
        cmd.Parameters.AddWithValue("@course", _course);
        cmd.Parameters.AddWithValue("@schoolyear", _schoolyear);
        cmd.Parameters.AddWithValue("@imgpath", _imgpath);

            int res = cmd.ExecuteNonQuery();
            if(res > 0)
            {
                MessageBox.Show("Data Stored Successfully!");
                FAdminSet._cleardata = cleardata;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            sc.Close();
        }

this is my database :

回答1:

public partial class Form1 : Form {
   public Form1(){
     InitializeComponent();
     //do this if you want to register the Load event handler using code
     Load += Form1_Load;
   }
   FlowLayoutPanel panel = new FlowLayoutPanel();
   private void InitPanel(){
     panel.Size = new Size(600, 150);
     panel.Location = new Point(50, 50);
     panel.FlowDirection = FlowDirection.LeftToRight;
     panel.AutoScroll = true;
     panel.WrapContents = false;
     Controls.Add(panel);
   }
   //Load event handler
   private void Form1_Load(object sender, EventArgs e){
     InitPanel();
     panel.SuspendLayout();
     string cmdText = "SELECT (FirstName + ' ' + MiddleName + ' ' + LastName) as FullName, " +
                 "imgPath as ImagePath FROM TableVote WHERE Position='President'";
     using(SqlCommand com = new SqlCommand(cmdText,sc)){
       if(sc.State != ConnectionState.Open) sc.Open();
       SqlDataReader reader = com.ExecuteReader();       
       while(reader.Read()){
         AddRadioButton(reader.GetString(0), reader.GetString(1));
       }
       reader.Close();
       sc.Close();
       panel.ResumeLayout(true);
     }
   }
   private void AddRadioButton(string fullName, string imagePath){
     RadioButton radio = new RadioButton {Text = fullName, Parent = panel};
     radio.AutoSize = true;
     radio.Image = new Bitmap(Image.FromFile(imagePath),75,75);
     radio.TextImageRelation = TextImageRelation.ImageAboveText;    
     radio.CheckAlign = ContentAlignment.BottomCenter;   
   }
}

NOTE: I can see that you store 2 info involving images in your table, I think you should choose 1 of them, storing Image Path is easy, light-weight for your table but the info may be lost if your Image path hasn't pointed to the actual image anymore.