locked
asp.net programming with VB RRS feed

  • Question

  • User96484794 posted

    Hi

    I've a problem with my program you see this program we have one DB and two table which is student and professor and a relationship between professor and student which is that we put professor id in student fields and connect professor id too another id in student table, the relation is 1:n. now we going to coding.

    ok in coding i add a drop down which show me professor names , i want when i choose one of them shown to me all student data that professor id exist in their table.

    i add a repeater for showing student data but it doesn't work

    here the code:

    Sub page_load()
    If Not IsPostBack Then
    Dim cn As Data.OleDb.OleDbConnection
    Dim cmd As OleDbCommand
    Dim rd As OleDbDataReader
    cn = New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\fred\Desktop\database.mdb")
    cmd = New OleDbCommand("select * from professor", cn)
    cn.Open()
    rd = cmd.ExecuteReader
    DropDownList2.DataSource = rd
    DropDownList2.DataTextField = "Lname"
    DropDownList2.DataValueField = "IDo"
    DropDownList2.DataBind()
    cn.Close()
    End If
    DropDownList2.Items.Insert(0, New ListItem("non select", -1))
    End Sub
    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim n As Integer
    n = DropDownList2.SelectedItem.Value
    If n <> -1 Then
    Dim cn As Data.OleDb.OleDbConnection
    Dim cmd As OleDbCommand
    Dim rd As OleDbDataReader
    cn = New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\fred\Desktop\database.mdb")
    cmd = New OleDbCommand("select * from student where IDo=@a", cn)
    cmd.Parameters.Add("@a", n)
    cn.Open()
    rd = cmd.ExecuteReader
    Repeater1.DataSource = rd
    Repeater1.DataBind()
    DropDownList2.DataBind()
    cn.Close()
    End If
    End Sub

    and code in repeater:

    <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
    <%#Container.DataItem("Fname")%>****
    </ItemTemplate>

    </asp:Repeater>

    Thursday, May 26, 2016 7:43 AM

Answers

  • User-2057865890 posted

    Hi faridphenom,

    i add a repeater for showing student data but it doesn't work

    Any error message? Please check if DropDownList AutoPostBack property is set to true.

    Dim n As Integer
    n = DropDownList2.SelectedItem.Value

    Cannot implicitly convert type 'string' to 'int'. It should be like this

    int n = 0;
    n = Convert.ToInt32(DropDownList1.SelectedItem.Value);

    Sample

    Html Markup

    <form id="form1" runat="server">
            <div>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                </asp:DropDownList>
            </div>
            <div>
                <asp:Repeater ID="Repeater1" runat="server">
                    <HeaderTemplate>
                        <table border="1">
                            <tr>
                                <th scope="col" style="width: 80px">Student Id</th>
                                <th scope="col" style="width: 180px">Student Name</th>
                                <th scope="col" style="width: 80px">Professor Id</th>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:Label ID="lblSId" runat="server" Text='<%# Eval("SId") %>' />
                            </td>
                            <td>
                                <asp:Label ID="lblSName" runat="server" Text='<%# Eval("SName") %>' />
                            </td>
                            <td>
                                <asp:Label ID="lblPid" runat="server" Text='<%# Eval("IDo") %>' />
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
        </form>

    Code behind

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }
    
            private void BindData()
            {
                Repeater1.DataSource = GetStudent();
                Repeater1.DataBind();
    
                DropDownList1.DataSource = GetProfessor();
                DropDownList1.DataTextField = "Lname";
                DropDownList1.DataValueField = "IDo";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("non select", "-1"));
            }
    
            public List<Professor> GetProfessor()
            {
                var list = new List<Professor>
                {
                    new Professor { IDo=1, Lname="ProfessorA" },
                    new Professor { IDo=2, Lname="ProfessorB" },
                    new Professor { IDo=3, Lname="ProfessorC" },
                    new Professor { IDo=4, Lname="ProfessorD" },
                    new Professor { IDo=5, Lname="ProfessorE" },
                };
                return list;
            }
    
            public List<Student> GetStudent()
            {
                var list = new List<Student>
                {
                    new Student {SId=11, SName="Student11-ProfessorA", IDo=1 },
                    new Student {SId=12, SName="Student12-ProfessorA", IDo=1 },
                    new Student {SId=13, SName="Student13-ProfessorA", IDo=1 },
                    new Student {SId=14, SName="Student14-ProfessorB", IDo=2 },
                    new Student {SId=15, SName="Student15-ProfessorB", IDo=2 },
                    new Student {SId=16, SName="Student16-ProfessorB", IDo=2 },
                    new Student {SId=17, SName="Student17-ProfessorC", IDo=3 },
                    new Student {SId=18, SName="Student18-ProfessorC", IDo=3 },
                    new Student {SId=19, SName="Student19-ProfessorD", IDo=4 },
                    new Student {SId=20, SName="Student20-ProfessorE", IDo=5 },
                };
                return list;
            }
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                int n = 0;
                n = Convert.ToInt32(DropDownList1.SelectedItem.Value);
                if (n != -1)
                {
                    var student = GetStudent().Where(x => x.IDo == n).ToList();
                    Repeater1.DataSource = student;
                    Repeater1.DataBind();
                    DropDownList1.DataBind();
                }
            }

    Class

    public class Professor
        {
            public int IDo { get; set; }
            public string Lname { get; set; }
        }
    
        public class Student
        {
            public int SId { get; set; }
            public string SName { get; set; }
            public int IDo { get; set; }
        }

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 27, 2016 5:34 AM