绑定下拉到3层体系结构中的sql表列 [英] Bind Drop down to a sql table column in 3 tier architecture

查看:79
本文介绍了绑定下拉到3层体系结构中的sql表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用3层第一次想要将我的dropdownlist与sql表列绑定。

我在APPCODE BLL文件夹中(空)在DAL中有两个内置的classess Sqlhelper .cs和DBBridge.cs。

应用代码

BLL

<小> DAL

sqlhelper.cs

DBBridge.cs





在表示层下拉列表存在时。

所以如何绑定我的下拉列表?

我是否需要在sqlhelper或DBBridge.cs中创建一个函数或者我需要让类成为BLL文件夹中的一个函数吗?

或者我需要通过向导直接绑定下拉列表吗?在专业编程中绝对不能接受..

I am using 3 tier first time the first thing i want to bind my dropdownlist with a sql table column.
I have in APPCODE BLL folder (empty) while in DAL two built in classess Sqlhelper.cs and DBBridge.cs.
App code
BLLempty
DAL
sqlhelper.cs
DBBridge.cs


while on presentation layer drop down list present.
so how can i bind my dropdownlist?
do i need to make a function in sqlhelper or DBBridge.cs or i need to make class then a function in BLL Folder??
or do i need to bind dropdown directly through wizard ?? which is absolutely not acceptable in professional programming..

推荐答案

Hi Muhammad,



你可以这样做通过以下两种方式。



1。通过使用ListItemCollection



在SqlHelper中从数据库中获取数据,然后按照以下方式构建ListItemCollection。

在您的SqlHelper类中,您需要添加以下命名空间 System.Web.UI.WebControls





Hi Muhammad,

You can do this by following two ways.

1. By using ListItemCollection

In SqlHelper fetch the data from database and then build ListItemCollection by following way.
In Your SqlHelper class you need to Add the following namespace System.Web.UI.WebControls.


Public ListItemCollection GetData()
{
//Code to fetch data from database.
 ListItemCollection collection = new ListItemCollection();
//Now loop the rows retrieved from db.

foreach(var row in db.rows)
{
//First Parameter of ListItem refers the DataTextField of Dropdown;
//Second Parameter of ListItem refers to DataValueField of Dropdown.
 collection.Add(new ListItem(row[0],row[1]));
}

return collection
}





现在在你的演示层中







Now in your presentation layer


ListItemCollection col  = GetData();
dropdown.datasource = col;
dropdown.DataBind();







2使用DataTable。



在你的SqlHelper中返回数据表








2 By using DataTable.

In Your SqlHelper return the Datatable


public DataTable  GetData()
{
    DataTable dt  = new DataTable();
    dt  = //fetched data from database. 
    
return dt;
}





在您的演示层中。





In Your Presentation Layer.

DataTable  dt  = GetData();
dropDown.DataSource  = dt;
// Here you have to explicitly set these two properties, because now we are getting datatable //form GetData()

dropdown.DataTextField = dt.col[0]; // Display Member
dropdown.DataValueField= dt.col[1]; // Value Member
dropdown.DataBind();





我们也可以通过使用Dictionary来完成。

如果你想通过词典实现这个目的,请留下评论。





希望这有助于。



we can also do this by using Dictionary also.
Leave the comment if you want to achieve this by using Dictionary.


Hope this Helps.


Hi Muhammad,



我认为您应该从函数返回一个列表/表,然后将其绑定到下拉列表。


sqlhelper.cs中的


Hi Muhammad,

I think you should return a list/table from a function and then bind it to the dropdownlist.

in sqlhelper.cs
public DataTable GetData(parameters)
{
DataTable dt=new DataTable;
//implement logic for Data Retrieval.

return dt;
}

in Presentation Layer give a call to this function and bind the dropdownlist;

DataTable Dt=GetData(parameters);
Drplist.DataSource=Dt;
DrpList.DataTextField=Dt.Col[0].ToString(); //Text displayed in DropDown
DrpList.DataValueField=Dt.Col[1].ToString();//Backend Value associated with Text i.e include PK.
DrpList.DataBind();





我希望这能解决你的问题。



注意:通过向导直接绑定下拉列表不是一个好的解决方案。



I hope this will solve your problem.

Note :Binding dropdown directly through wizard is not a good solution.


你是否添加引用相关的dll文件??

首先,您已将 BLL 的引用添加到 DAl

,在这种情况下您必须创建对象:

Did you Add Reference the related dll files??
Firstly you have add reference to BLL to DAl
in that case you have to create object:
business.classname objclass=new business.classname();
 objclass.Methodname();



它可以访问你的方法


its way to you can access your methods


这篇关于绑定下拉到3层体系结构中的sql表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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