Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Showing data from a database to a listbox during runtime

Showing data from a database to a listbox during runtime

Scheduled Pinned Locked Moved C#
helpdatabasequestion
11 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G gamer1127

    Hi. I'm having problems in showing all the data to a listbox. all of the data will be coming from a database and will be shown depending on the choices of the user. i tried to code it but it didn't work. here's my code:

    public frmMainForm()
    {
    InitializeComponent();
    string sConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = Schedule.mdb";

            using (OleDbConnection dbConnect = new OleDbConnection(sConnection))
            {
                dbConnect.Open();
    
                if (prog == "CS")
                {
                    string sqlString = "SELECT cs\_firstcourse, cs\_secondcourse, cs\_thirdcourse, cs\_fourthcourse, cs\_fifthcourse, cs\_sixthcourse, cs\_seventhcourse, cs\_eighthcourse, cs\_ninthcourse FROM CS WHERE cs\_year = @chosenYear AND cs\_term = @chosenTerm;";
    
                    OleDbCommand dbCmd = new OleDbCommand();
                    dbCmd.CommandText = sqlString;
                    dbCmd.Connection = dbConnect;
                    dbCmd.Parameters.AddWithValue("chosenYear", chosenYear);
                    dbCmd.Parameters.AddWithValue("chosenTerm", choseTerm);
    
                    using (OleDbDataReader dbReader = dbCmd.ExecuteReader())
                    {
                        if (dbReader.Read() == true)
                        {
                            for (int a = 0; a < 9; a++)
                            {
                                lstBoxCourses.Items.Add(dbReader\[a\].ToString());
                            }
                        }
                        else
                        {
                            MessageBox.Show("An error has occured while trying to retrieve data from the database", "Class Scheduling: Database Access Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        dbReader.Close();
                    }
                }
                dbConnect.Close();
            }
        }
    

    can someone help me?

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #2

    When you say "It didn't work" what did happen? An Exception? Nothing on the display? Your computer burst into flames?

    All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    D G 2 Replies Last reply
    0
    • OriginalGriffO OriginalGriff

      When you say "It didn't work" what did happen? An Exception? Nothing on the display? Your computer burst into flames?

      All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #3

      OriginalGriff wrote:

      Your computer burst into flames

      Ouch, remind me never to run any of your code :laugh:

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Why are you using VB6? Do you hate yourself? (Christian Graus)

      OriginalGriffO 1 Reply Last reply
      0
      • D DaveyM69

        OriginalGriff wrote:

        Your computer burst into flames

        Ouch, remind me never to run any of your code :laugh:

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #4

        I've built hardware destroyers into embedded code before - if the software believes the hardware has been copied and the firmware pirated, it blows the PSU, and anything else it can get it's hands on. One of our asian distributors and a paraniod MD prompted that one... :laugh:

        All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        D 1 Reply Last reply
        0
        • G gamer1127

          Hi. I'm having problems in showing all the data to a listbox. all of the data will be coming from a database and will be shown depending on the choices of the user. i tried to code it but it didn't work. here's my code:

          public frmMainForm()
          {
          InitializeComponent();
          string sConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = Schedule.mdb";

                  using (OleDbConnection dbConnect = new OleDbConnection(sConnection))
                  {
                      dbConnect.Open();
          
                      if (prog == "CS")
                      {
                          string sqlString = "SELECT cs\_firstcourse, cs\_secondcourse, cs\_thirdcourse, cs\_fourthcourse, cs\_fifthcourse, cs\_sixthcourse, cs\_seventhcourse, cs\_eighthcourse, cs\_ninthcourse FROM CS WHERE cs\_year = @chosenYear AND cs\_term = @chosenTerm;";
          
                          OleDbCommand dbCmd = new OleDbCommand();
                          dbCmd.CommandText = sqlString;
                          dbCmd.Connection = dbConnect;
                          dbCmd.Parameters.AddWithValue("chosenYear", chosenYear);
                          dbCmd.Parameters.AddWithValue("chosenTerm", choseTerm);
          
                          using (OleDbDataReader dbReader = dbCmd.ExecuteReader())
                          {
                              if (dbReader.Read() == true)
                              {
                                  for (int a = 0; a < 9; a++)
                                  {
                                      lstBoxCourses.Items.Add(dbReader\[a\].ToString());
                                  }
                              }
                              else
                              {
                                  MessageBox.Show("An error has occured while trying to retrieve data from the database", "Class Scheduling: Database Access Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                              }
                              dbReader.Close();
                          }
                      }
                      dbConnect.Close();
                  }
              }
          

          can someone help me?

          S Offline
          S Offline
          sanforjackass
          wrote on last edited by
          #5

          i dont know but if you put break point and detecting the code.

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            I've built hardware destroyers into embedded code before - if the software believes the hardware has been copied and the firmware pirated, it blows the PSU, and anything else it can get it's hands on. One of our asian distributors and a paraniod MD prompted that one... :laugh:

            All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #6

            I did something less severe several years ago. A company I worked for that I didn't completely trust used some software that I wrote in my own time that they relied on. I knew they wouldn't pay me for it (I was right) but it made my life so much easier whilst I was there so I permitted it's use making sure the T&Cs stated clearly that only I had the right to use the application at anytime and it was owned by me. I wrote a 'bomb' in it that corrupted the exe if I didn't log on for a certain period. All the data that was used however (the data is of course their property) was placed at this point in unencrypted text files so they could access it but could no longer process it using *my* software. After we inevitably parted company, the software went 'boom' atfter the expected ammount of time :-D I pointed them to the text files when they rang to ask WTF and told them to go **** themselves. One of the highlights of my life :laugh:

            Dave
            Tip: Passing values between objects using events (C#) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Why are you using VB6? Do you hate yourself? (Christian Graus)

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              When you say "It didn't work" what did happen? An Exception? Nothing on the display? Your computer burst into flames?

              All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

              G Offline
              G Offline
              gamer1127
              wrote on last edited by
              #7

              there's no data in the listbox that it supposed to show.

              OriginalGriffO 1 Reply Last reply
              0
              • G gamer1127

                there's no data in the listbox that it supposed to show.

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #8

                In that case, since it appears to be in your form constructor:

                public frmMainForm()
                {
                ...
                }

                I assume that

                if (prog == "CS")

                is failing - presumably because "prog" is not "CS" yet. Where are you assigning that? Have you put a breakpoint at the if statement, and looked as what "prog" is?

                All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                G 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  In that case, since it appears to be in your form constructor:

                  public frmMainForm()
                  {
                  ...
                  }

                  I assume that

                  if (prog == "CS")

                  is failing - presumably because "prog" is not "CS" yet. Where are you assigning that? Have you put a breakpoint at the if statement, and looked as what "prog" is?

                  All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

                  G Offline
                  G Offline
                  gamer1127
                  wrote on last edited by
                  #9

                  I declared the variable prog as global and used it to assign the value "CS" when the user has clicked the radio button. it is goes like this:

                  private void rdioProgCS_CheckedChanged(object sender, EventArgs e)
                  {
                  prog = "CS";
                  rdioProgCS.Enabled = false;
                  rdioProgIT.Enabled = false;
                  }

                  OriginalGriffO 1 Reply Last reply
                  0
                  • G gamer1127

                    I declared the variable prog as global and used it to assign the value "CS" when the user has clicked the radio button. it is goes like this:

                    private void rdioProgCS_CheckedChanged(object sender, EventArgs e)
                    {
                    prog = "CS";
                    rdioProgCS.Enabled = false;
                    rdioProgIT.Enabled = false;
                    }

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #10

                    So since your code that tests it is in your constructor it will not be equal to "CS" until after the form is displayed... By that time, it is too late, the constructor is only run once per instance.

                    All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    G 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      So since your code that tests it is in your constructor it will not be equal to "CS" until after the form is displayed... By that time, it is too late, the constructor is only run once per instance.

                      All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

                      G Offline
                      G Offline
                      gamer1127
                      wrote on last edited by
                      #11

                      ok. i understand now. i'll just transfer the code to a method (which didn't occured to me a while ago). what i first thought is if i put the code inside the method of the listbox the database will be opened once i clicked the listbox and since i need the data to be in the listbox before i click the listbox it will be too late so i put it in the constructor which i think is the best place because once the method of the form has ran the database will be open until i close the form.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups