vmedhe2
vmedhe2

Reputation: 237

Getting two DataFields from SQL into the Text of a dropdownlist

I am trying to make it so that I can get two pieces of data from SQL Server into the Text Field of a drop down list. So if I get the data AccountID='1','2' and CompanyName='Build it inc','It ltd'. I want it to display:

1-Build it inc

2-It ltd

I can get the data from sql and get the DataValueField to AccountID but how do I display it. Here is my aspx.

<asp:DropDownList ID="DDLTemplates" DataValueField="AccountID" 
   DataTextField='<%#Eval("AccountID") + "-" + Eval("CompanyName")%>' 
   runat="server"></asp:DropDownList>

How do I do this?

Edit: Since alot of you are asking here is my code behind. Thanks for all the Help

    protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

        DDLTemplates.DataSource = GetItems();
        DDLTemplates.DataBind();
    }

}
public DataSet GetItems()
{
    String conn = ConfigurationManager.ConnectionStrings["LiquidusConnectionString"].ConnectionString;
    SqlConnection sqlConnection2 = new SqlConnection(conn);
    string oString = "Select AccountID, CompanyName from Account";
    SqlCommand cmd = new SqlCommand(oString);
    cmd.Connection = sqlConnection2;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    sqlConnection2.Open();
    da.Fill(ds);
    sqlConnection2.Close();
    return ds;
}

Answer Edit: psoshmo and ragerory Have found the Answer to my question. Here is my code now that it works:

<asp:DropDownList ID="DDLTemplates" DataValueField="AccountID" DataTextField="Company" runat="server"></asp:DropDownList>

Code Behind:

    protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

        DDLTemplates.DataSource = GetItems();
        DDLTemplates.DataBind();
    }

}
public DataSet GetItems()
{
    String conn = ConfigurationManager.ConnectionStrings["LiquidusConnectionString"].ConnectionString;
    SqlConnection sqlConnection2 = new SqlConnection(conn);
    string oString = "SELECT AccountID, (Convert(varchar,AccountId) + ' - ' + CompanyName) as company FROM Account";
    SqlCommand cmd = new SqlCommand(oString);
    cmd.Connection = sqlConnection2;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    sqlConnection2.Open();
    da.Fill(ds);
    sqlConnection2.Close();
    return ds;
}

Hope this helps future googler's :)

Upvotes: 2

Views: 102

Answers (2)

psoshmo
psoshmo

Reputation: 1550

Simply Create a field in your class that adds these together, and bind that to the DataTextField

something like:

public string YourFieldNameHere
{
  get { return String.Format("{0} - {1}", AccountID, CompanyName); }

}

EDIT: My bad for assuming you had a class file setup. In general, you should avoid having methods like GetItems() in your code for the web page. If you have to get those same records in another page, you will end up duplicating your code, which is bad practice. I would recommend you consider setting up class files to handle select statements like this, as well as other fields and methods you may need.

Upvotes: 0

ragerory
ragerory

Reputation: 1378

Based on your GetItems method, you can change the oString variable...

string oString = "SELECT AccountID, (Convert(varchar,AccountId) + ' - ' + CompanyName) as Company FROM Account";

And then change your control to the following

<asp:DropDownList ID="DDLTemplates" DataValueField="AccountID" DataTextField="Company" runat="server"></asp:DropDownList>

You're concatenating the two fields and making them one column called Company so that's what you should have as the TextField.

Upvotes: 1

Related Questions