In my application,I call method at load time which fill college name into drop down list of college name.When I select one of the college,I want the faculties of corresponding that college from database.But SelectedIndexChanged event of drop down list of College name does not fire.Please give me a solution.
Thank You.
The code written by me is as follow.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
fillcollegename();
}
}
void fillcollegename()
{
objsqlconnection = new SqlConnection();
objsqlconnection.ConnectionString = strconnectionstring;
objsqlconnection.Open();
try
{
string strquery1 = "select * from tblCollege_Info";
objsqlcommand = objsqlconnection.CreateCommand();
objsqlcommand.CommandText = strquery1;
SqlDataAdapter a = new SqlDataAdapter(objsqlcommand);
DataTable table = new DataTable();
a.Fill(table);
String[] CollegeNames = new String[table.Rows.Count];
int i = 0;
foreach (DataRow row in table.Rows)
{
CollegeNames[i] = row["CollegeName"].ToString();
i++;
}
ddlCollegenm.DataSource = CollegeNames;
ddlCollegenm.DataBind();
}
catch
{
}
objsqlconnection.Close();
}
protected void ddlCollegenm_SelectedIndexChanged(object sender, EventArgs e)
{
objsqlconnection = new SqlConnection();
objsqlconnection.ConnectionString = strconnectionstring;
objsqlconnection.Open();
string strquery2 = "select CollegeId from tblCollege_Info where (CollegeName='" + ddlCollegenm.SelectedValue.ToString() + "')";
objsqlcommand = new SqlCommand(strquery2, objsqlconnection);
SqlDataReader objsqldatareader = objsqlcommand.ExecuteReader();
if(objsqldatareader.Read())
{
collegeid = objsqldatareader[0].ToString();
}
objsqldatareader.Close();
objsqlconnection.Close();
fillfacultyname();
}
void fillfacultyname()
{
objsqlconnection = new SqlConnection();
objsqlconnection.ConnectionString = strconnectionstring;
try
{
objsqlconnection.Open();
string strquery1 = "select FacultyName from tblFaculty_Info where (CollegeId='" + collegeid + "')";
objsqlcommand = objsqlconnection.CreateCommand();
objsqlcommand.CommandText = strquery1;
SqlDataAdapter a = new SqlDataAdapter(objsqlcommand);
DataTable table = new DataTable();
a.Fill(table);
String[] FacultyNames = new String[table.Rows.Count];
int i = 0;
foreach (DataRow row in table.Rows)
{
FacultyNames[i] = row["FacultyName"].ToString();
i++;
}
ddlfacultynm.DataSource = FacultyNames;
ddlfacultynm.DataBind();
}
catch
{
}
finally
{
objsqlconnection.Close();
}
}