C# .NET - dropdownlist

Asked By praveen pubby on 28-Oct-10 03:02 AM
i m using one dropdownlist in aspx and i hv created a table in sql server now i tryng to bind the data to this dropdown list ...so tht  i hv givn connection ...ERROR occuring !!

private void BindData()
    {
      string connStr = ConfigurationManager.ConnectionStrings["MyDbConn1"].ConnectionString;
      SqlConnection conn = new SqlConnection(connStr);
      SqlDataAdapter ad = new SqlDataAdapter("select * from [state]", conn);
      DataTable dt = new DataTable();
      ad.Fill(dt);
      DropDownList.DataSource = dt;
      DropDownList.DataBind();
    }

    protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
      
    }


Error   1   A using namespace directive can only be applied to namespaces; 'System.Web.UI.WebControls.BaseDataBoundControl' is a type not a namespace 
Nowshad M replied to praveen pubby on 28-Oct-10 03:05 AM
Hi,
Have you any where used this namespace 

System.Web.UI.WebControls.BaseDataBoundControl

Or can you tell me where exactly this error is happening..
praveen pubby replied to Nowshad M on 28-Oct-10 03:09 AM
yea evn i to tried tht ...bt no use of it !!!

i hv givn error nah ... hv displayed in tht previous scrap ..same repeating

did yu checked tht code of bindata..!!!
 thr i hv kept

dropdownlist.datasource =
dropdownlist.databind =

is it ryt ..???
help me yar !!
Jatin Prajapati replied to praveen pubby on 28-Oct-10 03:12 AM
At the top of the cs file check that if you have written

using System.Web.UI.WebControls.BaseDataBoundControl

If it's there then remove it.


Nowshad M replied to praveen pubby on 28-Oct-10 03:22 AM
Hi,

Please remove the namespace System.Web.UI.WebControls.BaseDataBoundControl and then try.

Your binding datasource code is correct and also you have to mention the 

dropdownlist.DataTextField=ColumnName;
dropdownlist.DataValueField=ColumnName;

Write these before dropdownlist.databind();

Reena Jain replied to praveen pubby on 28-Oct-10 04:19 AM
hi,

try this

private void BindData()
    {
    string connStr = ConfigurationManager.ConnectionStrings["MyDbConn1"].ConnectionString;
    SqlConnection conn = new SqlConnection(connStr);
    SqlDataAdapter ad = new SqlDataAdapter("select * from [state]", conn);
    DataTable dt = new DataTable();
    ad.Fill(dt);
    DropDownList1.DataSource = dt;
    DropDownList1.DataBind();
    }

if you are using windows application use this

private void BindData()
    {
    string connStr = ConfigurationManager.ConnectionStrings["MyDbConn1"].ConnectionString;
    SqlConnection conn = new SqlConnection(connStr);
    SqlDataAdapter ad = new SqlDataAdapter("select * from [state]", conn);
    DataTable dt = new DataTable();
    ad.Fill(dt);
    DropDownList1.DataSource = dt;  
DropDownList1.DataTextField = "FieldName";
DropDownList1.DataValueField = "FieldName";
DropDownList1.DataBind();
}

hope this will help you
praveen pubby replied to Reena Jain on 28-Oct-10 06:03 AM
protected void Page_Load(object sender, EventArgs e)
    {
      FillDropdownList();
    }

    private void FillDropdownList()
    {
      string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
      SqlConnection conn = new SqlConnection(connStr);
      SqlDataAdapter ad = new SqlDataAdapter("select * from [statte]" , conn);
      DataSet dset = new DataSet();
            ad.Fill(dset);
            DropDownList.DataSource = dset;
            DropDownList.DataTextField = "statte";
            DropDownList.DataBind();
          
        
    }