Click here to Skip to main content
15,891,905 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();
 
Share this answer
 
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!
   }
}
 
Share this answer
 
v2
SqlCommand cmd = new SqlCommand("SPC", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
cmd.Connection.Open();
 
SqlDataReader ddlValues;
ddlValues = cmd.ExecuteReader();
 
DropDownList1.DataSource = ddlValues;
DropDownList1.DataValueField = "theName";
DropDownList1.DataTextField = "theName";
DropDownList1.DataBind();
 
cmd.Connection.Close();
cmd.Connection.Dispose();
 
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