Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a slight confusion...I have two Drop down list and the values for one of the drop down list are derived from the database(I have written a C# code for it),Now what i want to do is ,when a particular value is selected from the first drop down list I want the second Drop down list to display only a few values which should be derived from another database.
for instance if first drop down list has departments like EC,CS etc. and the second drop down list has names,When I select Ec,all the names belonging to EC department should appear in the second drop down list.is this possible?...I managed to write few lines of code...

C#
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;
using System.Data.OleDb;

public partial class Forms_Master_Forms_Temporary_duty_form : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Server=SOFTDEV1; Database=ADE_2903_SOFTDEV; Integrated security = true");
    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList7.Visible = false;
        
        if (!IsPostBack)
        {
            GetData();
            txtdate.Text = DateTime.Now.ToShortDateString();//get the current date into the textbox
            //adding the states  in the drop down box
            add_states();

        }
    }
   

    protected void GetData()
    {
       
       
        SqlCommand cmd = new SqlCommand("select * from M_Division ", conn);
        SqlDataAdapter sd = new SqlDataAdapter(cmd);
        conn.Open();
        DataTable dt = new DataTable();
        sd.Fill(dt);
        DropDownList3.DataSource = dt;
        DropDownList3.DataTextField = "DivShort";
        DropDownList3.DataValueField = "DivShort";
        DropDownList3.DataBind();

        
        conn.Close();
    }

This is only to bind data to drop down list..Well I am not sure how to get values into the second drop down list from database when the first value is selected from the first database.
Posted
Updated 21-Jul-13 21:20pm
v2

 
Share this answer
 
I will tell you the answer as logic not code.. all what I will say you will find in .net visual studio and C# and it will work

first: go to the properties of DropDownList3 and set it as autopostback

then: go to the DropDownList3 event onSelectedIndexChanged and write the code inside

if (DropDownList3.selectedValue>0)
{
DropDownList7.visible=true;
fillDropDownList7();
}

Test it and ask me if you need anything.

Best Regards,
Ibrahim Karakira
 
Share this answer
 
Comments
Member 10066916 24-Jul-13 4:57am    
yes I am able to binda data to drop down box from the database. ...what I am not able to do is...when a certain item is selected from one dropdown box(these items are got from a database) it should display few items in the second drop down box from a different database..
Member 10066916 24-Jul-13 5:18am    
I have again re written the code...can someone please tell me where I am going wrong??

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;
using System.Data.OleDb;

public partial class Forms_Master_Forms_Temporary_duty_form : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("Server=SOFTDEV1; Database=ADE_2903_SOFTDEV; Integrated security = true");
protected void Page_Load(object sender, EventArgs e)
{
DropDownList7.Visible = false;

if (!IsPostBack)
{
GetData();

}
}


protected void GetData()
{


SqlCommand cmd = new SqlCommand("select divid from M_Division ", conn);
SqlDataAdapter sd = new SqlDataAdapter(cmd);
conn.Open();
DataTable dt = new DataTable();
sd.Fill(dt);
DropDownList3.DataSource = dt;
DropDownList3.DataTextField = "DivShort";
DropDownList3.DataValueField = "Divid";
DropDownList3.DataBind();


conn.Close();
}




protected void addname(int divid)
{

SqlCommand cmd = new SqlCommand("select emp_name from M_Emp_Personal where DivisionID=@divisionId", conn);

SqlDataAdapter sd = new SqlDataAdapter(cmd);


conn.Open();
DataTable dt = new DataTable();
sd.Fill(dt);
DropDownList4.DataSource = dt;
DropDownList4.DataTextField = "EmpName";
DropDownList4.DataValueField = "divid";
DropDownList4.DataBind();
conn.Close();
}


protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
int divid=Convert.ToInt32(DropDownList3SelectedValue.ToString());
addname(int divid);
}
}
Hi,
In this case you need to use Selected Index Change event for the first Dropdwon list.Check the link below for more details.Hope this will help you.

http://www.dotnetfunda.com/forums/thread2590-filling-a-dropdownlist-on-selectedindexchanged-of-another-dropdownlist.aspx[^]

Check Solution 2 from here.

DropDownList and capturing values after SelectedIndexChanged event[^]
 
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