1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

dynamic drop down list

Discussion in 'C#' started by netsailer76, Aug 30, 2010.

  1. netsailer76

    netsailer76 New Member

    Joined:
    Aug 30, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I am pretty new to C# but from vb background

    i have about 20 dropdowns to be filled in a page and got about 12 diff pages in which i need to fill the same way.

    so just to cut short the code

    i created a class as below. this class is placed under the app_code and i am trying to call the below class from my code page as PropertyMax.fnFillDropdown(ddlArea, "spGetArea", "AreaId", "Area", "Select Area");

    it shows a red line and is not working. any help would be greatly appreciated.

    Code:
    public class PropertyMax
    {
    
    public static void fnFillDropdown(DropDownList ddl, string spName, string dbFieldId, string dbFieldName, string DefaultSelText)
    {
    SqlConnection sqlConn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["PMDb"].ToString());
    sqlConn.Open();
    
    SqlCommand sqlCmd = new SqlCommand(spName, sqlConn);
    sqlCmd.CommandType = CommandType.StoredProcedure;
    
    SqlDataReader sqlRdr;
    sqlRdr = sqlCmd.ExecuteReader();
    
    if (sqlRdr.HasRows)
    {
    ddl.DataSource = sqlRdr;
    ddl.DataValueField = dbFieldId;
    ddl.DataTextField = dbFieldName;
    ddl.DataBind();
    ddl.Items.Insert(0, DefaultSelText);
    ddl.Items.FindByText(DefaultSelText).Value = "0";
    }
    else
    {
    ddl.Items.Insert(0, DefaultSelText);
    ddl.Items.FindByText(DefaultSelText).Value = "0";
    }
    
    sqlCmd.Dispose();
    sqlRdr.Dispose();
    sqlConn.Close();
    }
    
    }
     

Share This Page