can i fill two dependent dropdownlist from single database table [Resolved]

Posted by Ankitsrist under ASP.NET on 12/13/2012 | Points: 10 | Views : 1811 | Status : [Member] | Replies : 23
actually my problem is looking funny but can i fill two dependent dropdownlist from single data base table means selection of the dropdownlist2 depend on the selected value in dropdownlist1....my table is SELECT [ID]
,[state]
,[project_name]
,[regional]
,[length]
,[minimum_length]
FROM [test].[dbo].[project_report]......help me




Responses

Posted by: Pavanandey on: 12/13/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
mydb.myconnection();
SqlCommand cmd = new SqlCommand("select id,length from project_report where project_name='"+DropDownList2.SelectedValue+"'", mydb .sqlcon );
SqlDataAdapter adp = new SqlDataAdapter(cmd );
DataSet ds = new DataSet();
adp.Fill(ds );
DropDownList3.DataSource = ds;
DropDownList3.DataTextField = ds.Tables[0].Columns[1].ToString();;
DropDownList3.DataValueField = ds.Tables[0].Columns[0].ToString();;
DropDownList3.DataBind();
}

try this , check if you are populating data into dataset also

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/13/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
see for
DropDownList1.DataTextField = "state_name"; // Textfield
DropDownList1.DataValueField = "ID"; //value filed


specfiy you query as follows
select id,length from project_report where project_name='" + DropDownList1.SelectedItem.Text +"'

Change in Query DropDownList2 to DropDownList1

this will help you now

Pavan Kumar

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/13/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Mark the answer if this helps you :)

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/13/2012 [Member] Bronze | Points: 25

Up
0
Down
Select * from project_report where state = dropdown1.selectedvalue

you can pass the value of the first drop down and get data and fill the second drop down

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/13/2012 [Member] Bronze | Points: 25

Up
0
Down
you can fill any no of dropdownlist from a single table... there is no limit for that

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/13/2012 [Member] Starter | Points: 25

Up
0
Down
pavan sir is this correct.....
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)

{
mydb.myconnection();
SqlCommand cmd = new SqlCommand("select * from project_report where project_name='"+DropDownList2.SelectedValue+"'", mydb .sqlcon );
SqlDataAdapter adp = new SqlDataAdapter(cmd );
DataSet ds = new DataSet();
adp.Fill(ds );
DropDownList3.DataSource = ds;
DropDownList3.DataTextField = "length";
DropDownList3.DataValueField = "ID";
DropDownList3.DataBind();

}


Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/13/2012 [Member] Bronze | Points: 25

Up
0
Down
yes.. it this working or facing any problem

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/13/2012 [Member] Starter | Points: 25

Up
0
Down
no sir it is not working.......



Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/13/2012 [Member] Bronze | Points: 25

Up
0
Down
what error are you facing, make sure the autopostback propoery of the first drop is true.

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/13/2012 [Member] Starter | Points: 25

Up
0
Down
is there any problem in table.....

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/13/2012 [Member] Starter | Points: 25

Up
0
Down
postback is set true sir.....but second dropdown not showing any result...

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/13/2012 [Member] Starter | Points: 25

Up
0
Down
my complete code is as follows.....

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.Drawing;

public partial class monthlytarget : System.Web.UI.Page
{
dbClass mydb = new dbClass();
SqlConnection sqlcon = new SqlConnection();

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
filldropdown();

fillgrid();
}


}
protected void Button1_Click(object sender, EventArgs e)
{
mydb.myconnection();
SqlCommand cmd = new SqlCommand("insert into monthlytarget(state, projectname, startdate, enddate) values('" + DropDownList1.SelectedItem.Text + "','" + DropDownList2.SelectedItem.Text + "','" + TextBox1.Text + "', '" + TextBox2.Text + "')", mydb.sqlcon);
cmd.ExecuteNonQuery();
GridView1.DataBind();
fillgrid();
}

public void filldropdown()
{
mydb.myconnection();
SqlCommand cmd = new SqlCommand("select * from state", mydb.sqlcon );
SqlDataAdapter adp = new SqlDataAdapter(cmd );
DataSet ds = new DataSet();
adp.Fill(ds );
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "state_name";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem ("--select--", "0"));

SqlCommand cmd1 = new SqlCommand("select * from project_report", mydb.sqlcon);
SqlDataAdapter adp1 = new SqlDataAdapter(cmd1 );
DataSet ds1 = new DataSet();
adp1.Fill(ds1 );
DropDownList2.DataSource = ds1;
DropDownList2.DataTextField = "project_name";
DropDownList2.DataValueField = "ID";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem ("--select--","0"));
}


public void fillgrid()
{
mydb.myconnection();
string query = "SELECT [state],[projectname],[startdate],[enddate] FROM [test].[dbo].[monthlytarget]";
SqlDataAdapter adp = new SqlDataAdapter(query, mydb.sqlcon);
DataSet ds = new DataSet();
adp.Fill(ds, "table0");
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
mydb.myconnection();
SqlCommand cmd = new SqlCommand("select * from project_report where project_name='"+DropDownList2.SelectedValue+"'", mydb .sqlcon );
SqlDataAdapter adp = new SqlDataAdapter(cmd );
DataSet ds = new DataSet();
adp.Fill(ds );
DropDownList3.DataSource = ds;
DropDownList3.DataTextField = "length";
DropDownList3.DataValueField = "ID";
DropDownList3.DataBind();

}
}


Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/13/2012 [Member] Bronze | Points: 25

Up
0
Down
Check where the DropDownList2.SelectedValue is a project name your are passing in

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/13/2012 [Member] Bronze | Points: 25

Up
0
Down
Mark the answer if this helps you

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/13/2012 [Member] Starter | Points: 25

Up
0
Down
sorry sir but i tried ur code also but nothing is happening....

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/13/2012 [Member] Bronze | Points: 25

Up
0
Down
1) is the post back happening
2) is the DropDownList2_SelectedIndexChanged being called
3) is the dataset filled

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/13/2012 [Member] Starter | Points: 25

Up
0
Down
means sir i not getting ur words "Check where the DropDownList2.SelectedValue is a project name your are passing in"

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/13/2012 [Member] Starter | Points: 25

Up
0
Down
yes sir first two point working properly but how to check third point

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/13/2012 [Member] Bronze | Points: 25

Up
0
Down
see for
DropDownList1.DataTextField = "state_name"; // Textfield
DropDownList1.DataValueField = "ID"; //value filed


specfiy you query as follows
select id,length from project_report where project_name='" + DropDownList1.SelectedItem.Text +"'

this will help you now


Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/13/2012 [Member] Bronze | Points: 25

Up
0
Down
mark this if this helps you

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/13/2012 [Member] Starter | Points: 25

Up
0
Down
i filled it already....in this way
DataSet ds = new DataSet();

adp.Fill(ds);
DropDownList3.DataSource = ds;


Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/13/2012 [Member] Starter | Points: 25

Up
0
Down
sir.....which words i should use to thank u.....u jst rock....thanks a lot,....its now working....

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/13/2012 [Member] Starter | Points: 25

Up
0
Down
ohh definitely sir......:) this is the second tym u made my problem solved

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response