Reading data from a dataset with a checkedlistbox
-
Hi I'm posting a new question in case Malcolm doesn't read my reply in time. Basically what is wrong with this code? I want to automatically check the checklistbox items depending on if the items match the employees TechnicalSkillsID in the database table. Malcolm gave me a web form example which was a great response except I forgot to tell him I was using a winform. can you help at all?
int i; for (i = 0; i <= (techSkillsCheckListBox2.Items.Count - 1); i++) { // SqlDataReader MyDataReader = adoCmd.ExecuteReader(CommandBehavior.CloseConnection); while (adoDR.Read()) { sql = "Select em.TechnicalSkillsID,t.ProgLanguagesDatabase from EmpSkills em,TechnicalSkills t where EmployeeID = '" + FirstnameText.Text + "' and em.TechnicalSkillsID = t.TechnicalSkillsID"; //populate a datatable with the result set DataRow[] rows = dataSet1.TechnicalSkills.Select(); foreach (DataRow row in rows) { string techID = row["TechnicalSkillsID"].ToString(); techSkillsCheckListBox2.SetItemChecked(i, true); } } }
Sianny aka Sharny -
Hi I'm posting a new question in case Malcolm doesn't read my reply in time. Basically what is wrong with this code? I want to automatically check the checklistbox items depending on if the items match the employees TechnicalSkillsID in the database table. Malcolm gave me a web form example which was a great response except I forgot to tell him I was using a winform. can you help at all?
int i; for (i = 0; i <= (techSkillsCheckListBox2.Items.Count - 1); i++) { // SqlDataReader MyDataReader = adoCmd.ExecuteReader(CommandBehavior.CloseConnection); while (adoDR.Read()) { sql = "Select em.TechnicalSkillsID,t.ProgLanguagesDatabase from EmpSkills em,TechnicalSkills t where EmployeeID = '" + FirstnameText.Text + "' and em.TechnicalSkillsID = t.TechnicalSkillsID"; //populate a datatable with the result set DataRow[] rows = dataSet1.TechnicalSkills.Select(); foreach (DataRow row in rows) { string techID = row["TechnicalSkillsID"].ToString(); techSkillsCheckListBox2.SetItemChecked(i, true); } } }
Sianny aka Sharnyfalles01 wrote:
for (i = 0; i <= (techSkillsCheckListBox2.Items.Count - 1); i++)
Why not i < techSkillsCheckListBox2.Items.Count ? Or even foreach (CheckBox box in techSkillsCheckListBox2.Items) ? This code is really broken. What you want to do, is fill a list with the items that are selected, then go through each check box and do something like checkbox.Checked = (myListOfItems.Contains(theKEyForThisCheckbox)); Of course, I just invented some variables, assume the Tag of the checkbox contains the key value, so you can look it up. The code you're presenting does a nested loop, and just sets each box to be checked, over and over again.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
falles01 wrote:
for (i = 0; i <= (techSkillsCheckListBox2.Items.Count - 1); i++)
Why not i < techSkillsCheckListBox2.Items.Count ? Or even foreach (CheckBox box in techSkillsCheckListBox2.Items) ? This code is really broken. What you want to do, is fill a list with the items that are selected, then go through each check box and do something like checkbox.Checked = (myListOfItems.Contains(theKEyForThisCheckbox)); Of course, I just invented some variables, assume the Tag of the checkbox contains the key value, so you can look it up. The code you're presenting does a nested loop, and just sets each box to be checked, over and over again.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Thanks but the part you are talking about that is wrong, is actually the part that is correct and gives the correct value. I got that off msdn. Yes I want to fill a list with items but as i said, I can't use listitems as I'm using a winform so I would like to know how I do that exactly. How do you check the items against the ID's against an empID in the database? Sianny aka Sharny
-
Thanks but the part you are talking about that is wrong, is actually the part that is correct and gives the correct value. I got that off msdn. Yes I want to fill a list with items but as i said, I can't use listitems as I'm using a winform so I would like to know how I do that exactly. How do you check the items against the ID's against an empID in the database? Sianny aka Sharny
If you have a list of items and want to fill the control with them, you should be able to set the control's DataSource property with the list, and it should work. That code is from MSDN ? It's REALLY messy, especially with the nested loop. Do you have a link ? I believe you, I'm just interested.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
If you have a list of items and want to fill the control with them, you should be able to set the control's DataSource property with the list, and it should work. That code is from MSDN ? It's REALLY messy, especially with the nested loop. Do you have a link ? I believe you, I'm just interested.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
I'm not sure what you mean but i think I have filled the list already as you said as the items are there. I just need the ticked items to save as correct ID's in the database against a given employee. I would've used bits and pieces of msdn stuff so that might be why its messy. I didn't copy a whole paragraph of code, just a line here and there.
public override 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); }
-
falles01 wrote:
for (i = 0; i <= (techSkillsCheckListBox2.Items.Count - 1); i++)
Why not i < techSkillsCheckListBox2.Items.Count ? Or even foreach (CheckBox box in techSkillsCheckListBox2.Items) ? This code is really broken. What you want to do, is fill a list with the items that are selected, then go through each check box and do something like checkbox.Checked = (myListOfItems.Contains(theKEyForThisCheckbox)); Of course, I just invented some variables, assume the Tag of the checkbox contains the key value, so you can look it up. The code you're presenting does a nested loop, and just sets each box to be checked, over and over again.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
falles01 wrote:
for (i = 0; i <= (techSkillsCheckListBox2.Items.Count - 1); i++)
Why not i < techSkillsCheckListBox2.Items.Count ? Or even foreach (CheckBox box in techSkillsCheckListBox2.Items) ? This code is really broken. What you want to do, is fill a list with the items that are selected, then go through each check box and do something like checkbox.Checked = (myListOfItems.Contains(theKEyForThisCheckbox)); Of course, I just invented some variables, assume the Tag of the checkbox contains the key value, so you can look it up. The code you're presenting does a nested loop, and just sets each box to be checked, over and over again.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )