Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i hv a button,dropdownlist,and gridview. and i want that when i select any option dropdownlist . then the releted data is show into gridview .i hv a problem that tha data show in gridview but gridview d'nt affect with the data list..
my code is

C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Net.Mail;
using System.IO;
using System.ComponentModel;
using System.Drawing;
using System.Web.SessionState;

namespace AccountManagement
{
    public partial class Account : System.Web.UI.Page
    {
        SqlConnection con;
       SqlDataAdapter da;
        SqlDataReader dr;
        //DataSet ds1;
       // String Sql;
        //int Count;
        DataTable dt;
        SqlCommand cmd;
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            con = new SqlConnection(s);
            if (!IsPostBack)
            {
                bindgrid();
                bindlst();
            }
            con.Open();
        }

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    bindgrid();
        //    GridView1.Visible = true;
            
        //}
        public void bindgrid()
        {
            cmd=new SqlCommand ("select distinct Fname,Lname from Client_registration_tbl",con);
            //ds1 = new DataSet();
            con.Open();
            dr = cmd.ExecuteReader();
            //da.Fill(ds1);
            GridView1.DataSource = dr;
            GridView1.DataBind();
            GridView1.Visible = true;
            con.Close();

        }

        public void bindlst()
        {
            da = new SqlDataAdapter("select distinct Fname,Lname from Client_registration_tbl", con);
            dt = new DataTable();
            da.Fill(dt);
            DropDownList1.DataSource = dt;
            DropDownList1.DataBind();
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select distinct Fname,Lname from Client_registration_tbl" + DropDownList1.SelectedItem.Text + "", con);
            dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
           // bindgrid();
            //GridView1.Visible = true;
        }

      


    }
}
Posted
Updated 13-Sep-12 21:25pm
v3
Comments
Vani Kulkarni 14-Sep-12 1:42am    
You have already posted this question here[^].Please do not repost. Update your existing question using Improve Question.

1 solution

2 Options:
1. Use Dropdown selectedIndexchange event (with AutoPostback set to true) on server side to find the value selected in the dropdownlist. based on the value selected, get the data from database and display in a grid.
2. Use AJAX to fire a XMLHttpRequest/Callback/PageMethod from client side to server on drodown change and get the data back as a response which can be used to bind with the XML.

Try both and learn on.

Option 1 is straightforward. Option 2 needs knowledge on ways to interact with server form client, read here:
1. XMLHttpRequest[^]
2. Callback[^]
3. WebService call[^]
4. PageMethod[^]
 
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