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
Loading
dhilipan nairPosted Feb 14, 2010, 9:16 AM
u r not specified any parameter in ur procedure,u jus hav "select * from deparment".
but why do u pass a parameter in the command object.So i think that must be the reason.chk out!!!
Amit ChoudharyPosted Feb 11, 2010, 6:02 AM
your have missed a little thing.. add the command object to the adapter as below:
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(datacmd);
adpt.Fill(ds);
ddlDept.DataSource = ds;
ddlDept.DataTextField = "DeptName";
ddlDept.DataValueField = "DeptId";
ddlDept.DataBind();
datacon.Close();
}
}
Lalit MPosted Feb 14, 2010, 11:52 PM
C#
da.SelectCommand.CommandText ="Select * from ";
or
da = new SqlDataAdapter ("Select * from ", cn);
jitendra kumarPosted Feb 12, 2010, 2:26 AM
see the link below
http://forums.asp.net/t/1122540.aspx
Rashid KhanPosted Feb 11, 2010, 7:09 AM
but still problem in this code on same code line but this time new error
" Procedure usp_SelectAllDepartment has no parameters and arguments were supplied"
i execute that SP its working in database side.
ALTER PROCEDURE dbo.usp_SelectAllDepartment
As
select * from Department
again i m making some silly mistake please tell me
Thanks once again
Regards
Rashid