Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Can anyone please help me out how to fill grid view based on dropdownlist value. Iam new to dotnet
my program:

Design Page:
ASP.NET
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<table>
<tr>
<td>Empno: </td> <td></td>
<td>
    <asp:DropDownList ID="emplist" runat="server" DataSourceID="emplist1" 
        DataTextField="empCode" DataValueField="empCode">
    </asp:DropDownList>
    <asp:SqlDataSource ID="emplist1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:euroTransConnectionString3 %>" 
        SelectCommand="SELECT DISTINCT [empCode] FROM [eurEvents_activity]">
    </asp:SqlDataSource>
    </td>
    <td></td>
  <td>
 
    <asp:DropDownList ID="atddroplist" runat="server" AutoPostBack="true">
        
        <asp:ListItem Selected="True" Value="1">Current Week</asp:ListItem>
        <asp:ListItem Value="2">Last Week</asp:ListItem>
        <asp:ListItem Value="3">Current Month</asp:ListItem>
        <asp:ListItem Value="4">Last Month</asp:ListItem>
        <asp:ListItem Value="5">Custom</asp:ListItem>
    </asp:DropDownList>

    </td>
    </tr>
    </table>
    <br />
    <asp:GridView ID="GridView1" AutoGenerateColumns="False"  runat="server">
    <Columns>

        <asp:BoundField DataField="EmpCode" HeaderText="EmpCode" />

        <asp:BoundField DataField="EventDate" HeaderText="EventDate" />

        <asp:BoundField DataField="InTime" HeaderText="InTime" />

        <asp:BoundField DataField="OutTime" HeaderText="OutTime" />

        </Columns>
    </asp:GridView>

   
</asp:Content>




My Business logic is:

namespace empatdList
{
    public class empatdListBI
    {
        # region Local Variables

        DbConnection connection = new DbConnection();

        DataTable tempData;
        // DataSet tempData1;
        SqlCommand command;
        SqlDataAdapter adapter;

        # endregion

        public DataTable LoadRecords(string empcode)
        {
            //This function used to Load QueryReport for the respective Fields
            try
            {
                command = new SqlCommand("sp_InOutDetails_LastMonth");
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@EmpCode", SqlDbType.VarChar).Value = empcode;
                

                adapter = new SqlDataAdapter(command);
                tempData = new DataTable();
                //tempData1 = new DataSet();
                command.Connection = connection.OpenConnection();
                adapter.Fill(tempData);
                command.ExecuteNonQuery();
            }

            catch (System.Data.SqlClient.SqlException ex)
            {
                HttpContext.Current.Session["Error"] = ex;

            }

            finally
            {
                connection.CloseConnection();
            }

            return tempData;

        }
    }
}


My source code is:


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;

namespace empatdList
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                binddropdown();
            }
        }

        protected void binddropdown()
        {
            empatdListBI c = new empatdListBI();
            DbConnection b = new DbConnection();
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            b.OpenConnection();
            dt = c.LoadRecords(emplist.Text);

            GridView1.DataSource = dt;
            GridView1.DataBind();
            b.CloseConnection();
        }




    }
}


I had kept database connection in separate class. Please help me out where i am making mistake
Posted
Updated 11-Apr-16 18:20pm
v2
Comments
Arvind Zamakia 12-Apr-16 0:14am    
its working fine

Use the OnSelectedIndexChanged event from the DropDownList object.
See: here[^]
 
Share this answer
 
XML
<html>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:DropDownList ID="ddlCountry" runat="server" DataSourceID="sds_Countries"
            DataTextField="Country" DataValueField="Country" AutoPostBack="True">
        </asp:DropDownList>
        <asp:SqlDataSource ID="sds_Countries" runat="server"
            ConnectionString="<%$ ConnectionStrings:mgoloisConnectionString %>"
            SelectCommand="SELECT Distinct [Country] FROM [ContactForm] ORDER BY [Country]">
        </asp:SqlDataSource>
        <asp:GridView ID="GridViewPeople" runat="server" AutoGenerateColumns="False"
            DataSourceID="sds_People">
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"/>
                <asp:BoundField DataField="LastName" HeaderText="LastName"/>
                <asp:BoundField DataField="Email" HeaderText="Email" />
                <asp:BoundField DataField="Address" HeaderText="Address" />
                <asp:BoundField DataField="City" HeaderText="City"/>
                <asp:BoundField DataField="State" HeaderText="State" />
                <asp:BoundField DataField="Country" HeaderText="Country"/>
            </Columns>
        </asp:GridView>

        <asp:SqlDataSource ID="sds_People" runat="server"
            ConnectionString="<%$ ConnectionStrings:mgoloisConnectionString %>"
            SelectCommand="SELECT [FirstName], [LastName], [Email], [Address],
            [City], [State], [Country] FROM [ContactForm]
            WHERE ([Country] = @Country) ORDER BY [LastName]">
            <SelectParameters>
                <asp:ControlParameter ControlID="ddlCountry" Name="Country"
                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

    </div>
    </form>
</body>
</html>
 
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