Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C#
//this is presentation Layer Code
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
            DataTable dt = new DataTable();
            dt = bz.LoadValue();
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                ddl_CmpId.DataSource = dt.Rows[i]["CmpId"].ToString();

                ddl_CmpId.DataBind();
             
            }
            }

        }
//This is Buisness Layer Code
 public DataTable LoadValue()
        {
            try
            {
                return da.load();
            }
            catch
            {
                throw;
            }
            finally
            {
                da = null;
            }
}
//This is Data Access Laye Code
  public DataTable  load()
        {
            DataTable dt=new DataTable();
           try
            {
                string SelQry = "select CmpId from cmpDetails";
                return cl.LoadCtrl(SelQry);
              
               
            }
            catch
            {
                throw;
            }
            finally
            {
                cl = null;
            }
        }
//This Is Common Class Code
 public DataTable LoadCtrl(string Sel )
           {
               OleDbCommand cmd = new OleDbCommand(Sel, open());
               OleDbDataAdapter adapt = new OleDbDataAdapter(cmd);
               adapt.Fill(dt);
               return dt;
           }

CmpId Stored in DB xxx001
                   yyy002
but when load the dropdown control the cmpany id are loaded this order
                                                      x
                                                      x
                                                      x
                                                      0
                                                      0
                                                      1
                                                      y
                                                      y
                                                      y

                                                      0
                                                      0
                                                      2
please give the solution immediatly for this issues 

//
Posted

Hi,
instead of this code
C#
dt = bz.LoadValue();
for (int i = 0; i < dt.Rows.Count; i++)
{
    ddl_CmpId.DataSource = dt.Rows[i]["CmpId"].ToString();
    ddl_CmpId.DataBind();
}

Try this code
C#
dt = bz.LoadValue();
ddl_CmpId.DataSource = dt;
ddl_CmpId.DataTextField = "Display text field name from db";
ddl_CmpId.DataValueField = "Value field name from db";
ddl_CmpId.DataBind();

Hope it helps you.
Thanks.
 
Share this answer
 
You should load both ID & Name fields. So write query like
C#
string strQry = "SELECT ID_Field, Name_Field FROM TableName";

Load the data in DataTable
C#
OleDbCommand cmd = new OleDbCommand(strQry, open());
OleDbDataAdapter adapt = new OleDbDataAdapter(cmd);
adapt.Fill(dt);

And bind the data in Dropdownlist
C#
Dropdownlist1.DataSource = dt;
Dropdownlist1.DataTextField = "Name_Field";
Dropdownlist1.DataValueField = "ID_Field";
Dropdownlist1.DataBind();

Probably you need to start learn C# & ASP.NET immediately to avoid these situations because these're very basic things. I'm suggesting this for your own good.
Here more than couple of ways to learn anything in Internet.
Education Needed[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900