Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement Generics in business object class definition with DAL to create a proper user control dropdown
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>There is a perfectly succinct question here that details exactly what I am interested in exploring: <a href="https://stackoverflow.com/questions/4156037/set-selected-index-in-a-dropdownlist-in-usercontrol">Set selected index in a Dropdownlist in usercontrol</a>.</p> <p>What I would love to see, however, is what <code>Travel_CarSizes.GetCarSizes()</code> actually looks like inside and how the class <code>Travel_CarSizes</code> is defined. (I am having a hard time with <code>&lt;T&gt;</code> and knowing where it should occur.)</p> <p>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.</p> <p>I have begun to implement the model in the example from the link above but right now <strong>without using Generics</strong> because I can't figure it out:</p> <p>The dropdown user control:</p> <pre><code>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; } } } </code></pre> <p>The <code>StreetDirectionals</code> class:</p> <pre><code>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; } } </code></pre> <p>I will separate out the database interaction inside the <code>StreetDirectionals</code> 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:</p> <pre><code>private void Bind() { if (!IsPostBack) { **List&lt;StreetDirectionals&gt; sd = StreetDirectionals.GetDirectionals();** ddlStreetPrefix.DataSource = sd; ddlStreetPrefix.DataTextField = "StreetSuffixPrefixAbbr"; ddlStreetPrefix.DataValueField = "StreetSuffixPrefixAbbr"; ddlStreetPrefix.DataBind(); } } </code></pre> <p>Any assistance would be sooo much appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload