Click here to Skip to main content
15,893,790 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to bind dropdown box using session ?

1st page
C#
protected void Page_Load(object sender, EventArgs e)
    {
        LoadRetrieve();
       Session["ddcity"] = ddCity.Items;
    }

    public void LoadRetrieve()
    {
        SqlConnection conn = new SqlConnection(ConnString);

        string selectSql = "Select * from quest_categories ";
        SqlCommand cmd = new SqlCommand(selectSql, conn);


        conn.Open();

        ddCity.DataSource = cmd.ExecuteReader();
        ddCity.DataTextField = "City"; // Column Name
        ddCity.DataValueField = "Sr_No"; // Primary Key


        this.DataBind();
        conn.Close();

       
    }





2n page

C#
protected void Page_Load(object sender, EventArgs e)
{
   // this is not the correct code... I want correct one
    foreach (ListItem item in Session["ddcity"].ToString())
    {
        DropDownList1.Items.Add(item);
    }
}



Thanks...
Posted

1 solution

This may help you
First Page
protected void Page_Load(object sender, EventArgs e)
{
    LoadRetrieve();
    
}
public void LoadRetrieve()
{
    SqlConnection conn = new SqlConnection(ConnString);
    string selectSql = "Select * from quest_categories ";
    SqlCommand cmd = new SqlCommand(selectSql, conn);
    conn.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(dr);
    conn.Close();
    Session["ddcity"] = dt;
    ddCity.DataSource = dt;
    ddCity.DataTextField = "City"; // Column Name
    ddCity.DataValueField = "Sr_No"; // Primary Key
    ddCity.DataBind();
}


Second Page
C#
protected void Page_Load(object sender, EventArgs e)
{
   if(Session["ddcity"]!=null)
   {
       DataTable dt = (DataTable) Session["ddcity"];
       ddCity.DataSource = dt;
       ddCity.DataTextField = "City"; // Column Name
       ddCity.DataValueField = "Sr_No"; // Primary Key
       ddCity.DataBind();
   }
}


if you feel it useful then don't forget to accept the solution and vote it up :)
 
Share this answer
 
Comments
thatraja 11-Jun-11 7:27am    
Why don't you change your ID? Member_6499766 is may be tough to keep in mind for all.
parmar_punit 13-Jun-11 1:03am    
because someone has down vaoted my 10 to 12 answer, I don't know why but someone has problem with me...
thatraja 13-Jun-11 1:11am    
It happens some time to many authorities(also me), anyway don't worry just go ahead. BTW I have counter voted for couple of your answers.
parmar_punit 13-Jun-11 1:39am    
Thanks dear :)
sat_100m 12-Jun-11 3:10am    
Member_6499766 : I want to pass Session value to dropdown items not in another datatable..

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