Items covered below:
1.) Selecting a VARCHAR column as XML
2.) Selecting XML nodes containing a namespace
3.) Cross Apply XML column to produce multiple rows of results
TABLE example:
TestTable(
Id INT,
Data VARCHAR)
Data Column example:
<Props xmlns="http://tempuri.org/Test.xsd"><Type><Fun>a</Fun><Sup>1</Sup></Type><Type><Fun>b</Fun><Sup>2</Sup></Type></Props>
SQL example:
;WITH XMLNAMESPACES (N'http://tempuri.org/Test.xsd' AS NS)
SELECT
Id,
X.S.value('(NS:Fun)[1]', 'VARCHAR(8)') AS 'Fun',
X.S.value('(NS:Sup)[1]', 'VARCHAR(8)') AS 'Sup'
FROM TestTable
CROSS APPLY (SELECT CAST(Data AS XML)) AS [XML](Data)
CROSS APPLY [XML].Data.nodes('/NS:Props/NS:Type') AS X(S)
Wednesday, July 23, 2014
Tuesday, June 10, 2014
SQL query column with XML stored as VARCHAR
XML stored as VARCHAR can be queried by CASTing
--Example XML
--Query
SELECT a.Id, m.Message, a.TransmissionDtTm
FROM Message m
INNER JOIN MessageArchive a ON a.messageid=m.messageid
WHERE a.TransmissionError = 0
AND CAST(m.Properties AS xml).value('(/Props/Type)[1]', 'VARCHAR(128)') LIKE '%Email%'
Thursday, May 22, 2014
Use of Javascript to allow RadioButton grouping to work in Repeater
1.) JavaScript
<script language="javascript" type="text/javascript">
function RadioSelectionChanged(groupName, current) {
var group = document.getElementsByName(groupName);
for (i = 0; i < group.length; i++) {
//.NET uses a span tag to define the control "name", so the first control in the span control will be the radiobutton if(group[i].childNodes.length > 0 && group[i].childNodes[0].type == 'radio')
group[i].childNodes[0].checked = false;
} current.checked = true;
}</script>
2.) Control
<asp:Repeater ID="Rptr" runat="server">
<ItemTemplate>
<asp:RadioButton ID="Rb" runat="server" onclick="RadioSelectionChanged('GroupName', this);" name="GroupName" />
</ItemTemplate>
</asp:Repeater>
Wednesday, May 21, 2014
Convert class to DataRow
To convert a class instance to a DataRow
public DataRow ConvertClassToDataRow(ClassName theClass)
{
DataTable table = new DataTable();
DataRow row = table.NewRow();
PropertyInfo[] properties = theClass.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
DataColumn col = new DataColumn(property.Name, property.PropertyType);
table.Columns.Add(col); row[property.Name] = property.GetValue(theClass, null);
}
return row;
}
Thursday, April 24, 2014
Reanimate an animated gif when javascipt makes visible
To get an animated gif to work after some javascript adjusts the visibility, create a new instance
function Loading() {
var wait = document.getElementById('loadingGif');
if (wait != null) {
wait.style.display = "inline";
var newInstance = wait.cloneNode(true); //This will reanimate the gif
wait.parentNode.replaceChild(newInstance, wait);
}
}
Tuesday, April 22, 2014
Auto PostBack to refresh content
Set a timer to auto postback page using JavaScript
//Register Auto PostBack
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "PostBackFunction", GetPostBackFunction(), true);
//Function to detail script
private string GetPostBackFunction()
{ StringBuilder build = new StringBuilder();
build.AppendLine(string.Format("window.setTimeout(\"AutoPostBack()\", {0});", (1000 * secondsForAutoPostBack).ToString()));
build.AppendLine("function AutoPostBack()");
build.AppendLine("{javascript:__doPostBack('AspControl','');}");
return build.ToString();
}
Note: "AspControl" in the javascript postback is the ID of an asp.net control on the page this script is being registered on. If none exists, add one:
<asp:LinkButton runat="server" ID="AutoPostBackLb" Text=""></asp:LinkButton>
Wednesday, March 12, 2014
Use anchor to pop up window
Using an anchor to pop-up a window with JavaScriptAnchor tag:
<a href="javascript:popWindow('http://www.google.com');">Pop Window</a>
JavaScript:
<script type="text/javascript">
function popWindow(hrefTarget) {
// Pop up the window
window.open(hrefTarget, "_blank", "width=463,height=500,resizable=yes");
}</script>
Alternatively, returning a false will prevent the anchor from linking:
<a href='www.google.com' onclick='FunctionCall(); return false;'>Pop Window</a>
Subscribe to:
Posts (Atom)