Hi sir,
i have two combobox named comboTravelAgentLocation,comboTravelAgentName,when i change item from the agentloaction combobox i have to get back the agentsname from the particular area only and when i change the agent name i have to fill the values to the agent details form
here below the code please can you check and fix the error !..
public void FillTravelAgentLocation()
{
DataSet dsTAgentLoc = new DataSet();
dsTAgentLoc = Obj_Class.UserInfo("select Agent_Location from Travel_Agents where Agent_Name<>'NONE'");
DataRow dr = dsTAgentLoc.Tables[0].NewRow();
dr[0] = "--Select--";
dsTAgentLoc.Tables[0].Rows.InsertAt(dr, 0);
comboTravelAgentLocation.DataSource = dsTAgentLoc.Tables[0];
comboTravelAgentLocation.DisplayMember = "Agent_Location";
comboTravelAgentLocation.SelectedIndex = 0;
comboTravelAgentLocation.ValueMember = "Agent_Location";
comboTravelAgentLocation.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.Never;
dsTAgentLoc.Dispose();
}
private void comboTravelAgentLocation_SelectedValueChanged(object sender, EventArgs e)
{
if (comboTravelAgentLocation.SelectedIndex!=0 )
{
int Index = comboTravelAgentLocation.SelectedIndex;
string AgentLoc = comboTravelAgentLocation.SelectedValue.ToString();
FillTravelAgentName(AgentLoc);
}
}
public void FillTravelAgentName(string AgentLoc)
{
DataSet dsAgentName = new DataSet();
if (AgentLoc == "")
{
dsAgentName = Obj_Class.UserInfo("select Agent_Name from Travel_Agents where Agent_Location<>'NONE'");
DataRow drAgentName = dsAgentName.Tables[0].NewRow();
drAgentName[0] = "--Select--";
dsAgentName.Tables[0].Rows.InsertAt(drAgentName, 0);
}
else
{
dsAgentName = Obj_Class.UserInfo("select Agent_Name from Travel_Agents where Agent_Location='" + AgentLoc + "'");
DataRow drAgentName = dsAgentName.Tables[0].NewRow();
drAgentName[0] = "--Select--";
dsAgentName.Tables[0].Rows.InsertAt(drAgentName, 0);
}
comboTravelAgentName.DataSource = dsAgentName.Tables[0];
comboTravelAgentName.DisplayMember = "Agent_Name";
comboTravelAgentName.ValueMember = "Agent_Name";
comboTravelAgentName.SelectedIndex = 0;
dsAgentName.Dispose();
}
private void comboTravelAgentName_SelectedValueChanged(object sender, EventArgs e)
{
if (comboTravelAgentLocation.SelectedIndex != 0 && comboTravelAgentName.SelectedIndex != 0)
{
string AgentLocation = comboTravelAgentLocation.SelectedValue.ToString();
string AgentName = comboTravelAgentName.SelectedValue.ToString();
DataSet dsAgentDtls = new DataSet();
dsAgentDtls = Obj_Class.UserInfo("select Agent_Code,Agent_Phone,Agent_Email,Agent_Web,Agent_Address from Travel_Agents where Agent_Name='" + AgentName + "' and Agent_Location='" + AgentLocation + "'");
if (dsAgentDtls.Tables[0].Rows.Count > 0)
{
txttAgentPhone.Text = dsAgentDtls.Tables[0].Rows[0][1].ToString();
txtAgentEmail.Text = dsAgentDtls.Tables[0].Rows[0][2].ToString();
txtAgentWeb.Text = dsAgentDtls.Tables[0].Rows[0][3].ToString();
txtAgentAddress.Text = dsAgentDtls.Tables[0].Rows[0][4].ToString();
}
}
}
Loading
shijohn josephPosted Nov 20, 2011, 10:43 AM
you did correctly !.. but reader gets only one user data from datasource even if it is location or Name of the agent but i have to fill the entire location and Name to the combo boxes please do you have any solution for this !,...
Satyapriya NayakPosted Nov 19, 2011, 1:13 PM
Try this...
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.Data.OleDb;
namespace Dynamically_bind_data_to_combobox_from_database
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
string str;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox3.Text = "--Select--";
comboBox4.Text = "--Select--";
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from test";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
comboBox3.Items.Add(reader["TravelAgentLoc"]);
}
reader.Close();
con.Close();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox4.Items.Clear();
comboBox4.Text = "--Select--";
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from test where TravelAgentLoc='" + comboBox3.Text.Trim() + "'";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
if (reader.Read())
{
comboBox4.Items.Add(reader["TravelAgentName"]);
}
con.Close();
reader.Close();
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from test where TravelAgentName='" + comboBox4.Text.Trim() + "'";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
if (reader.Read())
{
textBox1.Text = reader["AgentAddress"].ToString();
textBox2.Text = reader["Agentcountry"].ToString();
textBox3.Text = reader["Agentphone"].ToString();
}
con.Close();
reader.Close();
}
}
}
Thanks
If this post helps you mark it as answer
shijohn josephPosted Nov 19, 2011, 12:06 PM
Javeed M ShaikhPosted Nov 18, 2011, 3:44 PM