I want to populate TWO dropdownlist, based on selection of first dropdownlist the second dropdownlist will get populated.
Example :
Like i have two dropdownlist namely 1.ddlCountry and 2.ddlState
Now when country is selected then depending on the selected Country the States related with that Country will get populated in the State dropdownlist. I want to achieve this withour reloading the whole page in Asp.Net with coding language as C#.
How can i achieve the same?
Dropdownlist is fetching data from database by executing query.
You can use AJAX toolkit CascadingDropDown as told by Naresh.
OR
use ajax update panel and keep all Dropdowns in it. So the whole page will not load on changing dropdown value.
You didnt give the code to further solution.
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
FillStateByCountry();
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
FillLocationByCountryandState();
}
private void FillStateByCountry()
{
DataSet dstFillState;
int CountryId = Convert.ToInt32(ddlCountry.SelectedValue.ToString());
dstFillState = Tbl_State.FillDDLState(CountryId);
ddlState.DataSource = dstFillState;
ddlState.DataTextField = "State";
ddlState.DataValueField = "Id";
ddlState.DataBind();
}
similarly FillLocationByCountryandState();
Add two dropdownlists to your form and name it as cmbStates, cmbCities
when you select state name from cmbStates(dropwdownlist), cmbCities(dropdownlist) generates cities based on state name(cmbStates)
by fetching data from database
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=pbs-server;database=p2p;user id=shekar;password=sekhar#1346");
SqlCommand cmd = new SqlCommand("select states from Country", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
da.Fill(ds, "Country");
cmbStates.DataSource = ds.Tables[0];
cmbStates.SelectedValue = 0;
con.Close();
}
private void cmbStates_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=xxxx;database=xxxx;user id=xxxxr;password=xxxxxx");
SqlCommand cmd = new SqlCommand("select cities from States where cityname = 'cmbStates.SelectedItem.ToString()'", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
da.Fill(ds, "States");
cmbCities.DataSource = ds.Tables[0];
cmbCities.SelectedValue = 0;
con.Close();
}
OR
manually adding items
namespace DropDownlist
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cmbStates.Items.Add("Andhra Pradesh");
cmbStates.Items.Add("Tamilnadu");
cmbStates.Items.Add("Karnataka");
cmbStates.SelectedValue = 0;
}
private void cmbStates_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbStates.SelectedItem.ToString() == "Andhra Pradesh")
{
cmbCities.Items.Clear();
cmbCities.Items.Add("Hyderabad");
cmbCities.Items.Add("Guntur");
cmbCities.Items.Add("Vijayawada");
cmbCities.SelectedValue = 0;
}
else if (cmbStates.SelectedItem.ToString() == "Tamilnadu")
{
cmbCities.Items.Clear();
cmbCities.Items.Add("Chennai");
cmbCities.Items.Add("Coimbatore");
cmbCities.Items.Add("ooty");
cmbCities.SelectedValue = 0;
}
else if (cmbStates.SelectedItem.ToString() == "Karnataka")
{
cmbCities.Items.Clear();
cmbCities.Items.Add("Bangalore");
cmbCities.Items.Add("Mangalore");
cmbCities.SelectedValue = 0;
}
else
{
MessageBox.Show("Please Select any value");
}
}
}
}
Related
this is my code for populating a dropdown list with customers name.
protected void Page_Load(object sender, EventArgs e)
{
//Creating a connection to my database using the connection string
string cs = System.Configuration.ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
{
SqlCommand cmd = new SqlCommand("Select CustomerName from Customer", con);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList1.DataTextField = "CustomerName";
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
}
}
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
}
}
I want to be able too; when this name is chosen from the dropdownlist box , the associated customer ID will appear in a textbox.
thanks!
It should be as simple as this, by the looks of it:
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
TextBox1.Text = DropDownList1.SelectedValue
}
Assuming you have a textbox whose ID is TextBox1.
Alternatively if you want to display the text from the dropdown list (as opposed to the ID value), then simply:
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
TextBox1.Text = DropDownList1.SelectedItem.Text
}
I have combobox in my form which display value from database. I want one default value so I use .text property also and on selected index changed event the label display the corresponding value of the database the code is not working
string cstr = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
private void AddStock_Load(object sender, EventArgs e)
{
bindcombo();
}
private void bindcombo()
{
SqlConnection con = new SqlConnection(cstr);
SqlDataAdapter da = new SqlDataAdapter("SELECT Dname FROM dealer", con);
DataTable dt = new DataTable();
da.Fill(dt);
cmbdealer.DataSource = dt;
cmbdealer.DisplayMember = "Dname";
cmbdealer.Text = "select-Dealer";
con.Close();
}
private void cmbdealer_SelectedIndexChanged(object sender, EventArgs e)
{
dealerdetails();
}
private void dealerdetails()
{
SqlConnection con = new SqlConnection(cstr);
SqlCommand cmd = new SqlCommand("select * from dealer where Dname='" + cmbdealer.Text + "'", con);
con.Open();
SqlDataReader re = cmd.ExecuteReader();
while (re.Read())
{
lbdl.Text = re["Ddlno"].ToString();
lbgst.Text = re["Dgstno"].ToString();
lbadd.Text = re["Daddress"].ToString();
}
}
the above code is working fine but when the form is load all the 3 label shows the database value of the first entry of my dealer table without selecting and value from combobox.
I'm new in this and I'm a little lost.
Trying to show the values of my database in textbox by selecting in the combobox. But I can't.
Please help me. This is my code:
private void CargarDatos()
{
string consulta = "SELECT * FROM [dbo].[alumno]";
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(Properties.Settings.Default.conexion);
SqlCommand cmd = new SqlCommand(consulta, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
con.Open();
da.Fill(dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
con.Close();
this.dataGridView1.DataSource = dt;
cbalumno.DataSource = dt;
cbalumno.DisplayMember="Nombre";
cbalumno.ValueMember="Id";
}
private void Form1_Load(object sender, EventArgs e)
{
CargarDatos();
}
private void cbalumno_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
The parameters that i want to show are "Name" "Surname" and "DNI" of the table alumno.
Any ideas to how can I do that??
You can use DataRowView to get the record being bound with current SelectedItem. The Row property of DataRowView object will give you data row. Using this row you can get the columns being bound to it.
private void cbalumno_SelectedIndexChanged(object sender, EventArgs e)
{
DataRowView vrow = (DataRowView)cbalumno.SelectedItem;
string sValue = vrow.Row["Name"].ToString();
}
You already have placed the event cbalumno_SelectedIndexChanged in your code and now you have to use it.
Inside that event, just use the Text peroperty of that textbox and assign the value of the selected item in your combo box like this :
private void cbalumno_SelectedIndexChanged(object sender, EventArgs e)
{
yourTextBoxID.Text = comboBoxID.Text;
}
Hope this helps.
I want to display the date which is selected in another form using monthCalender control..
Here is my code..
private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{
ActivityScheduler frm1 = new ActivityScheduler();
frm1.Show();
}
private void ActivityScheduler_Load(object sender, EventArgs e)
{
if (con.State == ConnectionState.Open) { con.Close(); }
con.Open();
RemainderPopUp frm = new RemainderPopUp();
string s = "select * from [Activity_Scheduler]";
SqlCommand sCmd = new SqlCommand(s, con);
SqlDataAdapter da = new SqlDataAdapter(sCmd);
DataSet ds = new DataSet();
da.Fill(ds, "[Activity_Scheduler]");
datagridActivityScheduler.DataSource = ds.Tables[0];
datagridActivityScheduler.AllowUserToAddRows = true;
DataTable dt = new DataTable();
dt = ds.Tables["Activity_Scheduler"];
if (dt == null)
{
datagridActivityScheduler.Rows[0].Cells[3].Value = frm.monthCalendar1.SelectionRange.Start.ToShortDateString();
MessageBox.Show(datagridActivityScheduler.Rows[0].Cells[3].Value.ToString());
}
con.Close();
}
It is displaying the correct value in messagebox..but the value is not displaying in datagridview...
Plz anybody help me out..
Try Following Code :
form_load()
{
private string myDate="1999/12/12";
// Declare a Date property of type string:
public string Name
{
get
{
return myDate;
}
set
{
myName = value;
}
}
private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{
ActivityScheduler frm1 = new ActivityScheduler();
ActivityScheduler.myDate = frm.monthCalendar1.SelectionRange.Start.ToShortDateString()
frm1.Show();
}
Now you can get date on other form as :
string myDate = frm1.myDate();
Let me know if any queries.
Every time my forms loads/open it will prompt "System.Data.DataRowView", How can I possibly remove this?
Here is my code:
{
InitializeComponent();
GetProcessorCardTypes();
}
private void GetProcessorCardTypes()
{
cn.Open();
MySqlCommand cmd = new MySqlCommand("call GetProcessorMethod(1)", cn);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmbProcessorMethods.DataSource = dt;
cmbProcessorMethods.ValueMember = "method_id";
cmbProcessorMethods.DisplayMember = "method_name";
}
private void cmbProcessorMethods_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(cmbProcessorMethods.SelectedValue.ToString());
}
If you want to completely remove message box, then remove cmbProcessorMethods_SelectedIndexChanged event handler. Or you can change it to display method_name of selected row:
private void cmbProcessorMethods_SelectedIndexChanged(object sender, EventArgs e)
{
DataRowView row = (DataRowView)cmbProcessorMethods.SelectedItem;
MessageBox.Show(row["method_name"].ToString());
}
As Derek pointed, just set DisplayMember and ValueMember properties before assigning data source and your original code will work:
cmbProcessorMethods.ValueMember = "method_id";
cmbProcessorMethods.DisplayMember = "method_name";
cmbProcessorMethods.DataSource = dt;