2
Reply

Class Implement and Factory

Rashid Khan

Rashid Khan

Mar 11 2010 1:46 AM
2.2k
i create a class with fill data set and i used this class in my aspx.cs page,
but for this i have to write 4 extra lines for data binding with dropdownlist.
so i want to create factory which take SP, and values and return filled dropdown list and for this it takes Dataset from FILLDS class.
here is my code for class:

public class Myclass
{
    #region Global variable declaration
    SqlConnection con;
    SqlCommand cmd;
    DataSet ds;
    SqlDataAdapter adpt;
    #endregion
 #region function filling dataset

    public DataSet FillDS (SqlConnection con, string spName, SqlParameter[] param)
    {
        if (con != null && !string.IsNullOrEmpty(spName) && param.Length > 0)
        {
           
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            cmd = new SqlCommand(spName, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd = AddParameterInCommand(cmd, param);

            ds = new DataSet();
            adpt = new SqlDataAdapter(cmd);
            adpt.Fill(ds);

            con.Close();
            return ds;
        }
        else
        {
            return null;
        }
    }

    #endregion




    #region funtion for adding parameters in command object

    private SqlCommand AddParameterInCommand(SqlCommand cmd, SqlParameter[] param)
    {
        if (cmd != null && param.Length > 0)
        {
            foreach (SqlParameter p in param)
            {
                if (p.Value == null)
                {
                    p.Value = DBNull.Value;
                }
                cmd.Parameters.Add(p);
            }
        }
        return cmd;
    }
    #endregion

    #region funcatiom for databinding(it might be completely wrong)

    public void getDL(string spName, string Value1, string value2)
    {
        DropDownList ddl1 = new DropDownList();
        ddl1.DataSource =FillDS();(error comes in this line :No overload for method 'FillDS' takes '0' arguments)
        ddl1.DataTextField = Value1 ;
        ddl1.DataValueField = value2  ;
        ddl1.DataBind();

    }
  
    #endregion

on aspx.cs page:

public partial class ClaasImpliment : System.Web.UI.Page
{
    #region object for data function class

    Myclass objMC = new Myclass();

    #endregion
private void FillList()
    {
        //if (IsPostBack)
        //{
        SqlParameter[] param = new SqlParameter[1];
        param[0] = new SqlParameter("@deptId", SqlDbType.NChar, 20);
        param[0].Value = 0;

        DataSet ds = objMC.FillDS(objMC.Connect(), "usp_SelectDepartmentById", param);
      
            if (ds != null)
            {
                objMC.getDL("usp_SelectDepartmentById","DeptName","DeptId",ddldeptname );

   }

please check it out nd tell me where is the problem,. i m so confused so making mistakes more nd more
Thanks in adv

Answers (2)