Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 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 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.

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 437
1 sanket saxena 355
2 thatraja 240
3 Abhinav S 216
4 CPallini 215
0 Sergey Alexandrovich Kryukov 8,784
1 OriginalGriff 5,025
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid