Click here to Skip to main content
14,845,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
DataTable ds = new DataTable();
            ds = select.SelectData("Sp_Erp_Pur_PRQuotationMaster_Select");
            ddl_Show.DataSource = ds;
            ddl_Show.DataTextField = "Quotation_ID";
            ddl_Show.DataValueField = "Quotation_ID";
            ddl_Show.DataBind();


this is my code of binding dropdownlist select.selectData is my Dll Name that i used for fetching data from sql server

this code is work for same dropdownlist in one page and not for anther page in asp.net
Posted
Updated 23-Sep-14 18:33pm
v2

C#
DataTable dtTable = new DataTable();

        try
        {
            using (SqlConnection sqlConnection = new SqlConnection("Your connection"))
            {
                using (SqlCommand sqlCommand = new SqlCommand("Your Selection Query", sqlConnection))
                {
                    sqlConnection.Open();

                    using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
                    {
                        dtTable.Load(sqlDataReader);
                        sqlDataReader.Close();
                    }
                }
            }
        }
        catch (Exception error)
        {
            throw error;
        }

        ddlList.DataSource = dtTable;
        ddlList.DataValueField = "id";
        ddlList.DataTextField = "id";
        ddlList.DataBind();
   
1.In ASP.NET is important the event that you use to init your controls, in generally the Page_Load event is used but could be also Page_Init in some situations. For more details see ASP.NET Page Life Cycle Overview[^]

2.In your case you should place your code that init and bind your drop down list in Page_Load event but only when the page is not post back, like in the next example:
C#
protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
      //Put your code for init and binding here!
   }
}
   
v2
  1  SqlCommand cmd = new SqlCommand("SPC", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
  2  cmd.Connection.Open();
  3   
  4  SqlDataReader ddlValues;
  5  ddlValues = cmd.ExecuteReader();
  6   
  7  DropDownList1.DataSource = ddlValues;
  8  DropDownList1.DataValueField = "theName";
  9  DropDownList1.DataTextField = "theName";
 10  DropDownList1.DataBind();
 11   
 12  cmd.Connection.Close();
 13  cmd.Connection.Dispose();
   

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