Rashid Khan

Rashid Khan

  • NA
  • 159
  • 0

DropDownList fill from database

Feb 11 2010 2:54 AM
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)