Click here to Skip to main content
15,908,274 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi.

I'm trying to make a dropdown list with a database using sql server on asp.net with codebehind vb.

this's the design.

| No | Name | Year | Address |
| 1 | Alex | 2010 | Moscow |
| 2 | Jack | 2011 | Berlin |
| 3 | Black | 2010 | Tokyo |

When i choose 2010 on dropdown list then show :

| No | Name | Year | Address |
| 1 | Alex | 2010 | Moscow |
| 3 | Black | 2010 | Tokyo |

Please help me..

Thanks
Posted

In aspx set
HTML
autopostback="true"
property to dropdown.

and in code behind

In dropdown selected index changed event write the query to get your results
C#
string query="select * from tablename where year="+Convert.ToInt32(ddlyear.selectedItem.text);


then connect to database and get records and then bind that dataset to gridview.
 
Share this answer
 
1. Load the "DropDownList1"

protected void Page_Load(object sender, EventArgs e)
       {

           try
           {
               if (!Page.IsPostBack){
                   using (SqlConnection oConn = new SqlConnection()){
                       oConn.ConnectionString = "data source=localhost;initial catalog=test_db;user=sa;password=123";
                       SqlDataAdapter oda = new SqlDataAdapter("select distinct year  from Acadamics", oConn);
                       DataSet ds = new DataSet();

                       oda.Fill(ds);
                       DropDownList1.DataSource = ds.Tables[0];
                       DropDownList1.DataValueField = "year";
                       DropDownList1.DataTextField = "year";
                       DropDownList1.DataBind();

                   }
               }

           }
           catch (Exception ex)
           {

               Response.Write(ex.Message);
           }
       }


2. Change the
"DropDownList1"
property to
autopostback="true"


3. Populate the grid on the DropdownList selected index changed event.

protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
       {
           try
           {
               using (SqlConnection oConn = new SqlConnection())
               {
                   oConn.ConnectionString = "data source=localhost;initial catalog=test_db;user=sa;password=123";
                   SqlDataAdapter oda = new SqlDataAdapter("select *  from Acadamics where year=" + DropDownList1.SelectedValue.ToString(), oConn);
                   DataSet ds = new DataSet();

                   oda.Fill(ds);
                   GridView1.DataSource = ds.Tables[0];
                   GridView1.DataBind();

               }

           }
           catch (Exception ex)
           {

               Response.Write(ex.Message);
           }
       }


try to pass your combo box value to the grid population method using parameters to avoid any SQL injection.
 
Share this answer
 
v2
Collect all data in data table and then filter by selected item from dropdownlist using rowfilter.
 
Share this answer
 
Thanks All..

Problem has been resolved.. :)
 
Share this answer
 
U can use an ordinary listview. Select data into datatable, loop through each row reading the value in each column pad it right appropriately. Concatenate values from one row into a string variable and add the variable to the list view.
 
Share this answer
 
Use SqlDataSource control and configure it from smart tag in design mode. Mention the where condition and bind it to the drop down list
 
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