请立即解决这个问题 [英] Please give the solution for this problem immediatly

查看:79
本文介绍了请立即解决这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//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 



//


//

推荐答案



代替此代码

Hi,
instead of this code
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

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.


你应该加载ID&名称字段。所以编写查询,例如

You should load both ID & Name fields. So write query like
string strQry = "SELECT ID_Field, Name_Field FROM TableName";



DataTable中加载数据


Load the data in DataTable

OleDbCommand cmd = new OleDbCommand(strQry, open());
OleDbDataAdapter adapt = new OleDbDataAdapter(cmd);
adapt.Fill(dt);



并绑定下拉列表中的数据


And bind the data in Dropdownlist

Dropdownlist1.DataSource = dt;
Dropdownlist1.DataTextField = "Name_Field";
Dropdownlist1.DataValueField = "ID_Field";
Dropdownlist1.DataBind();



可能你需要开始学习C#& ASP.NET立即避免这些情况,因为这些都是非常基本的东西。我建议你这样做。

这里有很多方法可以在网上学习任何东西。

需要教育 [ ^ ]


这篇关于请立即解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆