Click here to Skip to main content
15,886,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
c# code --Dropdown code
C#
private void FillBatch()  // Batch
   {
       try
       {
           clsDataAccess da = new clsDataAccess();
           da.AddTabletoDataset("SELECT  Batch_Year,BatchId FROM Batch", "Batch");
           if (da.DataSet.Tables["Batch"].Rows.Count > 0)
           {
               DDL_Batch.DataSource = da.DataSet.Tables["Batch"];
               DDL_Batch.DataTextField = "Batch_Year";
               DDL_Batch.DataValueField ="BatchId";
               DDL_Batch.DataBind();
               DDL_Batch.Items.Insert(0, "Select");
           }
       }
       catch (Exception ex)
       {
           HandleException.SendMail(ex);
       }
   }


create table Batch (BatchId int,Batch_Year varchar(50))



database insert script
SQL
insert Batch values
(1,'2013'),(2,'2012'),
(3,'2011'),
(4,'2010'),
(5,'2009'),
(6,'2008'),
(7,'2007'),
(8,'2006'),
(9,'2005'),
(10,'2004')
Posted
Comments
Nandakishore G N 24-Jan-13 1:16am    
try to elaborate the question?

try this-

C#
private void FillBatch()  // Batch
   {
       try
       {
           SqlDataAdapter da=new SqlDataAdapter("SELECT  Batch_Year,BatchId FROM Batch", con);
         Dataset ds=new Dataset();
da.Fill(ds);
           if (ds.Tables[0].Rows.Count > 0)
           {
               DDL_Batch.DataSource = ds;
               DDL_Batch.DataTextField = "Batch_Year";
               DDL_Batch.DataValueField ="BatchId";
               DDL_Batch.DataBind();
               DDL_Batch.Items.Insert(0, "Select");
           }
       }
       catch (Exception ex)
       {
           HandleException.SendMail(ex);
       }
   }
 
Share this answer
 
try below code

C#
protected void BindCountryDropDown()
   {
       DataTable dt = null;
       using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["tempdbConn"].ConnectionString))
       {
           using (SqlCommand cmd = conn.CreateCommand())
           {
               cmd.CommandType = CommandType.Text;
               cmd.CommandText = "SELECT  Batch_Year,BatchId FROM Batch";
               using (SqlDataAdapter da = new SqlDataAdapter(cmd))
               {
                   dt = new DataTable();
                   da.Fill(dt);
               }
           }
       }
       ddlCountry.DataSource = dt;
       ddlCountry.DataTextField = "Batch_Year";
       ddlCountry.DataValueField = "BatchId";
       ddlCountry.DataBind();
       ddlCountry.Items.Insert(0, new ListItem("Select Batch"));
   }
 
Share this answer
 
it is not working AdityaPratapSingh
 
Share this answer
 
Comments
AdityaPratapSingh 24-Jan-13 1:34am    
first u have to create connection string like this

SqlConnectionString con= new SqlConnectionString("Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;");

then use it in your code
Mohd. Mukhtar 24-Jan-13 1:35am    
Don't put your comment as solution.

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