5
Reply

DropDownList fill from database

Rashid Khan

Rashid Khan

Feb 11 2010 2:54 AM
7.8k
i m trying to fill dropdown list from database  but i m facing this error
"The SelectCommand property has not been initialized before calling 'Fill'"
this is my code
please help me out , where  i m making mistake
 
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection datacon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLink"].ConnectionString);
            datacon.Open();
            SqlCommand datacmd = new SqlCommand("usp_SelectAllDepartment",datacon);
            datacmd.CommandType = CommandType.StoredProcedure;
            
            datacmd.Parameters.AddWithValue("@deptId", 0);
            DataSet ds = new DataSet();
            SqlDataAdapter adpt = new SqlDataAdapter();
            
            adpt.Fill(ds);
                                   
            ddlDept.DataSource = ds;
            ddlDept.DataTextField = "DeptName";
            ddlDept.DataValueField = "DeptId";
            ddlDept.DataBind();
            
            datacon.Close();
        }


    }

thanks in advance

Answers (5)