Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In our project, we want the college name in drop down list from college table in drop down list's SelectedIndexChanged event.How I can get collge name in drop down list of that event.

The code written by us is as follow.

C#
protected void ddlCollegename_SelectedIndexChanged(object sender, EventArgs e)
    {
            objsqlconnection = new SqlConnection(strconnectionstring);
            string strquery1 = "select CollegeName from tblCollege_Info";
            objsqlcommand = new SqlCommand(strquery1, objsqlconnection);
           
            try
            {
                objsqlconnection.Open();
                SqlDataReader objreader = objsqlcommand.ExecuteReader();
                while (objreader.Read())
                {
                  
                    ddlCollegename.Items.Add(objreader[1].ToString());
                }
                objreader.Close();
            }
            catch
            {
                Console.WriteLine("Error!!!!!!!1");
            }
            finally
            {
                objsqlconnection.Close();
            }
}



Please Help me.
Thanks

[edit]Minor tweaks to code block - OriginalGriff[/edit]
Posted
Updated 3-Aug-11 0:39am
v2
Comments
Ashika s 3-Aug-11 6:34am    
for what purpose to get collegename in that event?
Ashika s 3-Aug-11 6:35am    
just fill dropdown list and that function call in page load
Ashika s 3-Aug-11 6:36am    
plz elaborate your question?
BobJanova 3-Aug-11 7:22am    
You do not want to do this on SelectedIndexChanged. That causes the page to post back EVERY TIME the user picks a new college, and for no purpose (you are filling the list with the same items each time). Fill the dropdown once, in Page_Load.

C#
objsqlconnection = new SqlConnection(strconnectionstring);
DataSet ds=new DataSet();
            string strquery1 = "select * from tblCollege_Info";
            objsqlcommand = new SqlCommand(strquery1, objsqlconnection);
OleDbAdapter a=new OleDbAdapter();
a.selectCommand=objsqlcommand;
a.fill(ds,"tblCollege_Info");
                     
 try
 {
                   ddlCollegename.DataSource=ds;
ddlCollegename.DataTextField="Collegename";
ddlCollegename.DataValueField="ID";//ie PK of your table
 
           }
            catch
            {
                Console.WriteLine("Error!!!!!!!1");
            }
            finally
            {
                objsqlconnection.Close(); 
 
Share this answer
 
use sqldatasource & configure it for college table.for dropdownlist select ur datasource
 
Share this answer
 

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