Click here to Skip to main content
14,920,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a project right now,and i need to allocate students populated in a checkedControlBox 2 to a lecturer populated in a checkedControlBox1,like a one to many relationship...please i don't know how to do this effectively,i will appreciate answers..thanks


this is something i have tried but its not saving to the database,checkedListBoxControl2 populates lecturers from tbl_CSLecturer and checkedListBoxControl1 populates students from tbl_Student

for the button click event i have
C#
       try
        {
            PMSLogic code = new PMSLogic();
            //string userId = main.userId;
            foreach (int i in this.checkedListBoxControl2.SelectedIndices)
            {
                int lecturerID = Convert.ToInt32(checkedListBoxControl2.GetItemValue(i));
                code.DeleteBindLecturerBylecturerId(lecturerID);
                foreach (int j in this.checkedListBoxControl1.SelectedIndices)
                {
                    int studentId = Convert.ToInt32(checkedListBoxControl1.GetItemValue(j));
                    tbl_Allocation tblalloc = new tbl_Allocation();
                    tblalloc.SessionID = Convert.ToInt32(this.comboBoxEx2.SelectedValue);
                    tblalloc.LecturerID = lecturerID;
                    tblalloc.StudentID = studentId;
                    tblalloc.DateAllocated = System.DateTime.Now;

                }
            }
            RadMessageBox.SetThemeName("Aqua");
            RadMessageBox.Show("Supervisor Successfully Allocated to Selected Student(s)", "Bind Lecturer To Student", MessageBoxButtons.OK, RadMessageIcon.Info, MessageBoxDefaultButton.Button1);
        }
        catch (Exception ex)
        {
        }




        private void checkedListBoxControl2_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            PMSLogic code = new PMSLogic();
            //int roleId =
            //DataTable ItemRoleT = this.checkedListBoxControl2.SelectedIndex.ToString();
            for (int i = 0; i < checkedListBoxControl1.ItemCount; i++)
            {
                this.checkedListBoxControl1.SetSelected(i, false);
            }
            if (ItemRoleT.Rows.Count > 0)
            {
                foreach (DataRow row in ItemRoleT.Rows)
                {
                    string itemText = row.ItemArray[1].ToString();
                    int menuCount = this.checkedListBoxControl1.ItemCount;
                    for (int i = 0; i < menuCount; i++)
                    {
                        string listText = checkedListBoxControl1.GetItemText(i);
                        if (itemText.Equals(listText))
                        {
                            this.checkedListBoxControl1.SetSelected(i, true);
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < checkedListBoxControl1.ItemCount; i++)
                {
                    this.checkedListBoxControl1.SetSelected(i, false);
                }
            }
        }
        catch (Exception ex)
        { }
    }
}
Posted
Updated 21-May-14 7:39am
v2
Comments
Maciej Los 22-May-14 12:56pm
   
What you mean by: i need to allocate students populated in a checkedControlBox 2 to a lecturer populated in a checkedControlBox1?

Hello friend, I'm trying to provide a solution as far as I understood the requirement. I believe apart from tbl_Lecturer and tbl_Student tables, you have introduced a third table to hold the one-to-many relationship. Let's say the new table name is tbl_Lecturer_Student.

I also assume that one Lecturer can be selected at a time hence ListBox control would be better choice than a CheckBoxList control for Lecturer. CheckBoxList is fine for Students.

Last assumption and not the least, this UI would be used for Saveing relationship among Lecturer and Students.

Here we go:

XML
<table style="width: 50%;" border="1">
                <tr>
                    <th>Lecturer:</th>
                    <th>Student:</th>
                </tr>
                <tr>
                    <td>
                        <asp:Panel runat="server" ScrollBars="Vertical" Height="150px">
                            <asp:ListBox ID="lstLecturer" runat="server" Width="100%">
                                <asp:ListItem Value="1">Lecturer1</asp:ListItem>
                                <asp:ListItem Value="2">Lecturer2</asp:ListItem>
                                <asp:ListItem Value="3">Lecturer3</asp:ListItem>
                                <asp:ListItem Value="4">Lecturer4</asp:ListItem>
                                <asp:ListItem Value="5">Lecturer5</asp:ListItem>
                                <asp:ListItem Value="6">Lecturer6</asp:ListItem>
                            </asp:ListBox>
                        </asp:Panel>
                    </td>
                    <td style="height: 100px;">
                        <!-- added Panel to show vertical scrollbar -->
                        <asp:Panel runat="server" ScrollBars="Vertical" Height="150px">
                            <asp:CheckBoxList ID="cblStudent" runat="server" Width="100%">
                                <asp:ListItem Value="1">Student-1</asp:ListItem>
                                <asp:ListItem Value="2">Student-2</asp:ListItem>
                                <asp:ListItem Value="3">Student-3</asp:ListItem>
                                <asp:ListItem Value="4">Student-4</asp:ListItem>
                                <asp:ListItem Value="5">Student-5</asp:ListItem>
                                <asp:ListItem Value="6">Student-6</asp:ListItem>
                                <asp:ListItem Value="7">Student-7</asp:ListItem>
                                <asp:ListItem Value="8">Student-8</asp:ListItem>
                                <asp:ListItem Value="9">Student-9</asp:ListItem>
                                <asp:ListItem Value="10">Student-10</asp:ListItem>
                            </asp:CheckBoxList>
                        </asp:Panel>
                    </td>
                </tr>
            </table>
            <p>
                <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
            </p



C#
protected void btnSave_Click(object sender, EventArgs e)
        {
            foreach (ListItem lecturer in lstLecturer.Items)
            {
                if (lecturer.Selected == true)
                {
                    // call method to delete all the existing entries from tbl_Lecturer_Student by LecturerID
                    // DeleteLecturerStudentRelationByLecturerID(lecturer.Value)

                    foreach (ListItem student in cblStudent.Items)
                    {
                        if (student.Selected == true)
                        {
                            // code to insert Lecturer - Student relation in a table
                            // InsertLecturerStudentRelation(lecturer.Value, student.Value);
                        }
                    }

                    break; // there could be one lecturer selected at a time
                }
            }
        }


Let me know if you have any further query. I'll be happy to help you :)
   
Alternatively, you may visit the below article:

Implementing One-To-Many Relationship in ASP.NET[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900