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

I am Not able to fire selected index changed event.
I am retreiving previous combo box selected index
(string selectedstore_name = cmbRestaurant.Items[cmbRestaurant.SelectedIndex].ToString();)
but don't know how
i can use the same. Can someone guide me further with the same.
C#
private void LoadUsers()
{
 //string connectionString = null;
SqlConnection connection;
SqlDataAdapter adapter;
SqlCommand cmd = new SqlCommand();
DataSet D = new DataSet();

connection = new SqlConnection(connectionString);

connection.Open();
cmd.Connection = connection;

//Load user list
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "dbo.GetStore";
adapter = new SqlDataAdapter(cmd);
adapter.Fill(D);
cmbRestaurant.DataSource=D.Tables[0].DefaultView;
cmbRestaurant.DisplayMember = "store_name";
cmbRestaurant.ValueMember= "store_id";
cmd.Parameters.Clear();

connection.Close();
connection.Dispose();

}     

       private void cmbRestaurant_SelectedIndexChanged(object sender, EventArgs e)
       {

          string selectedstore_name = cmbRestaurant.Items[cmbRestaurant.SelectedIndex].ToString();

           SqlConnection connection = new SqlConnection(connectionString);
           SqlCommand cmd = new SqlCommand();
           cmd.CommandType = CommandType.Text;
           cmd.CommandText = "dbo.om_service_window, dbo.om_sw_location_map, dbo.om_Equipment";
           connection.Open();
          
           SqlDataAdapter adapter = new SqlDataAdapter(cmd);
          
           string Q2 = "select sw.sw_name from dbo.om_service_window sw join dbo.om_sw_location_map s on sw.sw_id=s.sw_id join dbo.om_Equipment e on s.point_location=e.point_location";
           adapter.SelectCommand = new SqlCommand(Q2, connection);
           adapter.SelectCommand.ExecuteNonQuery();
           DataSet DS2 = new DataSet();
           adapter.Fill(DS2, "D");
           DataTable DSTbl2 = DS2.Tables["D"];
           cmbServiceWindow.DataSource = DS2.Tables[0].DefaultView;
           cmbServiceWindow.DisplayMember = "sw_name";
           cmbServiceWindow.ValueMember = "sw_name";
           connection.Close();
           connection.Dispose();
           cmd.Parameters.Clear();
       }

       private void cmbServiceWindow_SelectedIndexChanged(object sender, EventArgs e)
       {
          string selectedservice_window = cmbServiceWindow.Items[cmbServiceWindow.SelectedIndex].ToString();
           // string ServiceWindow_name = cmbServiceWindow.Items[cmbServiceWindow.SelectedIndex].ToString();
           SqlConnection connection = new SqlConnection(connectionString);
           SqlCommand cmd = new SqlCommand();
           cmd.CommandType = CommandType.Text;
           cmd.CommandText = "dbo.om_Equipment";
           connection.Open();
           //cmd = new SqlCommand("select f.p_equip_id from dbo.om_equip_functionblock f join dbo.om_sw_location_map s on f.point_location=s.point_location join dbo.om_service_window sw on s.sw_id=sw.sw_id", connection);
           SqlDataAdapter adapter = new SqlDataAdapter(cmd);
           //DataSet ds = new DataSet();
           //adapter.Fill(ds);
           //cmbEquipments.DataSource = ds.Tables[0].DefaultView;
           //cmbEquipments.DisplayMember = "p_equip_id";
           //cmbServiceWindow.ValueMember = "selectedservice_window";
           //connection.Close();
           //connection.Dispose();
           //cmd.Parameters.Clear();
           string Q1 = "select distinct a.equip_desc from om_Equipment a right outer join om_Equipment b on a.equip_id=b.p_equip_id";
           adapter.SelectCommand = new SqlCommand(Q1, connection);
           adapter.SelectCommand.ExecuteNonQuery();
           DataSet DS1 = new DataSet();
           adapter.Fill(DS1, "D");
           DataTable DSTbl1 = DS1.Tables["D"];
           cmbEquipments.DataSource = DS1.Tables[0].DefaultView;
           cmbEquipments.DisplayMember = "equip_desc";
           cmbEquipments.ValueMember = "equip_desc";
           connection.Close();
           connection.Dispose();
           cmd.Parameters.Clear();
       }

       private void cmbEquipments_SelectedIndexChanged(object sender, EventArgs e)
       {
          string selectedEquipments = cmbEquipments.Items[cmbEquipments.SelectedIndex].ToString();
           // string ServiceWindow_name = cmbServiceWindow.Items[cmbServiceWindow.SelectedIndex].ToString();
           SqlConnection connection = new SqlConnection(connectionString);
           SqlCommand cmd = new SqlCommand();
           cmd.CommandType = CommandType.Text;
           cmd.CommandText = "dbo.om_Equipment";
           connection.Open();
          // cmd = new SqlCommand("select equip_id from dbo.om_equip_functionblock", connection);
           SqlDataAdapter adapter = new SqlDataAdapter(cmd);
           //DataSet ds = new DataSet();
           //adapter.Fill(ds);
           //cmbSubSystems.DataSource = ds.Tables[0].DefaultView;
           //cmbSubSystems.DisplayMember = "equip_id";
           //cmbSubSystems.ValueMember = "equip_id";
           //connection.Close();
           //connection.Dispose();
           //cmd.Parameters.Clear();
           string Q3 = "select distinct b.equip_id from om_Equipment a right outer join om_Equipment b on a.equip_id=b.p_equip_id";
           adapter.SelectCommand = new SqlCommand (Q3, connection);
           adapter.SelectCommand.ExecuteNonQuery();
           DataSet DS3 = new DataSet();
           adapter.Fill(DS3, "D");
           DataTable DSTbl1 = DS3.Tables["D"];
           cmbSubSystems.DataSource = DS3.Tables[0].DefaultView;
           cmbSubSystems.DisplayMember = "equip_id";
           cmbSubSystems.ValueMember = "equip_id";
           connection.Close();
           connection.Dispose();
           cmd.Parameters.Clear();
       }      

        }
    }
Posted
Updated 10-Jun-12 23:43pm
v2
Comments
Sandeep Mewara 11-Jun-12 5:44am    
Please correct your tags for the question. Winforms I guess.
Kiran S.V 11-Jun-12 6:53am    
Yeah. I am retreiving the previous combobox values but don't know how to assign it for next combobox

string selectedstore_name = cmbRestaurant.Items[cmbRestaurant.SelectedIndex].ToString();
don't know where i can assign it?
RDBurmon 13-Jun-12 9:33am    
Thanks Everyone who replied to this thread , So Kiran, I think you have got enough responses and you should be able to mark it as your answer and close the thread. Please do so.

Hi,

You can remove all the code from xxx_SelectedIndexChanged events and make it different methods and call these methods on these xxx_SelectedIndexChanged event . Then you can get the selected value of combo using folowing code....

C#
int selectedstore = cmbRestaurant.SelectedValue;


You can input this selected value to the next method and fetch related records only, (write the methods with appropriate parameters) and fill the next combo. This way you may be able to solve your problem.

regards
Sebastian
 
Share this answer
 
please check that your control & the event(cmbRestaurant_SelectedIndexChanged) is perfectly sync. I suspect that event is orphan, so it is not getting invoked.

Regards
Sebastian
 
Share this answer
 
Comments
Kiran S.V 11-Jun-12 6:51am    
No control & the event(cmbRestaurant_SelectedIndexChanged) are perfectly sync but on cmbRestaurant_SelectedIndexChanged i want 'cmbServiceWindow' combobox should be filled with only 'sw_name' but here all the comboboxes are populated when i select the index of (cmbRestaurant_SelectedIndexChanged) event.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.CSharp;
using Excel = Microsoft.Office.Interop.Excel;


namespace ImportDataToExcelSP
{
public partial class Form1 : Form
{
private static readonly string connectionStringName = ConfigurationManager.AppSettings.Get("ConnectionString");
private static readonly string connectionString = connectionStringName;
public Form1()
{
InitializeComponent();

//cmbRestaurant.SelectedIndex = 0;
Restaurant();


}
public DataSet Restaurant()
{
SqlConnection connection;
SqlDataAdapter adapter;
SqlCommand cmd = new SqlCommand();
DataSet D = new DataSet();


connection = new SqlConnection(connectionString);

connection.Open();
cmd.Connection = connection;

//Load user list
cmd.CommandType = CommandType.Text;
cmd.CommandText = "dbo.om_Equipment";
adapter = new SqlDataAdapter(cmd);
string Q = "select point_location from dbo.om_Equipment";
adapter.SelectCommand = new SqlCommand(Q, connection);
adapter.SelectCommand.ExecuteNonQuery();
DataSet DS5 = new DataSet();
adapter.Fill(DS5, "DS5");
DataTable DSTbl5 = DS5.Tables["DS5"];
cmbRestaurant.DataSource = DS5.Tables[0].DefaultView;
cmbRestaurant.DisplayMember = "point_location";
cmbRestaurant.ValueMember = "point_location";
connection.Close();
connection.Dispose();
cmd.Parameters.Clear();
return D;

}




private void button1_Click(object sender, EventArgs e)
{

}

private void cmbRestaurant_SelectedIndexChanged(object sender, EventArgs e)
{
// string selectedStoreName = cmbRestaurant.SelectedIndex.ToString();
ServiceWindow();
}

public DataSet ServiceWindow()
{

String selectedStoreName = ((DataRowView)cmbRestaurant.SelectedItem).Row["point_location"].ToString();

SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "dbo.om_service_window, dbo.om_sw_location_map, dbo.om_Equipment";
connection.Open();

SqlDataAdapter adapter = new SqlDataAdapter(cmd);
//DataSet ds = new DataSet();
//adapter.Fill(ds);
//cmbServiceWindow.DataSource = ds.Tables[0].DefaultView;
//cmbServiceWindow.DisplayMember = "sw_name";
//cmbServiceWindow.ValueMember = "selectedstore_name";
////connection.Close();
//connection.Dispose();
//cmd.Parameters.Clear();
string Q2 = "select distinct sw.sw_name from dbo.om_service_window sw join dbo.om_sw_location_map s on sw.sw_id=s.sw_id join dbo.om_Equipment e on s.point_location=e.point_location where s.point_location='"+selectedStoreName.ToString() +"'";
adapter.SelectCommand = new SqlCommand(Q2, connection);
adapter.SelectCommand.ExecuteNonQuery();
DataSet DS2 = new DataSet();
adapter.Fill(DS2, "DS2");
DataTable DSTbl2 = DS2.Tables["DS2"];
cmbServiceWindow.DataSource = DS2.Tables[0].DefaultView;
cmbServiceWindow.DisplayMember = "sw_name";
cmbServiceWindow.ValueMember = "sw_name";
connection.Close();
connection.Dispose();
cmd.Parameters.Clear();
return DS2;
}

private void cmbServiceWindow_SelectedIndexChanged(object sender, EventArgs e)
{
Equipments();
}

public DataSet Equipments()
{
String ServiceWindow = ((DataRowView)cmbServiceWindow.SelectedItem).Row["sw_name"].ToString();
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "dbo.om_Equipment";
connection.Open();
//cmd = new SqlCommand("select f.p_equip_id from dbo.om_equip_functionblock f join dbo.om_sw_location_map s on f.point_location=s.point_location join dbo.om_service_window sw on s.sw_id=sw.sw_id", connection);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
//DataSet ds = new DataSet();
//adapter.Fill(ds);
//cmbEquipments.DataSource = ds.Tables[0].DefaultView;
//cmbEquipments.DisplayMember = "p_equip_id";
//cmbServiceWindow.ValueMember = "selectedservice_window";
//connection.Close();
//connection.Dispose();
//cmd.Parameters.Clear();
string Q1 = "select equip_id from dbo.om_Equipment where p_equip_id is null and point_location = 'IR_Schmale' ";
adapter.SelectCommand = new SqlCommand(Q1, connection);
adapter.SelectCommand.ExecuteNonQuery();
DataSet DS1 = new DataSet();
adapter.Fill(DS1, "DS1");
DataTable DSTbl1 = DS1.Tables["DS1"];
cmbEquipments.DataSource = DS1.Tables[0].DefaultView;
cmbEquipments.DisplayMember = "equip_id";
cmbEquipments.ValueMember = "equip_id";
connection.Close();
connection.Dispose();
cmd.Parameters.Clear();
return DS1;
}

private void cmbEquipments_SelectedIndexChanged(object sender, EventArgs e)
{
SubSystems();
}
public DataSet SubSystems()
{
String Equipments = ((DataRowView)cmbEquipments.SelectedItem).Row["equip_id"].ToString();
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "dbo.om_Equipment";
connection.Open();
// cmd = new SqlCommand("select equip_id from dbo.om_equip_functionblock", connection);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
//DataSet ds = new DataSet();
//adapter.Fill(ds);
//cmbSubSystems.DataSource = ds.Tables[0].DefaultView;
//cmbSubSystems.DisplayMember = "equip_id";
//cmbSubSystems.ValueMember = "equip_id";
//connection.Close();
//connection.Dispose();
//cmd.Parameters.Clear();
string Q3 = "select equip_id from dbo.om_Equipment where p_equip_id='PF' and point_location='IR_Schmale'";
adapter.SelectCommand = new SqlCommand(Q3, connection);
adapter.SelectCommand.ExecuteNonQuery();
DataSet DS3 = new DataSet();
adapter.Fill(DS3, "DS3");
DataTable DSTbl3 = DS3.Tables["DS3"];
cmbSubSystems.DataSource = DS3.Tables[0].DefaultView;
cmbSubSystems.DisplayMember = "equip_id";
cmbSubSystems.ValueMember = "equip_id";
connection.Close();
connection.Dispose();
cmd.Parameters.Clear();
return DS3;
}


private void cmbSubSystems_SelectedIndexChanged(object sender, EventArgs e)
{
Meter();
}

public DataSet Meter()
{
String SubSystems = ((DataRowView)cmbSubSystems.SelectedItem).Row["equip_id"].ToString();
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "dbo.r_equp_meter_map";
connection.Open();
// cmd = new SqlCommand("select equip_id from dbo.om_equip_functionblock", connection);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
//DataSet ds = new DataSet();
//adapter.Fill(ds);
//cmbSubSystems.DataSource = ds.Tables[0].DefaultView;
//cmbSubSystems.DisplayMember = "equip_id";
//cmbSubSystems.ValueMember = "equip_id";
//connection.Close();
//connection.Dispose();
//cmd.Parameters.Clear();
string Q4 = "select distinct source_type_id from dbo.r_equp_meter_map";
adapter.SelectCommand = new SqlCommand(Q4, connection);
adapter.SelectCommand.ExecuteNonQuery();
DataSet DS4 = new DataSet();
adapter.Fill(DS4, "DS4");
DataTable DSTbl4 = DS4.Tables["DS4"];
cmbMeter.DataSource = DS4.Tables[0].DefaultView;
cmbMeter.DisplayMember = "source_type_id";
cmbMeter.ValueMember = "source_type_id";
connection.Close();
connection.Dispose();
cmd.Parameters.Clear();
return DS4;
}



}
}


But all comboboxes are loading at a time.
SQL
(restaurant,service window,Equipments and subsystems) but i want to load them one by one.

On restaurant selected index change service window should be loaded like that.
 
Share this answer
 
SQL
All the four Comboboxes are loading at a time (restaurant,service window,Equipments and subsystems) but i want to load them one by one.

On restaurant selected index change service window should be loaded like that.


Can anyone guide me how to get it?
 
Share this answer
 
Comments
Sebastian T Xavier 12-Jun-12 2:31am    
This is Not an answer. please remove it and use the improve question link to update your question.
Kiran S.V 12-Jun-12 3:41am    
Hi,

Thanks for your suggestion but when i tried to call the method and tried to pass
selectedstore into the other method i am getting error as
object reference not set to instance of object.
Sebastian T Xavier 12-Jun-12 6:10am    
it seems that selectedstore is null. just check the selected value and the value in the variable.
Otherwise you post your new code & let me cross check it.

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