In this
article we will see how to bind DropDownList in 3-tier architecture from
database in dot net. Here I have created demo example to understand easily. I
know that it’s easy to bind from database but its tutorial for junior developers
who are learning dot net control. There are many more control in dot net to
learn more from that but in this article we will see about how to bind
DropDownList control in 3-tier architecture.
In 3-tier
architecture we know that there are 3 layers to create for DAL, BAL and
Presentation layer. In DAL (Data access layer) we will create Method for
retrieve data from database and return dataset or datable to BAL (Business
Access Layer) after that we will pass Dataset or DataTable to Presentation
layer for bind our DropDownList control. Presentation layer is nothing but
where you can design your webpage. In DAL layer we will do for Database
Connection to retrieve data from database.
In BAL
Layer we will do like get data from DAL Layer and Pass it to Presentation
layer. Or one more Layer is there for setting all property to get and set in
BLL (Business Logic Layer). Here I have create my connection string in
Web.Config file to use from that file so we don’t have to bind more than one
where we want to create connection any get data from database.
<connectionStrings> <add connectionString="DatabaseConnectionString" name="DBCS" providerName="System.Data.SqlClient"/> </connectionStrings>
In this example we will bind DropDownList control from dataset using store procedure. Here I have Create Classes for DAL, BAL, BLL and My Web Form. Drag DropDownList Control in your Webpage.
Store Procedure to get Country Data from
Database:-
create procedure SP_DDPCountry as begin select CountryID,CountryName from Country; end
DAL Class:-
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; public DataSet BindDDPCountry(BLLClass bll) { using (SqlConnection con = new SqlConnection(CS)) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SP_DDPCountry"; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = con; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); return ds; } }
BAL Class:-
public DataSet BindDDPCountry(BLLClass bll) { DAL.DALClass dal = new DAL.DALClass(); DataSet ds = dal.BindDDPCountry(bll); return ds; }
Presentation Layer (Webpage):
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bindcountry(); } } public void bindcountry() { BLL.BLLClass bll = new BLL.BLLClass(); DataSet ds = bll.BindDDPCountry(bll); DDPCountry.DataTextField = "CountryName"; DDPCountry.DataValueField = "CountryID"; DDPCountry.DataSource = ds; DDPCountry.DataBind(); }
Here DataTextField and DataValueField is nothing but DropDownList Property to Set Text and Value field in the DropDownList Control so User Can easily see Text value in this control and perform to any action we can set value field for doing action on it.
Now you’re
done to run your web application in any browser there you can see data in
DropDownList control which is coming from your database.
1 comments:
commentsNice
ReplyThanks for comments.....