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. Comparing a Tableadapter to checkedlistbox items

Comparing a Tableadapter to checkedlistbox items

Scheduled Pinned Locked Moved C#
databaseregexquestion
9 Posts 2 Posters 0 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.
  • F Offline
    F Offline
    falles01
    wrote on last edited by
    #1

    :(sigh:Hi Everyone, Okay in the past two weeks I have learnt so much and I'm starting to work things out on my own. However even this one is tricky for me. I am trying to automatically check checkedlistboxes based on data in the database. I know I have to compare the dataset tableadapter to items in the ckedlistbox and then tick them if they match the ID. But how? I have created a join table aswell with EmpID and TechSkillsID so that people can tick multiple skills and they will be saved in there. Even though it isn't working yet. and yes the foreign keys are set up correctly. My code doesn't tick anything. This is my code. [code] sql = "Select t.ProgLanguagesDatabase,e.EmployeeID,e.TechnicalSkillsID from TechnicalSkills t, employees e where t.TechnicalSkillsID = e.TechnicalSkillsID and e.EmployeeID = '" + FirstnameText.Text.ToString() + "'"; for (int i = 0; i < techSkillsCheckListBox2.Items.Count; i++) { for (int j = 0; j < this.dataSet1.TechnicalSkills.Count;j++) { if (i == j) { techSkillsCheckListBox2.SetItemChecked(i, true); } } Thank you so much Sianny (aka Sharny)

    M 1 Reply Last reply
    0
    • F falles01

      :(sigh:Hi Everyone, Okay in the past two weeks I have learnt so much and I'm starting to work things out on my own. However even this one is tricky for me. I am trying to automatically check checkedlistboxes based on data in the database. I know I have to compare the dataset tableadapter to items in the ckedlistbox and then tick them if they match the ID. But how? I have created a join table aswell with EmpID and TechSkillsID so that people can tick multiple skills and they will be saved in there. Even though it isn't working yet. and yes the foreign keys are set up correctly. My code doesn't tick anything. This is my code. [code] sql = "Select t.ProgLanguagesDatabase,e.EmployeeID,e.TechnicalSkillsID from TechnicalSkills t, employees e where t.TechnicalSkillsID = e.TechnicalSkillsID and e.EmployeeID = '" + FirstnameText.Text.ToString() + "'"; for (int i = 0; i < techSkillsCheckListBox2.Items.Count; i++) { for (int j = 0; j < this.dataSet1.TechnicalSkills.Count;j++) { if (i == j) { techSkillsCheckListBox2.SetItemChecked(i, true); } } Thank you so much Sianny (aka Sharny)

      M Offline
      M Offline
      Malcolm Smart
      wrote on last edited by
      #2

      falles01 wrote:

      if (i == j)

      I think your logic is wrong. All you are doing is comparing two numbers - the index of the item in the collection. Imagine techSkillsCheckListBox2.Items has 4 items and this.dataSet1.TechnicalSkills has 5 items. Your loop will look like 0 = 0 : true 0 = 1 : false 0 = 2 : false 0 = 3 : false 0 = 4 : false 1 = 0 : false 1 = 1 : true 1 = 2 : false 1 = 3 : false 1 = 4 : false 2 = 0 : false 2 = 1 : false 2 = 2 : true 2 = 3 : false 2 = 4 : false and so on, which is not what you want. You need to iterate through the dateset, retrieve the identifier of the skill, which should match an identifier on the check box, and then set the check box. Step through with a debugger and you will see what's happening.

      "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

      "I haven't spoken to my wife now for 48 hours. I don't like to interrupt her.

      F 1 Reply Last reply
      0
      • M Malcolm Smart

        falles01 wrote:

        if (i == j)

        I think your logic is wrong. All you are doing is comparing two numbers - the index of the item in the collection. Imagine techSkillsCheckListBox2.Items has 4 items and this.dataSet1.TechnicalSkills has 5 items. Your loop will look like 0 = 0 : true 0 = 1 : false 0 = 2 : false 0 = 3 : false 0 = 4 : false 1 = 0 : false 1 = 1 : true 1 = 2 : false 1 = 3 : false 1 = 4 : false 2 = 0 : false 2 = 1 : false 2 = 2 : true 2 = 3 : false 2 = 4 : false and so on, which is not what you want. You need to iterate through the dateset, retrieve the identifier of the skill, which should match an identifier on the check box, and then set the check box. Step through with a debugger and you will see what's happening.

        "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

        "I haven't spoken to my wife now for 48 hours. I don't like to interrupt her.

        F Offline
        F Offline
        falles01
        wrote on last edited by
        #3

        Thanks. so I've heard. Just ignore i==j.I've tried everything and I know what the debugger is showing me but I would love to know exactly how to iterate through the dataset and compare the items in teh table to the items in my checkedlistbox. If the items are matching based on the techskillsID then I have to have them ticked. I have retrieved the ID by using sql = "Select t.ProgLanguagesDatabase,e.EmployeeID,e.TechnicalSkillsID from TechnicalSkills t, employees e where t.TechnicalSkillsID = e.TechnicalSkillsID and e.EmployeeID = '" + FirstnameText.Text.ToString() + "'"; Can you suggest how I can create the if statement. I know we are supposed to be taught the logic and we find the answer ourselves, but I think I understand the logic but can't find the answer anywhere.Its the fact that I have a checkedlistbox, and a sql database and table adapters. its hard to find the exact answer when you have so many topics involved. Thanks. Much appreciated. :)

        M 1 Reply Last reply
        0
        • F falles01

          Thanks. so I've heard. Just ignore i==j.I've tried everything and I know what the debugger is showing me but I would love to know exactly how to iterate through the dataset and compare the items in teh table to the items in my checkedlistbox. If the items are matching based on the techskillsID then I have to have them ticked. I have retrieved the ID by using sql = "Select t.ProgLanguagesDatabase,e.EmployeeID,e.TechnicalSkillsID from TechnicalSkills t, employees e where t.TechnicalSkillsID = e.TechnicalSkillsID and e.EmployeeID = '" + FirstnameText.Text.ToString() + "'"; Can you suggest how I can create the if statement. I know we are supposed to be taught the logic and we find the answer ourselves, but I think I understand the logic but can't find the answer anywhere.Its the fact that I have a checkedlistbox, and a sql database and table adapters. its hard to find the exact answer when you have so many topics involved. Thanks. Much appreciated. :)

          M Offline
          M Offline
          Malcolm Smart
          wrote on last edited by
          #4

          I am assuming (hoping) that you create your CheckBoxList dynamically, and read the possible options from the db. When you do, you add the checks as ListItems. Something like while (myreader.Read()) { ListItem myCheckBoxItem = new ListItem( myreader["DESCRIPTION"].ToString() , myreader["ID"].ToString() ); myCheckBoxList.Items.Add( myCheckBoxItem ); } To select the relevant ones, read all the skills for a user from the database sqlstring = "select skillID from userSkills where userID = 'myuser'"; //populate a datatable with the result set DataRow[] rows = dt.Select(); foreach(DataRow row in rows) { string techID = row["skillID"].ToString(); myCheckBoxList.Items.FindByValue( techID ).Selected = true; } And that's it. Please excuse typos etc as I can't get to a dev box to test this.

          "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

          "I haven't spoken to my wife now for 48 hours. I don't like to interrupt her.

          F 2 Replies Last reply
          0
          • M Malcolm Smart

            I am assuming (hoping) that you create your CheckBoxList dynamically, and read the possible options from the db. When you do, you add the checks as ListItems. Something like while (myreader.Read()) { ListItem myCheckBoxItem = new ListItem( myreader["DESCRIPTION"].ToString() , myreader["ID"].ToString() ); myCheckBoxList.Items.Add( myCheckBoxItem ); } To select the relevant ones, read all the skills for a user from the database sqlstring = "select skillID from userSkills where userID = 'myuser'"; //populate a datatable with the result set DataRow[] rows = dt.Select(); foreach(DataRow row in rows) { string techID = row["skillID"].ToString(); myCheckBoxList.Items.FindByValue( techID ).Selected = true; } And that's it. Please excuse typos etc as I can't get to a dev box to test this.

            "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

            "I haven't spoken to my wife now for 48 hours. I don't like to interrupt her.

            F Offline
            F Offline
            falles01
            wrote on last edited by
            #5

            Thankyou. that was an excellent answer. I am trying it, how ever...will that work with a winform not a webform? In order to use Listitem I had to add the web reference system.web.ui.webcontrols and that causes 1137 errors. Hmmmmmm Is there someway of reading the dataset with out listitems? Thank you Sianny. (Aka Sharny)

            M 1 Reply Last reply
            0
            • F falles01

              Thankyou. that was an excellent answer. I am trying it, how ever...will that work with a winform not a webform? In order to use Listitem I had to add the web reference system.web.ui.webcontrols and that causes 1137 errors. Hmmmmmm Is there someway of reading the dataset with out listitems? Thank you Sianny. (Aka Sharny)

              M Offline
              M Offline
              Malcolm Smart
              wrote on last edited by
              #6

              I am not reading the dataset with ListItems. I am reading the dataset with reader.Read() and then populating the checkboxlist with ListItems. How did you create your checkboxlist? have you hardcoded all the entries in Visual Studio or do you create it dynamically?

              "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

              "I haven't spoken to my wife now for 48 hours. I don't like to interrupt her.

              F 2 Replies Last reply
              0
              • M Malcolm Smart

                I am not reading the dataset with ListItems. I am reading the dataset with reader.Read() and then populating the checkboxlist with ListItems. How did you create your checkboxlist? have you hardcoded all the entries in Visual Studio or do you create it dynamically?

                "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

                "I haven't spoken to my wife now for 48 hours. I don't like to interrupt her.

                F Offline
                F Offline
                falles01
                wrote on last edited by
                #7

                I tried to copy your code but it won't let me use listitems at all because it belongs to webforms. I have used tableadapters and have used Technicalskills list to bind as below. Sorry I'm a little confused. :-O private void TechnicalSkillsList() { DataSet1TableAdapters.TechnicalSkillsTableAdapter technicalskillsTableAdapter = new ResourceSearchTool.DataSet1TableAdapters.TechnicalSkillsTableAdapter(); DataSet1.TechnicalSkillsDataTable techSkillsDT = technicalskillsTableAdapter.GetData(); foreach (DataSet1.TechnicalSkillsRow techSkillsRow in techSkillsDT.Rows) this.techSkillsCheckListBox2.Items.Add(techSkillsRow.ProgLanguagesDatabase); }

                1 Reply Last reply
                0
                • M Malcolm Smart

                  I am not reading the dataset with ListItems. I am reading the dataset with reader.Read() and then populating the checkboxlist with ListItems. How did you create your checkboxlist? have you hardcoded all the entries in Visual Studio or do you create it dynamically?

                  "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

                  "I haven't spoken to my wife now for 48 hours. I don't like to interrupt her.

                  F Offline
                  F Offline
                  falles01
                  wrote on last edited by
                  #8

                  I have worked out on my own how to save multiple skills. so now its down to automatically populating the checkedlistbox with the skills. which still brings me back to my precious forum post,,,but I'm still working on it and i'm giving you a 5 for being such a good response anyway. :-D

                  1 Reply Last reply
                  0
                  • M Malcolm Smart

                    I am assuming (hoping) that you create your CheckBoxList dynamically, and read the possible options from the db. When you do, you add the checks as ListItems. Something like while (myreader.Read()) { ListItem myCheckBoxItem = new ListItem( myreader["DESCRIPTION"].ToString() , myreader["ID"].ToString() ); myCheckBoxList.Items.Add( myCheckBoxItem ); } To select the relevant ones, read all the skills for a user from the database sqlstring = "select skillID from userSkills where userID = 'myuser'"; //populate a datatable with the result set DataRow[] rows = dt.Select(); foreach(DataRow row in rows) { string techID = row["skillID"].ToString(); myCheckBoxList.Items.FindByValue( techID ).Selected = true; } And that's it. Please excuse typos etc as I can't get to a dev box to test this.

                    "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

                    "I haven't spoken to my wife now for 48 hours. I don't like to interrupt her.

                    F Offline
                    F Offline
                    falles01
                    wrote on last edited by
                    #9

                    Okay I was wrong. I didn't work it out. I'm still trying to save multiple skills and I thought it was saving them but it was actually saving the number of the index instead of the ID. Here is what I'm using. I still need the answer of how to get the id of the ticked items and then store it. string sql = "SELECT t.TechnicalSkillsID from TechnicalSkills where TechnicalSkillsID = '" + techSkillsCheckListBox2.CheckedItems.ToString() + "'"; if (techSkillsCheckListBox2.CheckedItems.Count != 0) { for (int x = 0; x <= techSkillsCheckListBox2.CheckedItems.Count -1; x++) foreach (int i in techSkillsCheckListBox2.CheckedIndices) { sql = "Insert into EmpSkills(EmployeeID,TechnicalSkillsID) values ('" + this.EmployeeID + "','" + techSkillsCheckListBox2.CheckedIndices[x].ToString() + "')"; SqlCommand adoCmd = new SqlCommand(sql, adoConn); adoCmd.ExecuteNonQuery(); Thank you so much ...this is killing me! I thought I had it. Sianny aka Sharny

                    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