binding dropdownlist in 3-tier [Resolved]

Posted by Anishabathija under ASP.NET on 11/1/2012 | Points: 10 | Views : 8509 | Status : [Member] | Replies : 2
hey ,

Could you please help me with a code to bind a dropdownlist from a column from a table in 3 tier architecture.

thanks




Responses

Posted by: Dharanidhar on: 11/1/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Here is the code:-
//This is an Sample code.
Table has 3 columns
1.AutoId(Autoincrement column)
2.FirstName(varchar(50))
3.LastName(varchar(50))
Here I am binding FirstName column values to the dropdownlist.
DAL 

public class Dropdown
{
string _connstr = ConfigurationManager.ConnectionStrings["Connstr"].ConnectionString;
public DataTable LoadAll()
{
DataTable table = new DataTable();
using (SqlConnection con = new SqlConnection(_connstr))
{
string sql = "Select AutoId,FirstName from PersonDetails ORDER BY AutoId";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
adp.Fill(table);
}
}
}
return table;
}
}
BAL
public DataTable LoadAll()
{
DropDownList1.DAL.Dropdown drop = new Dropdown();
return drop.LoadAll();
}
MainWebPage(.aspx)
//set the columname which u want to find as DataTextField and Primarykey or Autoincrement column as DataValueField for the dropdownlist
<asp:DropDownList ID="dropdown1" runat="server" DataValueField="AutoId" DataTextField="FirstName"></asp:DropDownList>
code behind(.cs)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadAllData();
}

}
private void LoadAllData()
{
DropDownList1.BAL.Class1 drop = new Class1();
dropdown1.DataSource = drop.LoadAll();
dropdown1.DataBind();
}

Regards
Dharanidhar.

Anishabathija, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anishabathija on: 11/2/2012 [Member] Starter | Points: 25

Up
0
Down
Thanks :)


Anishabathija, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response