ComboBox items from DataSet getting nulled indexes on selectedIndexChangedEvent

Shijohnjoseph
Posted by Shijohnjoseph under C# category on | Points: 40 | Views : 2148
Hi ,
1) i have two comboBox named ComboName,ComboLocation in my windows forms ,
2) i have to get a list of loaction to ComboLocation from DataBase using DataSet!..
in the dataset table i have inserted 0'th index manually to get "--Select--".
using this code
DataSet dsLoc = new DataSet();
dsLoc = Obj_Class.UserInfo("select Agent_Location from Travel_Agents where Agent_Location<>'NONE' ");
DataRow dr= dsLoc.Tables[0].NewRow();
dr[0] = "--Select--";
dsLoc.Tables[0].Rows.InsertAt(dr, 0);
comboTravelAgentLocation.DataSource = dsLoc.Tables[0];
comboTravelAgentLocation.DisplayMember = "Agent_Location";
comboTravelAgentLocation.ValueMember = "Agent_Location";
3) here is my problem while i am getting values to ComboLocation its check for selectedIndecChanged event for the ComboBoxLocation and below this event i put the code for binding values to the ComboBoxName relevant to the index
while binding the name also checks the event SelectedIndexChanged in ComboName and binding null values each and every time !..

please anyone do a favor for me and help me to finish my task !..
below my code please do check

public void FillTravelAgentLocation()
{
comboTravelAgentLocation.Text = "--Select--";
comboTravelAgentName.Text = "--Select--";
DataSet dsLoc = new DataSet();
dsLoc = Obj_Class.UserInfo("select Agent_Location from Travel_Agents where Agent_Location<>'NONE' ");
DataRow dr= dsLoc.Tables[0].NewRow();
dr[0] = "--Select--";
dsLoc.Tables[0].Rows.InsertAt(dr, 0);
comboTravelAgentLocation.DataSource = dsLoc.Tables[0];
comboTravelAgentLocation.DisplayMember = "Agent_Location";
comboTravelAgentLocation.ValueMember = "Agent_Location";
comboTravelAgentLocation.SelectedIndexChanged += new EventHandler(comboTravelAgentLocation_SelectedIndexChanged);
dsLoc.Dispose();
}
private void comboTravelAgentLocation_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboTravelAgentLocation.SelectedIndex != 0 && comboTravelAgentLocation.SelectedIndex != -1)
{

comboTravelAgentName.Items.Clear();
comboTravelAgentName.Text = "--Select--";
DataSet dsName = new DataSet();
dsName = Obj_Class.UserInfo("Select Agent_Name from Travel_Agents where Agent_Location='" + comboTravelAgentLocation.Text.Trim() + "'");
DataRow dr = dsName.Tables[0].NewRow();
dr[0] = "--Select--";
dsName.Tables[0].Rows.InsertAt(dr, 0);
comboTravelAgentName.DataSource= dsName.Tables[0];
comboTravelAgentName.DisplayMember = "Agent_Name";
comboTravelAgentName.ValueMember = "Agent_Name";
txttAgentPhone.Text = "";
txtAgentEmail.Text = "";
txtAgentWeb.Text = "";
txtAgentAddress.Text = "";
comboTravelAgentName.SelectedIndexChanged += new EventHandler(comboTravelAgentName_SelectedIndexChanged);
dsName.Dispose();
}
private void comboTravelAgentName_SelectedIndexChanged(object sender, EventArgs e)
{
Conn = new OleDbConnection();
Conn = Obj_Class.Connstr();
Conn.Open();
string CmdString = "select * from Travel_Agents where Agent_Name='" + comboTravelAgentName.Text.Trim() + "'";
Cmd = new OleDbCommand(CmdString, Conn);
OleDbDataReader Reader = Cmd.ExecuteReader();
if (Reader.Read())
{
txttAgentPhone.Text = Reader["Agent_Phone"].ToString();
txtAgentEmail.Text = Reader["Agent_Email"].ToString();
txtAgentWeb.Text = Reader["Agent_Web"].ToString();
txtAgentAddress.Text = Reader["Agent_Address"].ToString();
}
comboTravelAgentName.SelectedIndexChanged -= new EventHandler(comboTravelAgentName_SelectedIndexChanged);
comboTravelAgentLocation.SelectedIndexChanged -= new EventHandler(comboTravelAgentLocation_SelectedIndexChanged);
comboTravelAgentLocation.Items.Clear();
FillTravelAgentLocation();
}

please check the code and help me !..

Comments or Responses

Login to post response