Given the xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<lfSpec1ForLoc>
<a href="/spec.aspx?id=20" title="AI">AI</a>
</lfSpec1ForLoc>
<lfSpec2ForLoc />
<lfSpec3ForLoc />
<lfSpec4ForLoc />
<lfSpec5ForLoc />
<lfSpec6ForLoc />
<lfSpec7ForLoc />
<lfSpec8ForLoc />
<lfSpec9ForLoc />
<lfSpec10ForLoc />
</root>
My URL is: www.mypage.com/off.aspx?id=12
I have the following Stored Procedure:
ALTER PROCEDURE [dbo].[MySP]
(
@LstrID varchar(200), -- 12 {from the query string in the above URL}
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT
CAST ([con] AS XML).query('/root/lfSpec1ForLoc/a') AS [Spec1]
, CAST ([con] AS XML).query('/root/lfSpec2ForLoc/a') AS [Spec2]
, CAST ([con] AS XML).query('/root/lfSpec3ForLoc/a') AS [Spec3]
, CAST ([con] AS XML).query('/root/lfSpec4ForLoc/a') AS [Spec4]
, CAST ([con] AS XML).query('/root/lfSpec5ForLoc/a') AS [Spec5]
, CAST ([con] AS XML).query('/root/lfSpec6ForLoc/a') AS [Spec6]
, CAST ([con] AS XML).query('/root/lfSpec7ForLoc/a') AS [Spec7]
, CAST ([con] AS XML).query('/root/lfSpec8ForLoc/a') AS [Spec8]
, CAST ([con] AS XML).query('/root/lfSpec9ForLoc/a') AS [Spec9]
, CAST ([con] AS XML).query('/root/lfSpec10ForLoc/a') AS [Spec10]
FROM
[myDB1].[dbo].[con]
WHERE
[folder_id] = 106
AND
[content_id] = '%' + Lstr + '%'
END
The result from the SP is:
Spec1 Spec2 Spec3 Spec4 Spec5 Spec6 Spec7 Spec8 Spec9 Spec10
<a href="/spe.aspx?id=1" title="AI">AI</a> <a href="/spe.aspx?id=5" title="QW">QW</a>
I would like to populate the below dropdownlist:
<ASP:DropDownList runat="server" ID="ddl1"></ASP:DropDownList>
So the HTML output is as follow:
<SELECT>
<option value="/spe.aspx?id=1">AI</option>
<option value="/spe.aspx?id=5">QW</option>
</SELECT>
What is the best way to achieve the populating the dropdownlist after executing the SP?
I would change your PROC
to make better usage of Xquery and the xml parsing capabilities of Sql Server.
What you can do from your PROC is project a table of (URL, Title)
pairs from your the xml Records in your table with a query as simple as:
SELECT Nodes.node.value('(a/@href)[1]', 'varchar(100)') AS URL,
Nodes.node.value('(a/@title)[1]', 'varchar(100)') AS Title
FROM
(
SELECT CAST(con.con AS XML) AS con,
folder_id,
content_id
FROM con
) x
CROSS APPLY x.Con.nodes('/root/*[a]') as Nodes(node)
WHERE
[folder_id] = 106
AND
[content_id] LIKE '%' + @LstrID + '%';
The /root/*[a]
will navigate all lfSpecxForLoc
nodes (irrespective of name), and filter just those with an a
child element, thus avoiding the need to filter out nulls. Also, by projecting the elements as individual rows, you won't need to scrape out 10 explicit columns - there will be as many rows as there are data.
Here's a SqlFiddle of the resulting table projected by the above query.
From here, it is a simple task to pull the resultset into an ADO data reader or bind this to your drop down list (eg set the .DataSource
to the data reader, and call .DataBind()
on the DropDownList
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.