C# Jadu
  • Home
  • Asp.net
  • Web Design
    • Html
    • Css
    • Java Script
  • Bootstrap
  • Sql
    • Queries
    • Stored Procedures
  • About Me
    • About Myself
    • My Projects
    • My CV
  • Gallery
  • Do you Know?

Tuesday, 22 September 2015

how to find results by click on dropdownlist selected value into a grid view?

 Deepak Chaudhary     05:12     ASP.NET     2 comments   

Hi Friends...

Here I explain full procedure step by step 

Step 1: create a connection string in web.config  page


 <connectionStrings>
    <add name="conn" providerName="System.Data.SqlClient"
    connectionString="Data Source=DcPc;uid=sa;pwd=12345;database=droplist"></add>
  </connectionStrings>


Step 2: create database  

create a table UserDetail 


 set  primary key column ID and auto increment by +1 using sql properties 


Step 3: HTML Design code goes here

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="droplist.aspx.cs" Inherits="droplist" %>


<!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 id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div style="border: solid 2px #800000; width: 300px; height: auto; margin-left: 500px;

        margin-top: 100px; padding: 25px;">

        <div>

            <b style="color: #800000">Select Data From DataList

                <br />

            </b>

            <br />

            <asp:DropDownList ID="ddlSelectData" runat="server" OnSelectedIndexChanged="ddlSelectData_SelectedIndexChanged"

                BackColor="#993333" ForeColor="White" Height="29px" Width="101px">

            </asp:DropDownList>

            <asp:Label ID="lblFindResult" runat="server" Text="Label" Visible="false"></asp:Label>

            <br />

            <br />

            <asp:Button ID="btnFindResult" runat="server" Text=" Find Result" OnClick="btnFindResult_Click"

                Style="height: 26px" BackColor="#993333" ForeColor="White" />

            <br />

            <br />

            <asp:GridView ID="gvResult" runat="server" BackColor="White" BorderColor="#CC9966"

                BorderStyle="None" BorderWidth="1px" CellPadding="4" Height="16px" Width="278px">

                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />

                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />

                <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />

                <RowStyle BackColor="White" ForeColor="#330099" />

                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />

                <SortedAscendingCellStyle BackColor="#FEFCEB" />

                <SortedAscendingHeaderStyle BackColor="#AF0101" />

                <SortedDescendingCellStyle BackColor="#F6F0C0" />

                <SortedDescendingHeaderStyle BackColor="#7E0000" />

            </asp:GridView>

        </div>

    </div>

    </form>

</body>

</html>


Step 4: Add some header files


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;


Step 5:   c# page code goes here


public partial class droplist : System.Web.UI.Page
{

    string str = "Data Source=
DcPc;uid=sa;pwd=12345;database=droplist";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection(str);

            string com = "Select * from UserDetail";

            SqlDataAdapter adpt = new SqlDataAdapter(com, con);

            DataTable dt = new DataTable();

            adpt.Fill(dt);

            ddlSelectData.DataSource = dt;

            ddlSelectData.DataBind();

            ddlSelectData.DataTextField = "Name";

            ddlSelectData.DataValueField = "ID";

            ddlSelectData.DataBind();

        }
    }

    protected void btnFindResult_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(str);

        SqlCommand cmd = new SqlCommand("select * from UserDetail where id = '" + ddlSelectData.SelectedValue + "'", con);

        SqlDataAdapter Adpt = new SqlDataAdapter(cmd);

        DataTable dt = new DataTable();

        Adpt.Fill(dt);

        gvResult.DataSource = dt;

        gvResult.DataBind();
      
        lblFindResult.Visible = true;

        lblFindResult.Text = "record found";

    }

    protected void ddlSelectData_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

Demo :

 

 





 

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Home

2 comments:

  1. Unknown23 September 2015 at 02:55

    Thanks dear its Useful Code for my problem

    ReplyDelete
    Replies
    1. Deepak Chaudhary24 September 2015 at 05:18

      Your welcome...

      Delete
      Replies
        Reply
    2. Reply
Add comment
Load more...

Most Popular posts

  • How To Create Image wipe Effect With CSS3 Webkit Animation
    Hi Friends... In this article we learn how to create a Image wipe effect with CSS3. CSS transition and animations provide a way for web de...
  • CSS 3D Cube
    Hi Friends... CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a lang...
  • CSS 3D Transforms
    Hi Friends... There is nothing more flashy and full with eye-candy than an impressive 3D CSS demo. Although its utility outside of demos ...

About Me

Deepak Chaudhary
View my complete profile

Copyright © C# Jadu | Powered by Blogger
Design by Deepak Chaudhary