connectseekers.com

Saturday, 27 December 2014

Populate Data in GridView on DropdownList Selected Role in Asp.net Using C#.net

12/27/2014 - By Pranav Singh 2



This article will show you how you can populate data in GridView on dropdown List selected role in asp.net using c#.net.


So for this article first we will create a table in out sql db.



After creating sql table we will create a new asp.net application and add the below code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Populate Data in GridView on DropdownList Selected Role in Asp.net Using C#.net
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <table width="100%" cellpadding="4" cellspacing="4">
        <tr>
            <td align="center">
                <b>USER DETAIL</b>
            </td>
        </tr>
        <tr>
            <td align="center">
                <asp:DropDownList ID="DropDownList1" runat="server" Width="91px">
                    <asp:ListItem>Select</asp:ListItem>
                    <asp:ListItem>Admin</asp:ListItem>
                    <asp:ListItem>User</asp:ListItem>
                </asp:DropDownList>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Search" />
            </td>
        </tr>
        <tr>
            <td align="left">
                <asp:GridView ID="GridView1" runat="server" Width="100%">
                </asp:GridView>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

Now we will add the below code in your code behind page.
using System;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridView("Select");
            }
        }
        private void BindGridView(string userType)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.
ConnectionStrings["con"].ToString());
            try
            {
                DataTable objdt = new DataTable();
                string query = "";
                if (userType == "Select")
                {
                    query = "select * from UserDetail;";
                }
                else
                {
                    query = "select * from UserDetail where UserType='" + userType + "';";
                }
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                con.Open();
                da.Fill(objdt);
                con.Close();
                if (objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = objdt;
                    GridView1.DataBind();
                }
            }
            catch
            {
                con.Close();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BindGridView(DropDownList1.SelectedItem.ToString());
        }
    }
}

In above code I have created a method in which I have passed the user type to prepare the filter query and getting the data on the bases of user type.
Now we have done run the application and check the output.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

  1. i applied your code to display gridview on the selection of dropdown list . but it enters into infinite loop.

    ReplyDelete
    Replies
    1. Hi please let me know what the code you have used. please show me your code.

      Delete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top