how to retrive the values from one dropdownlist to another

Posted by Saritha.rajeshkumar under C# on 4/22/2011 | Points: 10 | Views : 3019 | Status : [Member] | Replies : 2
hi
this is saritha,
how to retrive the values from one dropdownlist to another
i.e when i select the value in first dropdownlist the second dropdownlist should automatically change in 3tier architecture with stored procedures.




Responses

Posted by: Prabhakar on: 4/22/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Saritha.rajeshkumar

write on code First DropdownList on Selectindechanged Event

protected void firstddl_SelectedIndexChanged(object sender, EventArgs e)

{
string value1;
value1=firstddl.SelectedItem.Text;
Sescondddl.Items.Insert(0,value1);
}




Try It . .

Best Regard's
Prabhakar

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

Posted by: Jiturcm on: 4/22/2011 [Member] Starter | Points: 25

Up
0
Down
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=ORCHE5;Initial Catalog=test;Integrated Security=True;Password=sqlserver2005");
protected void Page_Load(object sender, EventArgs e)
{

BindState();


}
public void BindState()
{
DataTable dt = new DataTable();
try
{
con.Open();
SqlCommand cmd = new SqlCommand("p_state", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Name";
DropDownList1.DataBind();
//DropDownList1.Items.Insert(0, new ListItem("select state", "0"));
DropDownList1.Items.Insert(0, "select");
}
}

catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error";
msg += ex.Message;
throw new Exception(msg);

}
finally
{
con.Close();
}
}
public void BindDistrict(string field)
{
DataSet ds = new DataSet();
try
{
con.Open();
SqlCommand cmd = new SqlCommand("p_district", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", field);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
DropDownList2.DataSource = ds;
DropDownList2.DataTextField = "Name";
DropDownList2.DataValueField = "Name";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem("select district", "0"));


}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}

}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{


string value1;
value1=DropDownList1.SelectedItem.Text;
DropDownList2.Items.Insert(0,value1);
value1.IndexOf(value1,0);


BindDistrict(value1);


}
}








jitendra kumar mahapatro

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

Login to post response