Click here to Skip to main content
14,644,614 members
Rate this:
Please Sign up or sign in to vote.
See more: , +
Hi.
i intend to insert a table column from sqlserver into the dropdownlist ,
my table(class)inlude 3 items:1-employee 2- teacher 3-both of them

and for this matter i have used DataReader as below:
if (!IsPostBack)
            {
                using (SqlConnection conn =new SqlConnection(ConfigurationManager.ConnectionStrings["DBMarashi"].ConnectionString))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("select ClassId,ClassName from Class",conn);
                    SqlDataReader dr = cmd.ExecuteReader();
                    SqlDataAdapter sad = new SqlDataAdapter(cmd);
                    dr.Read();
                    ClassDrpDown.DataSource = dr;
                    ClassDrpDown.DataTextField = "ClassName";
                    ClassDrpDown.DataValueField = "ClassId";
                    ClassDrpDown.DataBind();
                }
            }


but after i debug the following code , in dropdownlist only 2 items will appear:
2-teacher 3-both of them , where is the 3rd one ? and how can i retrieve it?
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

You called dr.Read, which read the first value. Then the last two values had not been returned, so they ended up in your drop list. Remove that code, it's completely wrong. Using a SqlDateReader is also wrong, but, it's clear you're just learning, so play with it if you like. A dataset is what you should use, though.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Hi,
Try this:
if (!IsPostBack)
{
    using (SqlConnection conn =new SqlConnection(ConfigurationManager.ConnectionStrings["DBMarashi"].ConnectionString))
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("select ClassId,ClassName from Class",conn);
        SqlDataAdapter sad = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable() ;
        sad.Fill(dt);
        ClassDrpDown.DataSource = dt;
        ClassDrpDown.DataTextField = "ClassName";
        ClassDrpDown.DataValueField = "ClassId";
        ClassDrpDown.DataBind();
    }
}



--Amit
   

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




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