Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi, I am wondering what is the best way to populate a UI dropdown from a sql database table.

Is it best to have a specific method in the data access layer for each and every drop down in the UI and use a data reader?

Do I then call this method from the middle layer and then call this from the UI form?

How do I pass the returned data up from the data access layer to the UI?

Thank you.
Posted

It depends upon your requirement.You can create a single stored procedure for getting all the drop down data in your application.And a single service method that call the sp and return the data .You can create a common class called DropDownList.Which may look like this

C#
Public Class DropDownList
{
public int DataVlaue{get;set;}
Public string DataText{get;set;}
}
 
Share this answer
 
v2
Comments
DHicks19 14-Nov-14 5:30am    
so the best way to do this is to create a method in data access layer called "GetColours", this then calls a stored procedure on sql server.

I then create a dataset in this method and return this to the business logic layer method which is then called from the ON load form event of the UI?
IpsitaMishra 14-Nov-14 5:35am    
yes.
DHicks19 14-Nov-14 6:56am    
My code stops executing at da.fill and goes no further, no errors. Can you see the problem?

using (Conn)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand("GetColours", Conn);
cmd.CommandType = CommandType.StoredProcedure;

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds, "Colour");


return ds;

}

Never mind, fixed it. There was an issue with the connected.
What is the best way to handle exception for this method?
IpsitaMishra 14-Nov-14 7:08am    
see the following link

http://programcall.com/7/aspnet/binding-dropdownlist-with-dataset-values-in-aspnet.aspx
IpsitaMishra 14-Nov-14 7:13am    
Here inline query is used .You can use sp like this
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spName";
I think these articles will give you and idea how to update the UI based on database values:

Silverlight Application with Management Entity Framework and WCF Service

And,

MVVM Silverlight Application with Entity Framework and WCF Service[^]

After reading these articles and experimenting with the source code, please feel free to let me know if you have any other questions.
 
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