How to implement Generics in business object class definition with DAL to create a proper user control dropdown
-
I am woefully new to generics, being tied to the support of a corporate intranet web application whose upgrade process is bound to red tape and slowwwly-changing standards. Consequently, today (thankfully!) I finally find myself scrambling during our upgrade to .Net 3.5 and transitioning all the code I can to a properly tiered model. I have been reading all morning about generics trying to digest how to transition dropdown user controls into a proper business object that gets its data from a class in the data access layer. There is a perfectly succinct question here that details exactly what I am interested in exploring: http://stackoverflow.com/questions/4156037/set-selected-index-in-a-dropdownlist-in-usercontrol. What I would love to see, however, is what Travel_CarSizes.GetCarSizes() actually looks like inside and how the class Travel_CarSizes is defined. (I am having a hard time with <T> and knowing where it should occur.) For my own specific circumstance at the moment I need a dropdown user control to contain location directionals (N, S, W, C/O, NW, SE, etc) that are stored in a SQL table in the DB and whose selected index needs to be able to be set by whichever page it happens to be in, when form data exists. I have begun to implement the model in the example from the link above but right now without using Generics because I can't figure it out: The dropdown user control: public partial class DropDownStreetPrefix : System.Web.UI.UserControl { public string StreetPrefixValue { get { return ddlStreetPrefix.SelectedValue.ToString(); } set { Bind(); ddlStreetPrefix.SelectedIndex = ddlStreetPrefix.Items.IndexOf(ddlStreetPrefix.Items.FindByValue(value)); } } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Bind(); } } private void Bind() { if (ddlStreetPrefix.Items.Count == 0) { SqlDataReader rdr = StreetDirectionals.GetDirectionals(); ddlStreetPrefix.DataSource = rdr; ddlStreetPrefix.DataBind(); ddlStreetPrefix.DataValueField = "StreetSuffixPrefixAbbr"; ddlStreetPrefix.DataTextField = "StreetSuffixPrefixAbbr"; ListItem li = new ListItem("", ""); ddlStreetPrefix.Items.Insert(0, li); ddlStreetPrefix.SelectedIndex = 0; } } } The StreetDirectionals class: public class StreetDirectionals { private StreetDirectionals () { } public static SqlDataReader GetDirectionals () { string sqlText = "SELECT StreetSuffixPrefixAbbr FROM common..tblStreetSuffixPrefix " + "ORDER BY StreetSuffixPrefixAbbr"; SqlDataReader rdr = SqlClient.ExecuteFetchReturnDataReader( theConnectionString, CommandType.Text, sqlText); return rdr; } } I will separate out the database interaction inside the StreetDirectionals class as soon as I can figure out how to change its code if I were to transform the Bind() method from my dropdown user control into this: private void Bind() { if (!IsPostBack) { **List<StreetDirectionals> sd = StreetDirectionals.GetDirectionals();** ddlStreetPrefix.DataSource = sd; ddlStreetPrefix.DataTextField = "StreetSuffixPrefixAbbr"; ddlStreetPrefix.DataValueField = "StreetSuffixPrefixAbbr"; ddlStreetPrefix.DataBind(); } } Any assistance would be sooo much appreciated!
-
Answer:
public class StreetDirectional { public string StreetSuffixPrefixAbbr { get; set; } public static IEnumerable<StreetDirectional> GetDirectionals () { string sqlText = "SELECT StreetSuffixPrefixAbbr FROM common..tblStreetSuffixPrefix " + "ORDER BY StreetSuffixPrefixAbbr"; SqlDataReader rdr = SqlClient.ExecuteFetchReturnDataReader( theConnectionString, CommandType.Text, sqlText); var list = new List<StreetDirectional>(); while (rdr.Read()) { var item = new StreetDirectional() { StreetSuffixPrefixAbbr = (string)rdr["StreetSuffixPrefixAbbr"] }; list.Add(item); } return list; } } then you can do this ddlStreetPrefix.DataSource = StreetDirectional.GetDirectionals();
Emmie at Stack Overflow Visit the source
Related Q & A:
- How to dynamically create a PHP class name on the fly?Best solution by stackoverflow.com
- How to properly reference a user control and dynamically add it to page?Best solution by Stack Overflow
- Do you have to create a business plan before starting the actual business?Best solution by Yahoo! Answers
- How do I create a guest ftp user and give access to specific sub-folder with SSH?Best solution by Server Fault
- How to create a flexible object?
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.