Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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..
Posted
Comments
Member 10425929 13-Dec-13 1:06am    
dropdown.DataBind(); this code not working on windows application ..how to resolve this error

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.


C#
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


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



2 By using DataTable.

In Your SqlHelper return the Datatable


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


In Your Presentation Layer.

C#
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();


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


Hope this Helps.
 
Share this answer
 
Hi Muhammad,

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

in sqlhelper.cs
C#
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.
 
Share this answer
 
v2
Comments
Muhamad Faizan Khan 6-Sep-13 3:03am    
no work of bll??
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:
C#
business.classname objclass=new business.classname();
 objclass.Methodname();

its way to you can access your methods
 
Share this answer
 
v4
1. Write a method in DAL to get data from database.
2. Write a method in BLL, Call the above method from write your business if there is any ...
3. Call the method which you wrote in BLL and bind it to the drop dwon list.
 
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