Click here to Skip to main content
14,383,503 members

How to display two tables data in single datalist by select index change in dropdownlist in C#

Member 14631509 asked:

Open original thread
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FinalStudentDetail.aspx.cs" Inherits="FinalStudentDetail" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <th colspan="3">Achievement Records</th>
                </tr>
                <tr>
                    <td>Select Student</td>
                    <td>:</td>
                    <td>
                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"></asp:DropDownList>

                    </td>
                </tr>
                
            </table>
           
             <asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" OnItemDataBound="DataList1_ItemDataBound">
                 <ItemTemplate>
                     <table border="1">
                         <tr>
                             <td colspan="2">

                                 <asp:Label ID="lb_id" Text='<%#Eval("exmNam") %>' runat="server" />
                             </td>
                         </tr>
                         
                
                
                       <tr>
                             <td><%# Eval("scrSubName") %></td>
                             <td><%# Eval("scrAch") %></td>
                         </tr>
                         </table>
                 </ItemTemplate>
                 <AlternatingItemTemplate>
                     <table border="1">
                         <tr>
                             <td colspan="2">
                                 <asp:Label ID="lb_id" Text='<%#Eval("exmNam") %>' runat="server" />
                             </td>
                         </tr>
                         <tr>
                             <th>Subject Name</th>
                             <th>Achievement</th>
                         </tr>
                
                
                       <tr>
                             <td><%# Eval("scrSubName") %></td>
                             <td><%# Eval("scrAch") %></td>
                         </tr>
                         </table>
                 </AlternatingItemTemplate>
                 <FooterTemplate>
                     </table>
                 </FooterTemplate>
             </asp:DataList>
        </div>
    </form>
</body>
</html>



<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class FinalStudentDetail : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();
    protected void Page_Load(object sender, EventArgs e)
    {
        con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
            Response.Write(@"<script language='javascript'>alert('Connection is open')</script>");
        }
        if (Page.IsPostBack == false)
        {
            DropDownList1_Bind();
            
        }

    }
    public void DropDownList1_Bind()
    {
        SqlCommand cmd = new SqlCommand("select * from tbStu", con);
        SqlDataReader sdr = cmd.ExecuteReader();
        DropDownList1.DataValueField = "stuCod";
        DropDownList1.DataTextField = "stuNam";
        DropDownList1.DataSource = sdr;

        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, "Select Student");
        sdr.Close();
        cmd.Dispose();

    }
    
    public void DataList_Bind()
    {
       String query;
        query= "select distinct exmNam from tbExm;select stuNam, exmNam,scrSubName,scrAch from tbScr,tbStu,tbExm where scrStuCod=stuCod and scrExmCod=exmCod and stuCod=@Cod";
      
        SqlDataAdapter sda = new SqlDataAdapter(query,con);
        sda.SelectCommand.Parameters.AddWithValue("@Cod", DropDownList1.SelectedValue);
        DataSet ds = new DataSet();
        sda.Fill(ds);

        DataList1.DataSource = ds;
        DataList1.DataBind();
       
       
    }
    
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
     
        DataList_Bind();
    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
    }

   
   
}


What I have tried:

problem is one table data is distinct and second table data based on columnswise and format should be same which i have mentioned above.
Tags: C#, ASP.NET

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100