Click here to Skip to main content
Rate this: bad
good
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 at 24-Jan-13 1:16am
   
try to elaborate the question?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

it is not working AdityaPratapSingh
  Permalink  
Comments
AdityaPratapSingh at 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 at 24-Jan-13 1:35am
   
Don't put your comment as solution.
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  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 210
1 kbrandwijk 100
2 Richard MacCutchan 100
3 ProgramFOX 80
4 Mukesh Bhagat 65
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2014
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