Click here to Skip to main content
12,399,640 members (52,048 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET DropDownList
c# code --Dropdown code
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
insert Batch values
(1,'2013'),(2,'2012'),
(3,'2011'),
(4,'2010'),
(5,'2009'),
(6,'2008'),
(7,'2007'),
(8,'2006'),
(9,'2005'),
(10,'2004')
Posted 23-Jan-13 19:06pm
sr_24484
Comments
Nandakishorerao 24-Jan-13 1:16am
   
try to elaborate the question?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

try this-

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);
       }
   }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

try below code

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"));
   }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

it is not working AdityaPratapSingh
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100