tag:blogger.com,1999:blog-66439325013856661722024-03-13T12:03:28.227-07:00Keep it simple...Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.comBlogger62125tag:blogger.com,1999:blog-6643932501385666172.post-140370231501968392019-03-18T13:14:00.004-07:002019-03-19T12:00:05.431-07:00Get dictionary object from list<br />
<div class="MsoNormal">
When you get dictionary object from a list in C#, make sure
that you select distinct values for the dictionary key. Otherwise the compiler will
throw run time error if there are duplicates.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
lst<b>.GroupBy(x=>x.id)</b>.Select(x=>x.First()).ToDictionary(r => r.id, k => k.enabled);<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Here is an example of how to select distinct keys from
list and to return them in dictionary object.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<iframe frameborder="0" height="475" src="https://dotnetfiddle.net/Widget/4cdZHc" width="100%"></iframe>Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-39647696090453999542019-03-12T13:39:00.001-07:002019-03-12T13:39:13.138-07:00Lazy initialization in C#Lazy<T> can be used in C# to delay the initialization of an object. It can be used in singleton architecture as given in the below example.<br />
<br />
public static Lazy<MyClass> MyClassInstance = new Lazy<MyClass>(()=> new MyClass());<br />
<br />
<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/rFaelJ" frameborder="0"></iframe>
<br />Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-52673757508739949442018-12-13T11:27:00.002-08:002018-12-17T13:33:54.129-08:00Using table type and batch update statements in MS SQL<br />
<div class="MsoNormal">
Given below is an example on how to use table types. Table types can be used to pass data array data from code to SQL stored procedure instead of passing the data in CSV or XML format. The example also shows how to batch SQL update statements.<o:p></o:p></div>
<div>
<br /></div>
<br />
<br />
<div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">CREATE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">TYPE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> [dbo]</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">.</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">[BIGIntArrayTemp] </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">AS</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">TABLE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 1;"> </span>[Val] [bigint] </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">NULL</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">GO</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">DECLARE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tableType [dbo]</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">.</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">[BIGIntArrayTemp]<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">1</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">2</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">4</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">8</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">7</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">4</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">6</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 1;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">DECLARE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @records </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">TABLE</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">records_id </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">INT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">IDENTITY</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">1</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">,</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">1</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">),</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">BIGINT</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">DECLARE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @toprecords </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">TABLE</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">BIGINT</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">DECLARE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tmp </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">TABLE</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">BIGINT</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">,</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">flag </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">bit</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">1</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">2</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">4</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">8</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">7</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">4</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">6</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">insert</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">into</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">values<span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">10</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 1;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">INSERT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">INTO</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @records</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">SELECT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span>SomeId<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">FROM</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span>@tmp t<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">INNER</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">JOIN</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>@tableType ids </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">ON</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> ids</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">.</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">Val </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">=</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>t</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">.</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">DECLARE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @count<span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">INT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">DECLARE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @batch_size </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">INT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">SET</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @batch_size </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">=</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> 100 </span><span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">--set batch size</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">INSERT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">INTO</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @toprecords</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">SELECT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">top </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">@batch_size</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> SomeId </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">FROM</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @records </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">ORDER</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">BY</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> records_id </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">desc</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">SELECT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @count </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">=</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: magenta; font-family: "consolas"; font-size: 9.5pt;">COUNT</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">1</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">FROM</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @toprecords<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 1;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">WHILE </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">@count </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">></span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> 0</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">BEGIN</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">BEGIN</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">TRY</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">BEGIN</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">TRANSACTION</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">--Begin transaction for each
batch</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: magenta; font-family: "consolas"; font-size: 9.5pt;">UPDATE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">
g<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">SET</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> g</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">.</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">flag</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">=</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">1<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">FROM</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tmp g<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 3;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">INNER</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">JOIN</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @toprecords t<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">ON</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> t</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">.</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">=</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> g</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">.</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 3;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">--
clean toprecords table variable</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">DELETE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">FROM</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @toprecords<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">--
remove the top records</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">DELETE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">FROM</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> dd<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">FROM
</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 3;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">SELECT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">top </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">@batch_size</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">*</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">FROM</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @records </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">ORDER</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">BY</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> records_id </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">desc</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> dd<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">--
select next top records</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">INSERT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">INTO</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @toprecords</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 3;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">SELECT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">top </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">@batch_size</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> SomeId </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">FROM</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @records </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">ORDER</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">BY</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> records_id </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">desc</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">SELECT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @count </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">=</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: magenta; font-family: "consolas"; font-size: 9.5pt;">count</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">1</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">)</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">FROM</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @toprecords<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 2;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">COMMIT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">TRANSACTION</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">-- commit transaction for
each batch</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">END</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">TRY</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">BEGIN</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">CATCH</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 3;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">ROLLBACK</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">
</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">TRANSACTION</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">-- rollback if any error
occurs</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">END</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">CATCH</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">END</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">SELECT</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">*</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">FROM</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> @tmp</span><o:p></o:p></div>
</div>
<div>
<br /></div>
<div>
------------------------------</div>
<div>
Another way to implement it without try...catch and transaction<br />
<br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">DECLARE</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tableType [dbo]</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">.</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">[BIGIntArrayTemp]<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">1</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">2</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">4</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">8</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">7</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">4</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tableType</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">val</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">6</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">DECLARE</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @records </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">TABLE</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">records_id </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">INT</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">IDENTITY</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">1</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">,</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">1</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">),</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">BIGINT</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">DECLARE</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @updatelist </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">TABLE</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">BIGINT</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">DECLARE</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tmp </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">TABLE</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">BIGINT</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">,</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">flag </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">bit</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">1</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">2</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">4</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">8</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">7</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">4</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">6</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">insert</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tmp</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">values </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">10</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">DECLARE</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @count </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">INT</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">DECLARE</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @batch_size </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">INT</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">,</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">@RecordCount </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">INT</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">SET</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @batch_size </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">=</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> 2 </span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">--set batch size</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">INSERT</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">INTO</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @records</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">SELECT</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> SomeId<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">FROM</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tmp t<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">INNER</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">JOIN</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tableType ids </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">ON</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> ids</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">.</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">Val </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">=</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> t</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">.</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">Select</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @RecordCount </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">=</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: magenta; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">@@RowCount</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">--initialize RecordCount</span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">WHILE </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">@RecordCount </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">></span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> 0</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">BEGIN</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">Delete</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">top</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">@batch_size</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">)</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> r <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">Output</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> deleted</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">.</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">Into</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @updatelist<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">From</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @records r<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">Select</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @RecordCount </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">=</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: magenta; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">@@RowCount</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">--update record count
variable</span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">PRINT
</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">'Record Count --'</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">+</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">
</span><span style="background: white; color: magenta; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">convert</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">varchar</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">,</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">@RecordCount</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">))</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: magenta; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">UPDATE</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> g<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">SET</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">
g</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">.</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">flag</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">=</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">1<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">FROM</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">
@tmp g<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">INNER</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">
</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">JOIN</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @updatelist t<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">ON</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">
t</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">.</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">=</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> g</span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">.</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">SomeId<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">-- clean toprecords table variable</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">Delete</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">from</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @updatelist<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">Waitfor</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">Delay</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">'00:00:00.5'</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">END</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">SELECT</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">*</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">FROM</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> @tmp<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<br /></div>
--------------------------------<br />
<br /></div>
<div>
Table type data can be used in stored procedure to pass array data.</div>
<div>
<br /></div>
<div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">CREATE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">PROCEDURE</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> [dbo]</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">.</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">[xyz]<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 1;"> </span>@ids dbo</span><span style="background: white; color: grey; font-family: "consolas"; font-size: 9.5pt;">.</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">[BIGIntArrayTemp] </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">READONLY</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">AS</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">BEGIN</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-tab-count: 1;"> </span></span><span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">--
Proc body</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">END</span><o:p></o:p></div>
</div>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-87986586747860928742018-12-07T12:39:00.001-08:002018-12-07T12:39:21.930-08:00C# Delegate Example<span style="background-color: white; font-family: "segoe ui" , "segoeui" , "segoe wp" , "helvetica neue" , "helvetica" , "tahoma" , "arial" , sans-serif; font-size: 16px;">A delegate </span><span style="background-color: white; font-family: "segoe ui" , "segoeui" , "segoe wp" , "helvetica neue" , "helvetica" , "tahoma" , "arial" , sans-serif; font-size: 16px;">is a type that represents references to methods with a particular parameter list and return type. It </span><span style="background-color: white; font-family: "verdana" , "geneva" , "tahoma" , "arial" , "helvetica" , sans-serif; font-size: 15px; text-align: justify;">is a reference type variable that holds the reference to a method and the reference can be changed at runtime.</span><br />
<span style="background-color: white; font-family: "verdana" , "geneva" , "tahoma" , "arial" , "helvetica" , sans-serif; font-size: 15px; text-align: justify;"><br /></span>
<span style="background-color: white; font-family: "verdana" , "geneva" , "tahoma" , "arial" , "helvetica" , sans-serif; font-size: 15px; text-align: justify;">Given below is an example of implementing logging using delegate. The class is not aware of the implementation of the logging. The calling application/object can decide to implement the logging using different mechanisms - log to disk or database or make api call etc.</span><br />
<span style="background-color: white; font-family: "verdana" , "geneva" , "tahoma" , "arial" , "helvetica" , sans-serif; font-size: 15px; text-align: justify;"><br /></span>
<span style="background-color: white; font-family: "verdana" , "geneva" , "tahoma" , "arial" , "helvetica" , sans-serif; font-size: 15px; text-align: justify;"><iframe frameborder="0" height="475" src="https://dotnetfiddle.net/Widget/PI8zjk" width="100%"></iframe></span><br />
<span style="background-color: white; font-family: "verdana" , "geneva" , "tahoma" , "arial" , "helvetica" , sans-serif; font-size: 15px; text-align: justify;"><br /></span>
<span style="background-color: white; font-family: "verdana" , "geneva" , "tahoma" , "arial" , "helvetica" , sans-serif; font-size: 15px; text-align: justify;"><br /></span>Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-41745687547554656712018-12-06T12:02:00.003-08:002018-12-06T12:02:56.540-08:00Dependency Injection Example<span style="background-color: white; color: #242729; font-family: "arial" , "helvetica neue" , "helvetica" , sans-serif; font-size: 15px;">Dependency injection is providing the objects that an object needs (its dependencies) instead of having it construct them itself. It's a very useful technique for testing, since it allows dependencies to be mocked or stubbed out.</span><br />
<span style="background-color: white; color: #242729; font-family: "arial" , "helvetica neue" , "helvetica" , sans-serif; font-size: 15px;"><br /></span>
<span style="background-color: white; color: #242729; font-family: "arial" , "helvetica neue" , "helvetica" , sans-serif; font-size: 15px;">Here is an example of dependency injection through constructor.</span><br />
<span style="background-color: white; color: #242729; font-family: "arial" , "helvetica neue" , "helvetica" , sans-serif; font-size: 15px;"><br /></span>
<span style="background-color: white; color: #242729; font-family: "arial" , "helvetica neue" , "helvetica" , sans-serif; font-size: 15px;"><iframe frameborder="0" height="475" src="https://dotnetfiddle.net/Widget/FFI82b" width="100%"></iframe></span>Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-23228657263681265712018-11-30T08:33:00.001-08:002018-11-30T08:36:58.618-08:00How to post data to MVC method using javascript fetch<div class="MsoNormal">
There are better ways to make API calls from javascript to
C# endpoints – like web api or using http handlers. But sometimes you may have
a requirement to call a MVC endpoint using javascript.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Here is an example to post data to MVC endpoint and to get the
return data as JSON. Make sure that you pass the correct content type<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> fetch(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'@Url.RouteUrl(new{ action="GetResults",
controller="Home"})'</span><span style="font-family: "consolas"; font-size: 9.5pt;">, {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> method: </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'POST'</span><span style="font-family: "consolas"; font-size: 9.5pt;">,<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> headers: {<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span style="font-family: "consolas"; font-size: 9.5pt;"> </span></b><b><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'Accept'</span></b><b><span style="font-family: "consolas"; font-size: 9.5pt;">: </span></b><b><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'application/json'</span></b><b><span style="font-family: "consolas"; font-size: 9.5pt;">,<o:p></o:p></span></b></div>
<div class="MsoNormal">
<b><span style="font-family: "consolas"; font-size: 9.5pt;"> </span></b><b><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'Content-Type'</span></b><b><span style="font-family: "consolas"; font-size: 9.5pt;">: </span></b><b><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'application/x-www-form-urlencoded;charset=UTF-8'</span></b><b><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> },<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> credentials: </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'include'</span><span style="font-family: "consolas"; font-size: 9.5pt;">,<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> body: </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"token=pass-some-data"</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> })<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> .then(blob =>
blob.json())<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> .then(data => {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> document.getElementById(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"txtResponse"</span><span style="font-family: "consolas"; font-size: 9.5pt;">).value =
JSON.stringify(data);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> });<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
Here is the complete working example; check the view and controller to see the C# and javascipt code.<o:p></o:p></div>
<br />
<br />
<iframe frameborder="0" height="655" src="https://dotnetfiddle.net/Widget/PXdIiL" width="100%"></iframe>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-51916121781827484942018-11-30T07:49:00.003-08:002018-12-04T13:16:36.140-08:00Thread safety when using static properties/fields inside functions<br />
<div class="MsoNormal">
When using static properties inside functions you have to
make sure that the properties are modified in a thread safe manner. If not this
could cause exceptions or unexpected behavior in the application.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
In the below example changing the static field will cause
exception/incorrect behavior if accessed by multiple threads/processes.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MyClass</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> List<</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">> lst
= </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> List<</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//DoWork with no lock will cause the application to fail</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> DoWork()<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">try</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>Console.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Work started"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>Thread.Sleep(100);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>lst.Add(100);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>Thread.Sleep(100);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>lst.Add(200);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>Thread.Sleep(100);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>lst.Add(300);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>Thread.Sleep(100);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>lst.Add(400);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>Thread.Sleep(100);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>Console.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Work Complete"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">catch</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (Exception ex)<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>Console.WriteLine(ex);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> lst.Count;<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The issue can be resolved by adding “lock” before changing
static field<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MyClass</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">static</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> List<</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">> lst
= </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> List<</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//DoWork with no lock will cause the application to fail</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> DoWork()<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">try</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">lock</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (lst)<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{ </span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//setting lock will ensure thread safety</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>Console.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Work started"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>Thread.Sleep(300);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>lst.Add(100);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>Thread.Sleep(300);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>lst.Add(200);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>Thread.Sleep(100);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>lst.Add(300);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>Thread.Sleep(600);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>lst.Add(400);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>Thread.Sleep(100);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>Console.WriteLine(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Work Complete"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">catch</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> (Exception ex)<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>Console.WriteLine(ex);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> lst.Count;<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Here is the complete code. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<iframe frameborder="0" height="475" src="https://dotnetfiddle.net/Widget/cXcX3b" width="100%"></iframe><o:p></o:p></div>
<br />
C#’s lock statement is in fact a syntactic shortcut for a call to the methods Monitor.Enter and Monitor.Exit, with a try/finally block. Here is the same example using Monitor<br />
<br />
<iframe frameborder="0" height="475" src="https://dotnetfiddle.net/Widget/mn5sPJ" width="100%"></iframe><br />
<br />
A Mutex is like a C# lock, but it can work across multiple processes. In other words, Mutex can be computer-wide as well as application-wide.<br />
<br />
The lock construct is faster and more convenient than Mutex. Mutex has a niche in that its lock can span applications in different processes on the computer.<br />
<br />
<iframe frameborder="0" height="475" src="https://dotnetfiddle.net/Widget/tY4z23" width="100%"></iframe>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-31282817164334376842018-02-28T08:33:00.001-08:002018-02-28T08:33:22.432-08:00Javascript Generator FunctionGenerators are functions which can be exited and later re-entered. The function* declaration defines a generator function, which returns a Generator object.<br />
<br />
Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator's next() method is called, the generator function's body is executed until the first yield expression, which specifies the value to be returned from the iterator or, with yield*, delegates to another generator function. The next() method returns an object with a value property containing the yielded value and a done property which indicates whether the generator has yielded its last value as a boolean. Calling the next() method with an argument will resume the generator function execution, replacing the yield expression where execution was paused with the argument from next().<br />
<br />
Here is an example of calling generator function.<br />
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">function*</span><span style="font-family: Consolas; font-size: 9.5pt;"> generator1() {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">yield</span><span style="font-family: Consolas; font-size: 9.5pt;"> 1;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">yield</span><span style="font-family: Consolas; font-size: 9.5pt;"> 2;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">yield</span><span style="font-family: Consolas; font-size: 9.5pt;"> 3;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">var</span><span style="font-family: Consolas; font-size: 9.5pt;"> gen1 = generator1();<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">let</span><span style="font-family: Consolas; font-size: 9.5pt;"> resp = gen1.next();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> console.log(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">'Value-'</span><span style="font-family: Consolas; font-size: 9.5pt;"> + resp.value + </span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">": Done-"</span><span style="font-family: Consolas; font-size: 9.5pt;"> + resp.done);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">//Response - Value-1: Done-false<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> resp = gen1.next();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> console.log(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">'Value-'</span><span style="font-family: Consolas; font-size: 9.5pt;"> + resp.value + </span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">": Done-"</span><span style="font-family: Consolas; font-size: 9.5pt;"> + resp.done);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">//Response - Value-2: Done-false<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> resp = gen1.next();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> console.log(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">'Value-'</span><span style="font-family: Consolas; font-size: 9.5pt;"> + resp.value + </span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">": Done-"</span><span style="font-family: Consolas; font-size: 9.5pt;"> + resp.done);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">//Response - Value-3 Done-false<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> resp = gen1.next();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> console.log(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">'Value-'</span><span style="font-family: Consolas; font-size: 9.5pt;"> + resp.value + </span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">": Done-"</span><span style="font-family: Consolas; font-size: 9.5pt;"> + resp.done);<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">//Response - Value-undefined: Done-true<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><br /></span></div>
<br />
Here is an example of calling generator function inside generator function.<br />
<br />
<br />
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">function*</span><span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> someFunc(param1) {<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">yield</span><span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> </span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">"inside
someFunc - param1-"</span><span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> + param1;<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">function*</span><span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> generator2() {<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">yield*</span><span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> someFunc(1);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">yield*</span><span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> someFunc(2);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;">var</span><span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"> gen2 = generator2();<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;">
</span>console.log(gen2.next().value);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas;"><span style="mso-spacerun: yes;">
</span>console.log(gen2.next().value);<o:p></o:p></span></div>
<br />Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-84415998376653080942018-02-28T07:38:00.002-08:002018-02-28T08:21:51.956-08:00Here is a simple example on how to use async, await for fetch javascript fetch<br />
Given below is a simple example on how to use async, await when using javascript fetch.<br />
<br />
<span style="font-family: consolas; font-size: 9.5pt;">--------------------</span><span style="font-family: consolas; font-size: 12.6667px;">--------------------</span><br />
<div class="MsoNormal">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//create fetch<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">let</span><span style="font-family: "consolas"; font-size: 9.5pt;"> reqPromise = fetch(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'https://jsonplaceholder.typicode.com/posts/1'</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">async</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">function</span><span style="font-family: "consolas"; font-size: 9.5pt;"> getData(){<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//await for 'fetch' response and then await to convert response to
'json'<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">let</span><span style="font-family: "consolas"; font-size: 9.5pt;"> response = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">await</span><span style="font-family: "consolas"; font-size: 9.5pt;">(</span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">await</span><span style="font-family: "consolas"; font-size: 9.5pt;"> reqPromise).json();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> console.log(response);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//call async function<o:p></o:p></span></div>
<span style="font-family: consolas; font-size: 9.5pt;">getData();</span><br />
<span style="font-family: consolas; font-size: 9.5pt;"><br /></span>
<span style="font-family: consolas; font-size: 9.5pt;">--------------------</span><span style="font-family: consolas; font-size: 12.6667px;">--------------------</span><br />
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;">You can see it working here - </span><span style="font-family: "consolas";"><span style="font-size: 12.6667px;"><a href="https://codepen.io/deebujacob/pen/BYveQm">https://codepen.io/deebujacob/pen/BYveQm</a></span></span></div>
<br />
<br />Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-72032253618499276472018-02-19T12:21:00.001-08:002018-02-28T08:23:43.474-08:00Get string content posted to a web apiIf you have to post string content to a web api you can use the below example. [FromBody] attribute will work only for formatted content like json or xml.<br />
<br />
<br />
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>[Route(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"GetData"</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">)]<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>[HttpPost]<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">virtual</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">async</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;">
System.Threading.Tasks.Task<HttpResponseMessage> GetData()<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> someData = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">await</span></b><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><b>
Request.Content.ReadAsStringAsync();</b><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;">
</span>HttpResponseMessage response = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> HttpResponseMessage(HttpStatusCode.OK);<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="color: black; font-family: "consolas"; font-size: 9.5pt;"> response;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}</span><o:p></o:p></div>
<br />
<br />
The syntax is similar to the JS async await - <a href="http://deebujacob.blogspot.com/2018/02/here-is-simple-example-on-how-to-use.html">http://deebujacob.blogspot.com/2018/02/here-is-simple-example-on-how-to-use.html</a>Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-53848006950603414732018-02-13T14:04:00.000-08:002018-02-20T11:37:05.023-08:00.NET MVC - Read Json Object From POST data<br />
<div class="MsoNormal">
If you want to get JSON data posted to MVC endpoint
without creating corresponding C# object, you can use the following method.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">Web API endpoint<o:p></o:p></b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none; text-indent: .5in;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> [HttpPost]<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> [Route(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"GetData"</span><span style="font-family: "consolas"; font-size: 9.5pt;">)]<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> String GetData()<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Get the request input stream</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> Stream req =
Request.InputStream;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> req.Seek(0,
System.IO.SeekOrigin.Begin);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;"> jsonString = </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="font-family: "consolas"; font-size: 9.5pt;"> StreamReader(req).ReadToEnd();<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Convert JSON String to JObject</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> JObject
jsonObject = JObject.Parse(jsonString);<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "consolas"; font-size: 9.5pt;">//Get posted data</span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="font-family: "consolas"; font-size: 9.5pt;"> name= jsonObject[</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"name"</span><span style="font-family: "consolas"; font-size: 9.5pt;">].ToString();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> <span style="font-size: 12.6667px;">name</span>;<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }</span><o:p></o:p></div>
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b style="mso-bidi-font-weight: normal;">JavaScript call<o:p></o:p></b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<div class="MsoNormal">
<div class="MsoNormal">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;"> let</span><span style="font-family: "consolas"; font-size: 9.5pt;"> jsonData
= </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'{"name":"myname",age:10}'</span><span style="font-family: "consolas"; font-size: 9.5pt;">;</span><o:p></o:p></div>
</div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">let</span><span style="font-family: "consolas"; font-size: 9.5pt;"> reqPromise = fetch(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'/GetData'</span><span style="font-family: "consolas"; font-size: 9.5pt;">,<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> method: </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"POST"</span><span style="font-family: "consolas"; font-size: 9.5pt;">,<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> body:
jsonData,<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> headers:
{<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'Accept'</span><span style="font-family: "consolas"; font-size: 9.5pt;">: </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'application/json,
text/plain, */*'</span><span style="font-family: "consolas"; font-size: 9.5pt;">,<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'Content-Type'</span><span style="font-family: "consolas"; font-size: 9.5pt;">: </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'application/json'</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> });<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> reqPromise.then(<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> (response)
=> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> response.text();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> ).then((jsonRespnse)
=> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;">
console.log(jsonRespnse);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> );</span><o:p></o:p></div>
</div>
<div class="MsoNormal">
<br />
<br />
The other option is to create corresponding C# object and accept it as a parameter in the endpoint.<br />
<br />
1) Create object<br />
<br />
<div class="MsoNormal">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;"> public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MyData</span><span style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> String Name { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Age { </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span style="font-family: "consolas"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;">
}</span><o:p></o:p></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas";"><span style="font-size: 9.5pt;">2) Accept the object as a </span><span style="font-size: 12.6667px;">parameter</span><span style="font-size: 9.5pt;"> using [</span></span><span style="font-family: "consolas"; font-size: 12.6667px;">FromBody] attribute as given below</span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 12.6667px;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> [HttpPost]<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> [Route(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"GetData"</span><span style="font-family: "consolas"; font-size: 9.5pt;">)]<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> String
GetData([System.Web.Http.FromBody] MyData myData)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> myData.Name;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<o:p><br /></o:p></div>
</div>
<br />Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-43809927112125719012018-02-02T13:47:00.000-08:002018-12-06T12:04:37.584-08:00Youtube video thumbnail imagesYou can get the thumbnail images of youtube video by using the below format.<br />
<br />
https://img.youtube.com/vi/<video-id>/0.jpg<br />
https://img.youtube.com/vi/<video-id>/1.jpg<br />
https://img.youtube.com/vi/<video-id>/2.jpg<br />
https://img.youtube.com/vi/<video-id>/hqdefault.jpg<br />
<br />
You can see a live example here - <a href="https://codepen.io/deebujacob/full/BYjxpm">https://codepen.io/deebujacob/full/BYjxpm</a><br />
<iframe frameborder="0" height="475" src="https://codepen.io/deebujacob/full/BYjxpm" width="100%"></iframe>
<br />Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-90289564404210072692018-02-02T13:33:00.000-08:002018-02-14T08:20:58.716-08:00Data URLs - Include Data In-line In Web Pages <div class="MsoNormal">
Data URLs, URLs prefixed with the data: scheme, allow content
creators to embed small files inline in documents.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can pass html directly to the url as given below.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<iframe
src="data:text/html,<html><body><div>test</div></body></html>"
height="100%" width="100%"></iframe><o:p></o:p></div>
<div class="MsoNormal">
<iframe src="data:text/html,data:text/html,<script>alert('hi');</script>"
height="100%" width="100%"></iframe><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Or you can set base64 data in URL <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
<iframe src="data:application/pdf;base64,BASE64_DATA"
height="100%" width="100%"></iframe><o:p></o:p></div>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-24882364751405685972017-01-25T11:35:00.003-08:002017-01-25T12:21:15.410-08:00How to execute JavaScipt code inserted as innerHTML/text in DOM<div class="MsoNormal">
If you tried to load scripts into a page using innerHTML on HTML
element like div it will not be executed. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The alert statement in below code will not be executed since
it is set as inner HTML.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-family: "consolas"; font-size: 9.5pt;">document.getElementById(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">'divTemp'</span><span style="font-family: "consolas"; font-size: 9.5pt;">).innerHTML = </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"<scr"</span><span style="font-family: "consolas"; font-size: 9.5pt;"> + </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"ipt>alert('hi')</scr"</span><span style="font-family: "consolas"; font-size: 9.5pt;"> + </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"ipt>"</span><span style="font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can use eval() but there is an easier and better way to
do this. That is to load the script element into DOM.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Here is an example - <o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span style="font-family: "consolas"; font-size: 9.5pt;"> script1 = document.createElement(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"script"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"> script1.innerHTML = </span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"alert('hi');"</span><span style="font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<span style="font-family: "consolas"; font-size: 9.5pt;"> document.body</span><span style="font-family: "consolas"; font-size: 9.5pt;">.appendChild(script1);</span><br />
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
</div>
<div class="MsoNormal">
In this way you can execute javascript statements set as innerHTML or text. This will help you to execute scripts that are loaded asynchronously using APIs.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Live example in codepen- </div>
<div class="MsoNormal">
<a href="http://codepen.io/deebujacob/pen/egEoyq">http://codepen.io/deebujacob/pen/egEoyq</a><br />
<br /></div>
<div class="MsoNormal">
<br /></div>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-76087817296978152962017-01-13T08:12:00.002-08:002017-01-23T12:39:25.249-08:00How to scale html elements and fonts based on the containerWhen we generate html we can use '%' to scale the containers based on parent element. But html elements set with pixel value will not scale correctly if parent container size is changed.<br />
<br />
Here is an example to scale html elements correctly. Keep in mind that this is not a solution for setting layouts for different devices like desktop, mobile and tablet. The example below can be used when you want the layout to look exactly the same and scale the content size and position based on parent element's size.<br />
<br />
We can achieve this by setting the transform property<br />
transform: scale(scaleWidthValue,scaleHeightValue);<br />
<br />
To scale twice the size -<br />
-ms-transform: scale(2,2); /* IE 9 */<br />
transform: scale(2,2); /* Safari */<br />
transform: scale(2,2); /* Standard syntax */<br />
<br />
Here is the codepen link to live example -<a href="http://codepen.io/deebujacob/pen/OWXjjp" target="_blank">http://codepen.io/deebujacob/pen/OWXjjp</a><br />
<br />
/********************* Here is the auto scaling html logic ***************/<br />
<html><br />
<style><br />
#containerElement{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>width:200px;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>height:120px;<br />
}<br />
.outerWrapperStyle{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>background-color:yellow;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>width:400px;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>height:225px;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>padding:10px;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>transform-origin:top left;<br />
}<br />
.innerWrapperStyle{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>background-color:red;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>width:200px;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>height:125px;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>padding:10px;<br />
}<br />
.contentStyle{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>font-family:arial;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>font-size:18px;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>font-color:#333;<br />
}<br />
.transform{/*not used directly here*/<br />
transform: scale(1.5,1.5); /* Standard syntax */<br />
}<br />
</style><br />
<div id="containerElement" ><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><div id="contentElement" class="outerWrapperStyle transform"><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><div class="contentStyle"><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Outer wrapper content.<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span></div><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><div class="innerWrapperStyle"><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><div class="contentStyle"><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Inner Wrapper Content<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span></div><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span></div><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span></div><br />
</div><br />
<script><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>var containerElement = document.getElementById("containerElement");/*get container element*/<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>var contentElement = document.getElementById("contentElement");/*get content element*/<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>var scaleWidth = containerElement.clientWidth/contentElement.clientWidth; /*find out how much to scale for width*/<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>var scaleHeight = containerElement.clientHeight/contentElement.clientHeight; /*find out how much to scale for height*/<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>contentElement.style.transform = "scale("+scaleWidth+","+scaleHeight+")"; /*set the scale on content Element*/<br />
</script><br />
<br />
</html>Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-27263428200647374502016-09-14T12:37:00.001-07:002016-09-14T12:47:50.253-07:00How to POST JSON object to ASP.NET httphandler API using XmlHttpRequest and Javascript<div class="MsoNormal">
This example show how to post a JSON object to asp.net http
handler using xmlhttprequest in JavaScript.<o:p></o:p><br />
<br />
Passing objects this way will help to maintain the same object model in UI , business object and database. Similar to MVC framework - but here you are building your own framework and have more control over the functionalities and performance.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
JS <o:p></o:p></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;"><</span><span style="background: white; color: maroon; font-family: "courier new"; font-size: 9.5pt;">script</span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">></span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">function</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
postData() {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">var</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> xhr = </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> XMLHttpRequest();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "courier new"; font-size: 9.5pt;">/*set the asp.net end point*/</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">var</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> url = </span><span style="background: white; color: #a31515; font-family: "courier new"; font-size: 9.5pt;">"departmentAPI?action=add"</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "courier new"; font-size: 9.5pt;">/*set method as post*/</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
xhr.open(</span><span style="background: white; color: #a31515; font-family: "courier new"; font-size: 9.5pt;">"POST"</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">, url, </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">true</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
xhr.setRequestHeader(</span><span style="background: white; color: #a31515; font-family: "courier new"; font-size: 9.5pt;">"Content-type"</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">, </span><span style="background: white; color: #a31515; font-family: "courier new"; font-size: 9.5pt;">"application/json"</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
xhr.onreadystatechange = </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">function</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> () {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">if</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> (xhr.readyState == 4
&& xhr.status == 200) {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">var</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> json =
JSON.parse(xhr.responseText);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
console.log(json)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "courier new"; font-size: 9.5pt;">/*create object*/</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">var</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> department = {};<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
department.department_name = </span><span style="background: white; color: #a31515; font-family: "courier new"; font-size: 9.5pt;">"Finance"</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">var</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> data =
JSON.stringify(department);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "courier new"; font-size: 9.5pt;">/*post data to server*/</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
xhr.send(data);<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "courier new"; font-size: 9.5pt;">/*call the function*/</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> <span style="font-size: 12.6667px;">postData</span>();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;"></</span><span style="background: white; color: maroon; font-family: "courier new"; font-size: 9.5pt;">script</span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">></span><span style="color: blue; font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
C#/.net <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">class</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "courier new"; font-size: 9.5pt;">DepartmentAPI</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> : </span><span style="background: white; color: #2b91af; font-family: "courier new"; font-size: 9.5pt;">IHttpHandler</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none; text-indent: .5in;">
<span style="background: white; color: #2b91af; font-family: "courier new"; font-size: 9.5pt;">JavaScriptSerializer</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> serializer = </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "courier new"; font-size: 9.5pt;">JavaScriptSerializer</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">void</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> ProcessRequest(</span><span style="background: white; color: #2b91af; font-family: "courier new"; font-size: 9.5pt;">HttpContext</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> context)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "courier new"; font-size: 9.5pt;">/*read data from server side*/</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">string</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> jsonString = </span><span style="background: white; color: #2b91af; font-family: "courier new"; font-size: 9.5pt;">String</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">.Empty;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
context.Request.InputStream.Position = 0;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">using</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> (</span><span style="background: white; color: #2b91af; font-family: "courier new"; font-size: 9.5pt;">StreamReader</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
inputStream = </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">new</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "courier new"; font-size: 9.5pt;">StreamReader</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">(context.Request.InputStream))<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
jsonString = inputStream.ReadToEnd();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "courier new"; font-size: 9.5pt;">DepartmentBO</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> department
= serializer.Deserialize<</span><span style="background: white; color: #2b91af; font-family: "courier new"; font-size: 9.5pt;">DepartmentBO</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">>(jsonString);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> }</span><span style="font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">public</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">bool</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> IsReusable<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">get</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> { </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">return</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">true</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">}</span><o:p></o:p></div>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com1tag:blogger.com,1999:blog-6643932501385666172.post-78490979041886947142016-05-25T13:13:00.004-07:002016-06-06T08:05:58.092-07:00Set content in an iframe or load scripts in it asynchronously<div class="MsoNormal">
Sometimes you may have to load the scripts or content inside
and iframe. This will be helpful especially if you are loading an ad or video
on a web page and do not want to slow down the whole web page. Given below is
an example on how to load content inside an iframe asynchronously.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;"> <</span><span style="background: white; color: maroon; font-family: "courier new"; font-size: 9.5pt;">iframe</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: red; font-family: "courier new"; font-size: 9.5pt;">id</span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">="myiframe"</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: red; font-family: "courier new"; font-size: 9.5pt;">height</span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">="300"</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: red; font-family: "courier new"; font-size: 9.5pt;">width</span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">="700"></</span><span style="background: white; color: maroon; font-family: "courier new"; font-size: 9.5pt;">iframe</span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">></span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;"><</span><span style="background: white; color: maroon; font-family: "courier new"; font-size: 9.5pt;">script</span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">></span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
window.setTimeout(setIframeContent,1000);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">function</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
setIframeContent() {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "courier new"; font-size: 9.5pt;">//set content or script</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">var</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> html = </span><span style="background: white; color: #a31515; font-family: "courier new"; font-size: 9.5pt;">'<body><div>my content</div><scr'</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> + </span><span style="background: white; color: #a31515; font-family: "courier new"; font-size: 9.5pt;">'ipt>alert("test")</'</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> + </span><span style="background: white; color: #a31515; font-family: "courier new"; font-size: 9.5pt;">'script></body>'</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "courier new"; font-size: 9.5pt;">//get reference to iframe and load the content to iframe</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">var</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> iframe =
document.getElementById(</span><span style="background: white; color: #a31515; font-family: "courier new"; font-size: 9.5pt;">"myiframe"</span><span style="background: white; font-family: "courier new"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
iframe.contentWindow.document.open();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
iframe.contentWindow.document.write(html);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;">
iframe.contentWindow.document.close();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="background: white; font-family: "courier new"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;"></</span><span style="background: white; color: maroon; font-family: "courier new"; font-size: 9.5pt;">script</span><span style="background: white; color: blue; font-family: "courier new"; font-size: 9.5pt;">></span><o:p></o:p></div>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-65127625767725088132016-04-19T08:10:00.001-07:002016-04-19T08:10:29.047-07:00Add HTTP access control (CORS) to response<div class="MsoNormal">
For security reasons, browsers restrict cross-origin HTTP
requests initiated from within scripts.
For example, XMLHttpRequest follows the same-origin policy. So, a web application
using XMLHttpRequest could only make HTTP requests to its own domain unless
CORS headers are added in the response.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Given below is the code to add CORS header to response in
asp.net<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">HttpContext</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">.Current.Response.AddHeader(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"Access-Control-Allow-Origin"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">, </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"domain.com" </span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">HttpContext</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">.Current.Response.AddHeader(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"Access-Control-Allow-Credentials"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">, </span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"true"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">);</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
If you want to include CORS header for requests from all
domains, you have to format the referrer url to get the correct domain<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> GetDomain(</span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> referrer)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> tmpReferrer = </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">String</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">.IsNullOrEmpty(referrer)
|| referrer.Length < 5 ? </span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">""</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> : referrer;<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">// strip out the domain portion of the referring url, if
there is a starting point</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> (tmpReferrer.Length > 0
&& tmpReferrer.Substring(0, 4) == </span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"http"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">//remove string after first /</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> (tmpReferrer.IndexOf(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"/"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">,
8) > 0)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">
tmpReferrer = tmpReferrer.Substring(0, tmpReferrer.IndexOf(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"/"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">,
8));<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">//remove string after first ?</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> (tmpReferrer.IndexOf(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"?"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">)
> 0)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">
tmpReferrer = tmpReferrer.Substring(0, tmpReferrer.IndexOf(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"?"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">));<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">else</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> (tmpReferrer.Length > 0)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">//remove string after first /</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> (tmpReferrer.IndexOf(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"/"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">)
> 0)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">
tmpReferrer = tmpReferrer.Substring(0, tmpReferrer.IndexOf(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"/"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">,
8));<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> tmpReferrer;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">}</span><span style="font-family: 'Courier New'; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
And then call the function to add domain name to CORS header<span style="font-family: 'Courier New'; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> referrer = </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">HttpContext</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">.Current.Request.ServerVariables[</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"HTTP_REFERER"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">];<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">HttpContext</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">.Current.Response.AddHeader(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"Access-Control-Allow-Origin"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">, GetDomain(referrer));<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">HttpContext</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">.Current.Response.AddHeader(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"Access-Control-Allow-Credentials"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">, </span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"true"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">);</span><span style="font-family: 'Courier New'; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
JS code to call endpoint<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">var</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> httpRequest = </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> XMLHttpRequest();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> httpRequest.open(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"GET"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">, </span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"http://somedomain.com/endpoint"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">, </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">true</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> httpRequest.withCredentials = </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">true</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> httpRequest.send();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> httpRequest.onreadystatechange = </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">function</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> () {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> (httpRequest.readyState ==
4) {</span><span style="background: white; color: green; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">//wait for request to complete</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">
console.log(httpRequest.responseText);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> };</span><o:p></o:p></div>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-39902463508092859142016-04-19T07:51:00.002-07:002016-04-19T07:53:09.225-07:00How to add semi transparent backgroundYou can set semi transparent background on a div or any html element by adding below style -<br />
<br />
<b>background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.5) 0%,rgba(255, 255, 255, 0.5) 100%), url(http://www.bestmanspeechestoasts.com/wp-content/themes/thesis/rotator/sample-4.jpg);</b><br />
<b><br /></b>
<br />
You can see the example here - http://codepen.io/deebujacob/pen/xVzRxw
<br />
<br />
<div style="background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.5) 0%,rgba(255, 255, 255, 0.5) 100%), url(http://www.bestmanspeechestoasts.com/wp-content/themes/thesis/rotator/sample-4.jpg); height: 300px; width: 500px;">
</div>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-20451593288195581572015-12-02T12:54:00.000-08:002015-12-02T12:54:16.918-08:00LINQ - Group multiple columns or keys <div class="MsoNormal">
Using .NET linq you can easily group data in a list. This is an
example of grouping data based on multiple keys.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This is the class that I created for testing<o:p></o:p></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">public</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">class</span>
<span style="color: #2b91af;">Employee</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">public</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">string</span>
Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>;
}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">public</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">string</span>
Department { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>;
}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">public</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">string</span>
SubDepartment { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>;
}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">public</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">float</span>
Salary { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>;
}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This is the multi- key grouping using LINQ<o:p></o:p></div>
<div class="MsoNormal">
<span style="color: green; font-family: "Courier New"; font-size: 9.5pt;">/*create employee
list*/</span><span style="font-family: "Courier New"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: #2b91af; font-family: "Courier New"; font-size: 9.5pt;">List</span><span style="font-family: "Courier New"; font-size: 9.5pt;"><<span style="color: #2b91af;">Employee</span>>
emps = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Employee</span>>();<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: green; font-family: "Courier New"; font-size: 9.5pt;">/*Add test data
*/</span><span style="font-family: "Courier New"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">emps.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>() {
Name = <span style="color: #a31515;">"Employee 1"</span>, Department = <span style="color: #a31515;">"HR"</span>, SubDepartment=<span style="color: #a31515;">"Recruitment"</span>, Salary = 4000 });<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">emps.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>() {
Name = <span style="color: #a31515;">"Employee 2"</span>, Department = <span style="color: #a31515;">"HR"</span>,SubDepartment=<span style="color: #a31515;">"Recruitment"</span>, Salary = 4500 });<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">emps.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>() {
Name = <span style="color: #a31515;">"Employee 3"</span>, Department = <span style="color: #a31515;">"Tech"</span>,SubDepartment=<span style="color: #a31515;">"Engineering"</span>, Salary = 5000 });<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">emps.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>() {
Name = <span style="color: #a31515;">"Employee 4"</span>, Department = <span style="color: #a31515;">"Tech"</span>,SubDepartment=<span style="color: #a31515;">"Engineering"</span>, Salary = 5200 });<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: green; font-family: "Courier New"; font-size: 9.5pt;">/*group data
based on departments*/</span><span style="font-family: "Courier New"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: #2b91af; font-family: "Courier New"; font-size: 9.5pt;">List</span><span style="font-family: "Courier New"; font-size: 9.5pt;"><<span style="color: #2b91af;">Employee</span>>
newlist = (<span style="color: blue;">from</span> x <span style="color: blue;">in</span>
emps<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">group</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> x <span style="color: blue;">by</span>
<span style="color: blue;">new</span> { x.Department, x.SubDepartment } <span style="color: blue;">into</span> grp<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">select</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">new</span>
<span style="color: #2b91af;">Employee</span>()<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">Department =
grp.Key.Department,<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">SubDepartment =
grp.Key.SubDepartment,<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">Salary = grp.Sum(x =>
x.Salary)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">).ToList();<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<br /></div>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-65971325382029177022015-04-07T07:59:00.003-07:002015-04-08T08:10:32.241-07:00HTML help/info control<div class="MsoNormal">
<div class="MsoNormal">
Given below is code to show help information next to
controls(like textbox, dropdownlist etc). This will show help info next to the
control “onfocus” event. This feature can be attached to other events also like
“onmouseover”,”onclick” etc.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
The control will look like this - <o:p></o:p></div>
</div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt; mso-no-proof: yes;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_ZzXDSmna180sXSTqFZwz5gIa7M4-3DIrXajdNJ6BnihOe_ylwMbuCkE_QROqIJQ9PjH41rjDtmsELL7XsQgSyKp6pwMGCZ_MwVf6GBBTrX61niBwv_N09IOqt2Ud0zPHiTN2J9Q__2FD/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_ZzXDSmna180sXSTqFZwz5gIa7M4-3DIrXajdNJ6BnihOe_ylwMbuCkE_QROqIJQ9PjH41rjDtmsELL7XsQgSyKp6pwMGCZ_MwVf6GBBTrX61niBwv_N09IOqt2Ud0zPHiTN2J9Q__2FD/s1600/Untitled.png" height="57" width="320" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<div class="MsoNormal">
Set the script and style in html head<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"><</span><span style="color: maroon; font-family: "Courier New"; font-size: 9.5pt;">script</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">></span><span style="font-family: "Courier New"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">var</span>
prevmsgControlName;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">var</span>
timeoutVar;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">function</span>
showControlMessage(control, msg) {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">var</span>
msgControlName = <span style="color: maroon;">'message_'</span> + control.id;<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">if</span>
(prevmsgControlName) {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">
document.getElementById(prevmsgControlName).style.display = <span style="color: maroon;">"none"</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">if</span>
(document.getElementById(msgControlName) == <span style="color: blue;">null</span>)
{<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">var</span>
tempDiv = document.createElement(<span style="color: maroon;">"div"</span>);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> tempDiv.id = msgControlName;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">var</span>
rect = control.getBoundingClientRect();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> tempDiv.innerHTML = msg;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> tempDiv.className = <span style="color: maroon;">"client_message"</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> tempDiv.style.left =
Number(rect.left + control.offsetWidth + 5) + <span style="color: maroon;">"px"</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> tempDiv.style.top = rect.top + <span style="color: maroon;">"px"</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">
control.parentNode.appendChild(tempDiv);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> clearTimeout(timeoutVar);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> timeoutVar =
setTimeout(hideControlMessage, 8000);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> prevmsgControlName =
msgControlName;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">else</span> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> clearTimeout(timeoutVar);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">
document.getElementById(msgControlName).style.display = <span style="color: maroon;">"block"</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> timeoutVar =
setTimeout(hideControlMessage, 8000);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> prevmsgControlName =
msgControlName;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">function</span>
hideControlMessage() {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> clearTimeout(timeoutVar);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">
document.getElementById(prevmsgControlName).style.display = <span style="color: maroon;">"none"</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"></</span><span style="color: maroon; font-family: "Courier New"; font-size: 9.5pt;">script</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">></span><span style="font-family: "Courier New"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"><</span><span style="color: maroon; font-family: "Courier New"; font-size: 9.5pt;">style</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">></span><span style="font-family: "Courier New"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: maroon; font-family: "Courier New"; font-size: 9.5pt;">.client_message</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: red;">border</span>:<span style="color: blue;">1px</span> <span style="color: blue;">solid</span> <span style="color: blue;">black</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: red;">background-color</span>:<span style="color: blue;">rgb(250, 250, 188)</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: red;">padding</span>:<span style="color: blue;">5px</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: red;">position</span>:<span style="color: blue;">absolute</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: red;">font-size</span>:<span style="color: blue;">90%</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: red;">width</span>: <span style="color: blue;">300px</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: red;">border-radius</span>:<span style="color: blue;">2px</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"></</span><span style="color: maroon; font-family: "Courier New"; font-size: 9.5pt;">style</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Once the script and style are set in head, you can add help
info to control as given below.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"><</span><span style="color: maroon; font-family: "Courier New"; font-size: 9.5pt;">input id="control1"</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: red;">type</span><span style="color: blue;">="text"</span> <span style="color: red;">onfocus</span><span style="color: blue;">="showControlMessage(this,'Replace with help info you
want to show for the control')"</span> <span style="color: blue;">/><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
<br /></div>
</div>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-6432164850779200342015-01-30T08:26:00.005-08:002015-01-30T08:28:06.944-08:00How to use SOLR.NET to add/search documents from SOLR<div class="MsoNormal">
In this article we will look into how to use SOLR to add and
query data using SOLR.NET</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<u>Install Apache Tomcat and SOLR<o:p></o:p></u></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
1. Install Java version 6 or above<o:p></o:p></div>
<div class="MsoListParagraph">
<br /></div>
<div class="MsoNormal">
2.Download Apache Tomcat -Core: 32-bit/64-bit Windows
Service Installer (pgp, md5) http://tomcat.apache.org/download-70.cgi<o:p></o:p></div>
<div class="MsoNormal">
After you install apache tomcat you can check if service is
running by checking the location -http://localhost:8080/<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
3.Download solr (zip) file <a href="http://www.gtlib.gatech.edu/pub/apache/lucene/solr/4.10.3/">http://www.gtlib.gatech.edu/pub/apache/lucene/solr/4.10.3/</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Details on how to install and run SOLR is clearly stated in
the below awesome articles <o:p></o:p></div>
<div class="MsoNormal">
<a href="http://www.lawruk.com/blog/55/how-to-install-solr-on-windows-8">http://www.lawruk.com/blog/55/how-to-install-solr-on-windows-8</a><o:p></o:p></div>
<div class="MsoNormal">
<a href="http://blog.navigationarts.com/installing-solr-4-6-on-windows/">http://blog.navigationarts.com/installing-solr-4-6-on-windows/</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<u>Use SOLR.NET to connect to SOLR<o:p></o:p></u></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
So now that we have Apache and SOLR running let’s look at how
to use this in ASP.NET(C#)<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
1.Download SOLR.NET - <a href="https://github.com/mausch/SolrNet">https://github.com/mausch/SolrNet</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
2.Compile the project and get the dlls from SOLRNET project
(there will be SolrNet.dll, Microsoft.Practices.ServiceLocation.dll, HttpWebAdapters.dll
and the pdb and xml files)<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
3.Create an empty ASP.NET project<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
4.Create a new folder in this project and copy the dlls from
SOLRNET project (step 2) to that folder.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
5.Add references to the 3 dlls <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
6.Add Global.asax and initialize SOLR.NET connection in
application start<o:p></o:p></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">protected</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">void</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> Application_Start(</span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">object</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> sender, </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">EventArgs</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> e)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">Startup</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">.Init<SOLRPOST.</span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">PostData</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">.</span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrSearchResult</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">>(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"http://localhost:8080/solr"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
7.Create a class which has the same fields as the schema in
SOLR. For testing purpose I added only 2 fields from schema<o:p></o:p></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">class</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrSearchResult</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> [</span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrField</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"id"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">)]<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> id { </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">get</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">; </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">set</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> [</span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrField</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"title"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">)]<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> title { </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">get</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">; </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">set</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> }</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
8.Now we are ready to add documents to SOLR! Given below is the
function to add documents to SOLR<o:p></o:p></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">void</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> AddData()<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">ISolrOperations</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrSearchResult</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">>
solr = </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">ServiceLocator</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">.Current.GetInstance<</span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">ISolrOperations</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrSearchResult</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">>>();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrSearchResult</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">
test = </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrSearchResult</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">() { id = </span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"changeme2"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">, title = </span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"changeme2"</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> };<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">
solr.Add(test);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">
solr.Commit();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> }</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
9.To search data use the below function<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">void</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> SearchData()<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">ISolrOperations</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrSearchResult</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">>
solr = </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">ServiceLocator</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">.Current.GetInstance<</span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">ISolrOperations</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrSearchResult</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">>>();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrQueryResults</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrSearchResult</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">>
results = solr.Query(</span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrQuery</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">(</span><span style="background: white; color: #a31515; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">"id:\"changeme2\""</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">));<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">foreach</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> (</span><span style="background: white; color: #2b91af; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">SolrSearchResult</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">
result </span><span style="background: white; color: blue; font-family: "Courier New"; font-size: 9.5pt; mso-highlight: white;">in</span><span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> results)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;">
Response.Write(result.title);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: 'Courier New'; font-size: 9.5pt;"> }</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
<br /></div>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com11tag:blogger.com,1999:blog-6643932501385666172.post-46063540431681131342015-01-29T08:05:00.001-08:002015-01-29T08:05:09.046-08:00Group and pivot data in SQL<div class="MsoNormal">
Given below is an example of how to group and pivot data in
SQL .This example shows you how to group data in CSV format and pivot it.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: green; font-family: Consolas; font-size: 9.5pt;">--Create temp table</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">CREATE</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">TABLE</span> #car_data<span style="color: blue;"> </span><span style="color: grey;">(</span>company <span style="color: blue;">varchar</span><span style="color: grey;">(</span>10<span style="color: grey;">),</span> model <span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">))</span><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: green; font-family: Consolas; font-size: 9.5pt;">--Insert data</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">INSERT</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">INTO</span>
#car_data<span style="color: blue;"> </span><span style="color: grey;">(</span>company<span style="color: grey;">,</span>model<span style="color: grey;">)</span> <span style="color: blue;">values </span><span style="color: grey;">(</span><span style="color: red;">'Ford'</span><span style="color: grey;">,</span><span style="color: red;">'Escape'</span><span style="color: grey;">)</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">INSERT</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">INTO</span>
#car_data<span style="color: blue;"> </span><span style="color: grey;">(</span>company<span style="color: grey;">,</span>model<span style="color: grey;">)</span> <span style="color: blue;">values </span><span style="color: grey;">(</span><span style="color: red;">'Ford'</span><span style="color: grey;">,</span><span style="color: red;">'Explorer'</span><span style="color: grey;">)</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">INSERT</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">INTO</span>
#car_data<span style="color: blue;"> </span><span style="color: grey;">(</span>company<span style="color: grey;">,</span>model<span style="color: grey;">)</span> <span style="color: blue;">values </span><span style="color: grey;">(</span><span style="color: red;">'Ford'</span><span style="color: grey;">,</span><span style="color: red;">'Expedition'</span><span style="color: grey;">)</span><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">INSERT</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">INTO</span>
#car_data<span style="color: blue;"> </span><span style="color: grey;">(</span>company<span style="color: grey;">,</span>model<span style="color: grey;">)</span> <span style="color: blue;">values </span><span style="color: grey;">(</span><span style="color: red;">'Chevy'</span><span style="color: grey;">,</span><span style="color: red;">'Cruze'</span><span style="color: grey;">)</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">INSERT</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">INTO</span>
#car_data<span style="color: blue;"> </span><span style="color: grey;">(</span>company<span style="color: grey;">,</span>model<span style="color: grey;">)</span> <span style="color: blue;">values </span><span style="color: grey;">(</span><span style="color: red;">'Chevy'</span><span style="color: grey;">,</span><span style="color: red;">'Impala'</span><span style="color: grey;">)</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">INSERT</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">INTO</span>
#car_data<span style="color: blue;"> </span><span style="color: grey;">(</span>company<span style="color: grey;">,</span>model<span style="color: grey;">)</span> <span style="color: blue;">values </span><span style="color: grey;">(</span><span style="color: red;">'Chevy'</span><span style="color: grey;">,</span><span style="color: red;">'Silverado'</span><span style="color: grey;">)</span><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: green; font-family: Consolas; font-size: 9.5pt;">-- Group data in CSV
format</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">SELECT</span><span style="font-family: Consolas; font-size: 9.5pt;">
t<span style="color: grey;">.</span>company<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: grey;">,</span><span style="color: magenta;">STUFF</span><span style="color: grey;">((</span><span style="color: blue;">SELECT</span> <span style="color: red;">', '</span> <span style="color: grey;">+</span> model<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">FROM</span>
#car_data <span style="color: blue;">with</span><span style="color: grey;">(</span><span style="color: blue;">nolock</span><span style="color: grey;">)</span> <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">WHERE</span>
company <span style="color: grey;">=</span> t<span style="color: grey;">.</span>company<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">FOR</span> <span style="color: blue;">XML</span> <span style="color: blue;">PATH</span><span style="color: grey;">(</span><span style="color: red;">''</span><span style="color: grey;">),</span> <span style="color: blue;">TYPE</span><span style="color: grey;">)</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: grey;">.</span>value<span style="color: grey;">(</span><span style="color: red;">'.'</span><span style="color: grey;">,</span><span style="color: red;">'VARCHAR(400)'</span><span style="color: grey;">),</span>1<span style="color: grey;">,</span>2<span style="color: grey;">,</span><span style="color: red;">' '</span><span style="color: grey;">)</span> List_Output<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">FROM</span><span style="font-family: Consolas; font-size: 9.5pt;"> #car_data t <span style="color: blue;">with</span><span style="color: grey;">(</span><span style="color: blue;">nolock</span><span style="color: grey;">)</span> <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">GROUP</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">BY</span>
t<span style="color: grey;">.</span>company <o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<br /></div>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com1tag:blogger.com,1999:blog-6643932501385666172.post-75217305602513503642014-09-11T12:33:00.001-07:002014-09-11T12:33:04.942-07:00Add Excel type Filtering to Repeater or Gridview in ASP.NET<div class="MsoNormal">
In this example we will add dropdown list filter to
repeater. The filters will be created dynamically at runtime.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
1) Create a repeater control <o:p></o:p></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"><</span><span style="color: maroon; font-family: "Courier New"; font-size: 9.5pt;">asp</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">:</span><span style="color: maroon; font-family: "Courier New"; font-size: 9.5pt;">Repeater</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: red;">runat</span><span style="color: blue;">="server"</span> <span style="color: red;">ID</span><span style="color: blue;">="rptData"</span> <span style="color: red;">OnItemCreated</span><span style="color: blue;">="rptData_ItemCreated"</span> <span style="color: red;">EnableViewState</span><span style="color: blue;">="false"></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">HeaderTemplate</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">table</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">tr</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">td</span><span style="color: blue;">></span>Department<span style="color: blue;"></</span><span style="color: maroon;">td</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">td</span><span style="color: blue;">></span>Name<span style="color: blue;"></</span><span style="color: maroon;">td</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">td</span><span style="color: blue;">></span>Age<span style="color: blue;"></</span><span style="color: maroon;">td</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"></</span><span style="color: maroon;">tr</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">tr</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">td</span> <span style="color: red;">id</span><span style="color: blue;">="Department"</span> <span style="color: red;">runat</span><span style="color: blue;">="server"</span> <span style="color: blue;">></</span><span style="color: maroon;">td</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">td</span> <span style="color: red;">id</span><span style="color: blue;">="Name"</span> <span style="color: red;">runat</span><span style="color: blue;">="server"</span> <span style="color: blue;">></</span><span style="color: maroon;">td</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">td</span> <span style="color: red;">id</span><span style="color: blue;">="Age"</span> <span style="color: red;">runat</span><span style="color: blue;">="server"></</span><span style="color: maroon;">td</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"></</span><span style="color: maroon;">tr</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"></</span><span style="color: maroon;">HeaderTemplate</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">ItemTemplate</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">tr</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">td</span><span style="color: blue;">></span><span style="background: yellow; mso-highlight: yellow;"><%</span><span style="color: blue;">#</span> Eval(<span style="color: #a31515;">"Department"</span>)
<span style="background: yellow; mso-highlight: yellow;">%></span><span style="color: blue;"></</span><span style="color: maroon;">td</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">td</span><span style="color: blue;">></span><span style="background: yellow; mso-highlight: yellow;"><%</span><span style="color: blue;">#</span> Eval(<span style="color: #a31515;">"Name"</span>)
<span style="background: yellow; mso-highlight: yellow;">%></span><span style="color: blue;"></</span><span style="color: maroon;">td</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">td</span><span style="color: blue;">></span><span style="background: yellow; mso-highlight: yellow;"><%</span><span style="color: blue;">#</span> Eval(<span style="color: #a31515;">"Age"</span>)
<span style="background: yellow; mso-highlight: yellow;">%></span><span style="color: blue;"></</span><span style="color: maroon;">td</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"></</span><span style="color: maroon;">tr</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"></</span><span style="color: maroon;">ItemTemplate</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">FooterTemplate</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"></</span><span style="color: maroon;">table</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"></</span><span style="color: maroon;">FooterTemplate</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"></</span><span style="color: maroon;">asp</span><span style="color: blue;">:</span><span style="color: maroon;">Repeater</span><span style="color: blue;">><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
2) Create an Employee class <o:p></o:p></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">[<span style="color: #2b91af;">Serializable</span>]<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Employee</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">public</span>
<span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>;
<span style="color: blue;">set</span>; }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">public</span>
<span style="color: blue;">int</span> Age { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">public</span>
<span style="color: blue;">string</span> Department { <span style="color: blue;">get</span>;
<span style="color: blue;">set</span>; }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">public</span>
Employee() { }<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">public</span>
<span style="color: blue;">static</span> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Employee</span>> GetEmployees()<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Employee</span>> employees = <span style="color: blue;">new</span>
<span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Employee</span>>();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> employees.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>() {
Name = <span style="color: #a31515;">"Victor"</span>, Age = 31,
Department = <span style="color: #a31515;">"Finance"</span> });<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> employees.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>() {
Name = <span style="color: #a31515;">"Jeff"</span>, Age = 35,
Department = <span style="color: #a31515;">"Finance"</span> });<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> employees.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>() {
Name = <span style="color: #a31515;">"Tom"</span>, Age = 42, Department
= <span style="color: #a31515;">"IT"</span> });<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> employees.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>() {
Name = <span style="color: #a31515;">"Matt"</span>, Age = 42,
Department = <span style="color: #a31515;">"IT"</span> });<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">return</span>
employees;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
3) On page load bind data to the repeater<o:p></o:p></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">protected</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">void</span>
Page_Load(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> rptData.DataSource = <span style="color: #2b91af;">Employee</span>.GetEmployees();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> rptData.DataBind();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
OK now we will start the code to add filtering to the
repeater. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
4) Add “ItemCreated” Event<o:p></o:p></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">protected</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">void</span>
rptData_ItemCreated(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">RepeaterItemEventArgs</span> e)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: green;">//Check
if item is header</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">if</span>
(e.Item.ItemType == <span style="color: #2b91af;">ListItemType</span>.Header)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: green;">//Get
employee data</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Employee</span>> tempData = ViewState[<span style="color: #a31515;">"filtered_data"</span>] == <span style="color: blue;">null</span>?<span style="color: #2b91af;">Employee</span>.GetEmployees():(<span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Employee</span>>)ViewState[<span style="color: #a31515;">"filtered_data"</span>] ;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: green;">//Get
properties of employee data. We will be adding drop down list filters for these
properties</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">foreach</span>
(<span style="color: #2b91af;">PropertyInfo</span> propertyInfo <span style="color: blue;">in</span> <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Employee</span>).GetProperties())<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">if</span>
(e.Item.FindControl(propertyInfo.Name) != <span style="color: blue;">null</span>)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: green;">//Create dropdownlist</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: #2b91af;">DropDownList</span> ddltemp = <span style="color: blue;">new</span> <span style="color: #2b91af;">DropDownList</span>();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> ddltemp.Items.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">ListItem</span>(<span style="color: #a31515;">"Select"</span>, <span style="color: blue;">string</span>.Empty));<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: green;">//Get value of the selected property from the list</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">foreach</span> (<span style="color: blue;">string</span> value <span style="color: blue;">in</span> (<span style="color: blue;">from</span> x <span style="color: blue;">in</span> tempData <span style="color: blue;">select</span>
propertyInfo.GetValue(x, <span style="color: blue;">null</span>).ToString()).Distinct().ToList())<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> ddltemp.Items.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">ListItem</span>(value,
value));<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> ddltemp.Attributes.Add(<span style="color: #a31515;">"style"</span>, <span style="color: #a31515;">"width:70px;"</span>);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> ddltemp.ID = <span style="color: #a31515;">"ddl"</span>
+ propertyInfo.Name;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: green;">//Set autopost back</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> ddltemp.AutoPostBack = <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: green;">//Add event handler</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">
ddltemp.SelectedIndexChanged += <span style="color: blue;">new</span> <span style="color: #2b91af;">EventHandler</span>(ddlTemp_SelectedIndexChanged);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: green;">//Add dropdownlist to the repeater header</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">
e.Item.FindControl(propertyInfo.Name).Controls.Add(ddltemp);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
5) Add dropdownlist post back event to filter data<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">protected</span> <span style="color: blue;">void</span> ddlTemp_SelectedIndexChanged(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span>
e)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: green;">//Get
dropdownlist object that send the request</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: #2b91af;">DropDownList</span>
ddlTemp = (<span style="color: #2b91af;">DropDownList</span>)sender;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Employee</span>> tempData = ViewState[<span style="color: #a31515;">"filtered_data"</span>] == <span style="color: blue;">null</span> ? <span style="color: #2b91af;">Employee</span>.GetEmployees()
: (<span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Employee</span>>)ViewState[<span style="color: #a31515;">"filtered_data"</span>];<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: green;">//Filter
the list based on the selected value of the property</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Employee</span>> temp = (<span style="color: blue;">from</span>
x <span style="color: blue;">in</span> tempData<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">where</span> x.GetType().GetProperties().Where(y =>
y.Name == ddlTemp.ID.Replace(<span style="color: #a31515;">"ddl"</span>,
<span style="color: #a31515;">""</span>)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">
&& y.GetValue(x, <span style="color: blue;">null</span>).ToString()
== ddlTemp.SelectedItem.Text).Count() > 0<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">select</span> x).ToList();<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> ViewState[<span style="color: #a31515;">"filtered_data"</span>]
= temp;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> rptData.DataSource = temp;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> rptData.DataBind();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
That’s it! Run the application and you can filter records
from header of the repeater!<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoListParagraph">
<br /></div>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0tag:blogger.com,1999:blog-6643932501385666172.post-87118610436943176572014-08-06T12:32:00.001-07:002014-08-06T12:32:24.254-07:00Create AJAX dropdownlist (select) using jQuery<br />
<br />
<div class="MsoNormal">
Given below is an example of AJAX drop down list which fills
data by making an AJAX call and also persists data on post back.<o:p></o:p></div>
<div class="MsoNormal">
<o:p><br /></o:p></div>
<div class="MsoNormal">
<o:p>1) C</o:p><span style="text-indent: -0.25in;">reate an AJAX handler</span></div>
<div class="MsoListParagraph" style="mso-list: l0 level1 lfo1; text-indent: -.25in;">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">public</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">class</span>
<span style="color: #2b91af;">AjaxHandler</span> : <span style="color: #2b91af;">IHttpHandler</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">void</span> ProcessRequest(<span style="color: #2b91af;">HttpContext</span>
context)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">if</span>
(<span style="color: #2b91af;">HttpContext</span>.Current.Request.QueryString[<span style="color: #a31515;">"getDDLData"</span>] == <span style="color: #a31515;">"1"</span>)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">DDLData</span>> values = <span style="color: blue;">new</span>
<span style="color: #2b91af;">List</span><<span style="color: #2b91af;">DDLData</span>>();<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> values.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">DDLData</span>() {
Text = <span style="color: #a31515;">"Option1"</span>, Value = <span style="color: #a31515;">"1"</span> });<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> values.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">DDLData</span>() {
Text = <span style="color: #a31515;">"Option2"</span>, Value = <span style="color: #a31515;">"2"</span> });<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> values.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">DDLData</span>() {
Text = <span style="color: #a31515;">"Option3"</span>, Value = <span style="color: #a31515;">"3"</span> });<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: #2b91af;">JavaScriptSerializer</span>
serializer = <span style="color: blue;">new</span> <span style="color: #2b91af;">JavaScriptSerializer</span>();<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">string</span>
result = serializer.Serialize(values);<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> context.Response.ContentType = <span style="color: #a31515;">"text/json"</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> context.Response.Write(result.ToString());<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">DDLData</span> { <span style="color: blue;">public</span> <span style="color: blue;">string</span> Text; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Value;
}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">bool</span> IsReusable<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">get</span>
{ <span style="color: blue;">return</span> <span style="color: blue;">true</span>;
}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Make sure that you declare AJAX handler information in
web.config. To learn about AJAX call check the AJAX call related articles this
blog<o:p></o:p></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"> <</span><span style="color: #a31515; font-family: "Courier New"; font-size: 9.5pt;">system.webServer</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">></span><span style="font-family: "Courier New"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"> <</span><span style="color: #a31515; font-family: "Courier New"; font-size: 9.5pt;">modules</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"> </span><span style="color: red; font-family: "Courier New"; font-size: 9.5pt;">runAllManagedModulesForAllRequests</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">=</span><span style="font-family: "Courier New"; font-size: 9.5pt;">"<span style="color: blue;">true</span>"<span style="color: blue;">/></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"> <</span><span style="color: #a31515; font-family: "Courier New"; font-size: 9.5pt;">handlers</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">></span><span style="font-family: "Courier New"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"> <</span><span style="color: #a31515; font-family: "Courier New"; font-size: 9.5pt;">add</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"> </span><span style="color: red; font-family: "Courier New"; font-size: 9.5pt;">name</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">=</span><span style="font-family: "Courier New"; font-size: 9.5pt;">"<span style="color: blue;">ajaxHandler</span>"<span style="color: blue;"> </span><span style="color: red;">verb</span><span style="color: blue;">=</span>"<span style="color: blue;">GET,POST</span>"<span style="color: blue;"> </span><span style="color: red;">path</span><span style="color: blue;">=</span>"<span style="color: blue;">RequestHandler.aspx</span>"<span style="color: blue;"> </span><span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">AJAXAsptest.AjaxHandler</span>"<span style="color: blue;"> /></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"> </</span><span style="color: #a31515; font-family: "Courier New"; font-size: 9.5pt;">handlers</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">></span><span style="font-family: "Courier New"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"> </</span><span style="color: #a31515; font-family: "Courier New"; font-size: 9.5pt;">system.webServer</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal">
<o:p> 2) </o:p><span style="text-indent: -0.25in;">Create a “Select” element in HTML</span></div>
<div class="MsoListParagraph" style="mso-list: l0 level1 lfo1; text-indent: -.25in;">
<o:p></o:p></div>
<div class="MsoListParagraph">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">select</span> <span style="color: red;">name</span><span style="color: blue;">="ddlData"</span> <span style="color: red;">id</span><span style="color: blue;">="ddlData"></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">option</span><span style="color: blue;">></</span><span style="color: maroon;">option</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none; text-indent: .5in;">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"></</span><span style="color: maroon; font-family: "Courier New"; font-size: 9.5pt;">select</span><span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;">></span><span style="font-family: "Courier New"; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none; text-indent: .5in;">
<span style="font-family: "Courier New"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal">
<o:p> 3)</o:p><span style="font-size: 7pt; text-indent: -0.25in;"> </span><span style="text-indent: -0.25in;">Make AJAX call to the handler and fill the
dropdown list</span></div>
<div class="MsoListParagraph" style="mso-list: l0 level1 lfo1; text-indent: -.25in;">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 9.5pt;"><</span><span style="color: maroon; font-family: "Courier New"; font-size: 9.5pt;">script</span><span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: red;">src</span><span style="color: blue;">="//code.jquery.com/jquery-1.11.0.min.js"></</span><span style="color: maroon;">script</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"><</span><span style="color: maroon;">script</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">var</span>
selectedValue = <span style="color: maroon;">'<span style="background: yellow; mso-highlight: yellow;"><%</span>=
Request.Form["ddlData"]!=null?Request.Form["ddlData"]:""<span style="background: yellow; mso-highlight: yellow;">%></span>'</span>;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> $.ajax({ url: <span style="color: maroon;">"RequestHandler.aspx?getDDLData=1"</span>,<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> success: <span style="color: blue;">function</span>
(data) {<span style="color: darkgreen;">//Get result object from server</span><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">var</span>
elementRef = $(<span style="color: maroon;">"#ddlData"</span>);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> elementRef.append(<span style="color: blue;">new</span> Option(<span style="color: maroon;">"Select"</span>,
<span style="color: maroon;">"Select"</span>, <span style="color: blue;">false</span>,
<span style="color: blue;">true</span>));<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> $.each(data, <span style="color: blue;">function</span> () {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> elementRef.append(<span style="color: blue;">new</span> Option(<span style="color: blue;">this</span>.Text,
<span style="color: blue;">this</span>.Value));<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> });<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;">if</span>
(selectedValue != <span style="color: maroon;">''</span>) {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">
elementRef.val(selectedValue);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> });<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;"> <span style="color: blue;"></</span><span style="color: maroon;">script</span><span style="color: blue;">><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<o:p> 4) </o:p><span style="text-indent: -0.25in;">On server side you can get the selected value of
dropdown from the “form” data</span></div>
<div class="MsoListParagraph" style="mso-list: l0 level1 lfo1; text-indent: -.25in;">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 9.5pt;">Response.Write(Request.Form[<span style="color: #a31515;">"ddlData"</span>]);<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal" style="text-align: left;">
<br /></div>
Deebu Jacobhttp://www.blogger.com/profile/01700608038957444888noreply@blogger.com0