SearchForKnowledge
SearchForKnowledge

Reputation: 3751

How to split sql query result to populate a dropdownlist

The DataSet looks like this:

Spec1                                           Spec2                                           Spec3   Spec4   Spec5   Spec6   Spec7   Spec8   Spec9   Spec10
<a href="/spe.aspx?id=10" title="AI">AI</a>     <a href="/spe.aspx?id=40" title="BA">BA</a>

I would like to populate a dropdownlist:

<asp:dropdownlist ID="ddl1" runat="server"></asp:dropdownlist>

so the HTML output is this (an entry for each column instead of each row):

<select>
    <option value="/spe.aspx?id=10">AI</option>
    <option value="/spe.aspx?id=40">BA</option>
</select>

Upvotes: 0

Views: 632

Answers (1)

ahwm
ahwm

Reputation: 692

You'll need to use something like this to parse it out. And you'll need to (probably) use a foreach loop and a second DataSet (or a Dictionary<string, string>) object which you can put the values into and then use to data bind your DropDownList.

EDIT: Without the HTML Agility Pack:

Dictionary<string, string> dict1 = new Dictionary<string, string>();
foreach (DataRow r in my.Tables[0].Rows)
{
    foreach (DataColumn c in my.Tables[0].Columns)
    {
        if (r[c] == DBNull.Value || r[c].ToString().Trim() == "")
           continue;
        string spec = r[c].ToString();
        string href = spec.Substring(spec.IndexOf("href=");
        href = href.Trim("\"").Substring(0, spec.IndexOf("\""));
        ....
        dict1.Add(href, val);
    }
}
ddl1.DataSource = dict1;
ddl1.DataBind();

You might be able to get an easier solution using the library but I'm not sure how well it handles a lack of a document and only having a single element. But that should demonstrate fairly well.

Upvotes: 1

Related Questions