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,
BLL contains business logic of your code, so what you need to do is:
1.Create a function in DAL to retrieve the data.
2. Create a function in BAL and apply any conditions or business logics if any. From BAL, you will call your DAL function and pass the parameters to BAL. From this BAL function return the data you get from DAL function to your page.
3. Just call the BAL function and bind the dropdown from the data returned from BAL.
This is already resolved by some one in asp.net forum
If i summarize your requirement, it would be something as follows:
1. A BusinessEntity (Employee, Student, Product) with simple properties
2. A BLL.ManagerClass which creates a of BusinessEntity (from step 1)
3. An ObjectDataSource within the WebPage (withe DLL) calls BLL.ManagerClass (from step 2) to get the
4. Set the DataSource of the DDL to the ObjectDataSource
5. Set the DataTextField and DataValueField of the DDL to the respective BusinessEntity fields
6. You have it without binding your table to the DDL
For further help, have a look at the following link:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=420
Or Refer to below code.
//using 3 tier architecture BLLCommonTasks newCountries = new BLLCommonTasks(); DataTable dtCountry = PopulationOverview.BLL.BLLCommonTasks.getAllCountriesList(); ddlCountries.DataSource = dtCountry; ddlCountries.DataTextField = "country_name"; ddlCountries.DataValueField = "country_id"; ddlCountries.DataBind();
//BLL public static DataTable getAllCountriesList() { return DAL.commonTasks.getAllCountriesList(); }
//DAL public static DataTable getAllCountriesList() { bool status = false; DBCon sqlCon = new DBCon(); SqlCommand sqlcmd = new SqlCommand("spSelectAllCountries"); sqlcmd.Connection = sqlCon.OpenCon(); sqlcmd.CommandType = CommandType.StoredProcedure; SqlDataReader rdr = sqlcmd.ExecuteReader(); DataTable dtTable = null; if (rdr.HasRows) { dtTable = new DataTable("countries"); dtTable.Load(rdr); status = true; return dtTable; } if (sqlcmd != null) { sqlcmd.Dispose(); sqlcmd = null; } return dtTable; }
string cs = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand sqlcmd = new SqlCommand("select * from Exam", con);
con.Open();
ddlExam.DataSource = sqlcmd.ExecuteReader();
ddlExam.DataTextField = "ExamNo";
ddlExam.DataValueField = "ExamName";
ddlExam.DataBind();
}
i have added this c# code in code behind. but do now how to do more!
Hi,
In data access layer add like this:
public DataSet BindDropDown(string id, string statename, DropDownList drpstate)
{
SqlParameter[] param = new SqlParameter[0];
{
ds = SqlHelper.ExecuteDataset(clscon.Con(), "Store procedure", param);
ds.Tables[0].TableName = "Table name";
drpstate.DataSource = ds.Tables[0];
drpstate.DataTextField = ds.Tables[0].Columns[statename].ColumnName.ToString();
drpstate.DataValueField = ds.Tables[0].Columns[id].ColumnName.ToString();
drpstate.DataBind();
drpstate.Items.Insert(0, new ListItem("[Select State]", "-1"));
drpstate.SelectedIndex = -1;
}
return ds;
}
and on page load
clsdal.BindDropDown("Valuefield", "Textfield", dropdownlist Id);
hope this help.
沒有留言:
張貼留言