Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hai Sir,
I have one table with name Subject and two columns SubId,SubName and Year .In this table I added multiple subject names based on year.

From this, in front end design there is a dropdown list having binding Year rows from Subject Table from Database. When I select particular year the subject names should come in Labels only.

How can I write code for this requirement?
Posted
Updated 21-May-15 1:47am
v2
Comments
[no name] 21-May-15 8:05am    
What you have tried?
Sinisa Hajnal 21-May-15 8:21am    
Do you mean you have multiple rows like this
subject year
A 2015
B 2015
C 2015
D 2014
A 2014

Or like this
subject year
a,b,c 2015
d, a 2014

If it is first kind (I hope so) - then I don't understand what the problem is. You get list of rows, add same number of labels (or better yet, use datalist, repeater or any other list in ASP.NET) and you're done.

If it is the second...well, redesign :) If you cannot for some reason, split it as needed and again do as per list version.

May be this will help you out.Onselectindexchange event of drop-downlist

C#
using (SqlCommand cmd = new SqlCommand("select * from tablename where year='"+dropdownlist.SelectedValue+"'", CONN)) 
            {
                cmd.ExecuteNonQuery();
                using (SqlDataAdapter sqd = new SqlDataAdapter(cmd))
                {
                    DataTable dtq = new DataTable();
                    sqd.Fill(dtq);
                    if (dtq.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtq.Rows.Count; i++)
                        {
                            if (lbltext.Text != "")// lbltext is the id of Label
                            {
                                lbltext.Text = lbltext.Text + ", " + dtq.Rows[i]["name"].ToString();
                            }
                            else
                            {
                                lbltext.Text = dtq.Rows[i]["name"].ToString();
                            }
                        }
                    }

                }
            }
 
Share this answer
 
C#

C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (DBEntities dc = new DBEntities())
                {
                    ddlYear.DataSource = dc.Subjects.Select(s => s.Year).Distinct().ToList();
                    ddlYear.DataBind();
                }
            }
        }

        protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            int year = Convert.ToInt32(ddlYear.Text);
            using (DBEntities dc = new DBEntities())
            {
                ListViewSubjects.DataSource = dc.Subjects.Where(s=> s.Year == year) .Distinct().ToList();
                ListViewSubjects.DataBind();
            }
        }



ASPX



XML
<form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlYear_SelectedIndexChanged"></asp:DropDownList>
        <br />
        <asp:ListView ID="ListViewSubjects" runat="server">
            <ItemTemplate>
                  <asp:Label ID="NameLabel" runat="server"
          Text='<%#Eval("Name") %>' />
            </ItemTemplate>
        </asp:ListView>
    </div>
    </form>
 
Share this answer
 

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