来自BLL的填充下拉框

时间:2014-04-26 11:45:38

标签: c# asp.net sql

如何从数据库中填充DropDown?我有UI和BLL图层和sp,但无法填充它。下拉名称是" DrpdwnComplainantTypes"。我已经做了很多次这种事情,但这次不是。请帮助。

protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            ComplainantBizz comBizz = new ComplainantBizz(txtName.Text, txtCNIC.Text, txtAddress.Text, txtContact.Text, Convert.ToInt32(DropdwnCompType.SelectedValue));
            ManageComplainant mngComplainantType = new ManageComplainant();

            bool Result = mngComplainantType.Insert(comBizz);


            if (Result == true)
            {
                HiddenFieldSetMessage.Value = "Saved";
                HiddenFieldShowMessage.Value = "True";
                Clear(txtName);
            }
            else
            {
                HiddenFieldSetMessage.Value = "RecordAlreadyExists";
                HiddenFieldShowMessage.Value = "True";
            }
        }
        catch (Exception)
        {
            HiddenFieldSetMessage.Value = "NotSaved";
            HiddenFieldShowMessage.Value = "True";
        }
    }

BLL:

public DataTable FillDropDown()
        {
            SqlCommand cmd = new SqlCommand("SelectAllComplainants_SP", DataBaseConnection.OpenConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            //SqlParameter pName = new SqlParameter("@Name", comBizz.Name);
            //cmd.Parameters.Add(pName);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataBaseConnection.CloseConnection();
            return ds.Tables[0];
        }

SP: 这是我打电话的SP。

 alter  PROCEDURE SelectAllComplainantTypes_SP


AS
BEGIN

    Begin Try
      Select * from ComplainantTypes 
    End Try
    Begin Catch
     Select ERROR_MESSAGE() as ErrorMsg
    End Catch

END
GO

1 个答案:

答案 0 :(得分:0)

您需要将函数的数据表返回绑定到下拉列表,如

dropdonlist.datasource=FillDropDown();
dropdownlist.datatextfield="yourTextColum";//in example column name "City"
dropdownlist.dataValueFiedl="yourValueColumn";//in example cloum name "ID"
dropdownlist.databind();

这就是全部

注意:如果您将从函数返回null,则抛出异常,如果没有数据返回,则下拉列表中不会显示任何数据