Viktar Karpach Web Developer Blog about ASP.NET,C#,T-SQL,Javascript,CSS,SilverlightViktar Karpach Web Developer Blog about ASP.NET,C#,T-SQL,Javascript,CSS,Silverlight. New technics and tehnlogies reviews.en-usKarpach Web Developer Bloghttp://www.karpach.com/control-iphone-remotely.htmhttp://www.karpach.com/control-iphone-remotely.htmIPhoneHow to control your iPhone remotely?<p>Remote control of your iPhone might be very useful for testing iPhone friendly web sites. You can use emulators for this purpose. <a target="_blank" href="http://labs.blackbaud.com/NetCommunity/article?artid=662">Blackboud iPhone Browser Simulator</a> is not bad, but still this is not a real iPhone browser.</p> <p>First of all you iPhone needs to be jailbroken. Then you need to install VNC Server for iPhone. Just search for Veency in Cydia, it is free.</p> <p>On you desktop you need VNC Viewer. There are bunch of them, but I use <a target="_blank" href="http://www.uvnc.com/">UltraVNC</a> since it is free. Then you can connect to your iPhone over Wi-Fi, but you can do it thru USB too.</p> <p>For USB connection you need to install <a target="_blank" href="http://www.python.org/download/releases/2.6.4/">python interpreter</a> and <a target="_blank" href="http://marcansoft.com/blog/iphonelinux/usbmuxd/"> usbmuxd</a>. From usbmuxd we need only <a href="http://www.karpach.com/files/usbmuxd.zip">python client part</a>. Run connect.bat, which has following line inside:</p> <p>c:\python26\python.exe tcprelay.py 5900</p> <p>And then you can connect to your iPhone using 127.0.0.1 address.</p> <p><img alt="iPhone remote view" src="http://www.karpach.com/images/uploaded/iPhone-remote.png" /></p>Fri, 09 Jul 2010 00:00:00 -07002010-07-09T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/viewstateexception-invalid-viewstate.htmhttp://www.karpach.com/viewstateexception-invalid-viewstate.htmASP.NETHow to fix ViewStateException Invalid ViewState?<p>Usually you don't see Invalid ViewState exception unless you implement some kind of <a href="http://www.karpach.com/ASP-NET-Global-asax-error-logger.htm">ASP.NET error logger</a> in Global.asax.</p> <p>Error usually states something like this:</p> <p><b>Exception:</b> System.Web.HttpException: The state information is invalid for this page and might be corrupted. ---&gt; System.Web.UI.ViewStateException: Invalid viewstate.</p> <p>Those errors are usually generated by some automatic submission form engines. Comments and contact forms are mostly effected by this exception. End user never sees those errors, so invalid viewstate errors don't have any value for your error logger. Let's bypass them:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">if</span> (ex <span class="cb1">is</span> <span class="cb2">HttpException</span> &amp;&amp; ex.InnerException <span class="cb1">is</span> <span class="cb2">ViewStateException</span>)</pre> <pre class="cl"> {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; _app.Response.Redirect(_app.Request.Url.AbsoluteUri)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">return</span></pre> <pre class="cl"> }</pre> </div>Wed, 23 Jun 2010 00:00:00 -07002010-06-23T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/xrefresh-alternative.htmhttp://www.karpach.com/xrefresh-alternative.htmASP.NETXRefresh alternative solution<p>I am using two monitors for a web development for the past 4 years. Usually I have website that I am working on a left monitor and my Visual Studio on a right monitor. Usually I do change in Visual Studio then press Shift-F6 (in case of code change) or just Ctrl-S (to save). After that I grab a mouse, move cursor to browser on left monitor click and then F5 for refresh. If you do a little CSS tricks it is to much repetitive movements.</p> <p><img alt="Dual Monitor Web Development" src="http://www.karpach.com/images/uploaded/dual-monitor-web-development.png" /></p> <p>About two years ago I discovered <a target="_blank" href="http://xrefresh.binaryage.com/">XRefresh</a>. I mentioned it in my <a href="http://www.karpach.com/asp-net-coding-performance-tips.htm">ASP.NET development tips</a> article. First I was really excited. It is excellent tool for html and CSS markup tweaking. But if you do a lot of code changes and need to recompile your project more often then markup tweaking then it might not be a best tool for you. XRefresh watches files. If file changes then it refreshes browser. Now imagine if you compile big project and compilation takes 15 sec. XRefresh would notice dll change and would try to refresh browser. However compilation is not done yet and some dll-s are locked. So, XRefresh most likely not going to work for Shift-F6 scenario (recompilation of project).</p> <p>So, I found XRefresh alternative solution. I wrote a <b>Visual Studio macros</b>. You can hook up to OnBuildDone event. Then find Firefox process. Move focus to Firefox window and send F5 keystroke to it. Macros below still needs some polishing, but you can get an idea:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cln { color: #2b91af; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">Private</span> <span class="cb1">Sub</span> BuildEvents_OnBuildDone(<span class="cb1">ByVal</span> Scope <span class="cb1">As</span> EnvDTE.vsBuildScope, _ <span class="cb1">ByVal</span> Action <span class="cb1">As</span> EnvDTE.vsBuildAction) <span class="cb1">Handles</span> BuildEvents.OnBuildDone</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> projects <span class="cb1">As</span> <span class="cb1">Object</span>() = DTE.ActiveSolutionProjects</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">If</span> projects.Length &gt; 0 <span class="cb1">Then</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">If</span> <span class="cb1">CType</span>(projects(0), Project).ExtenderNames.Length <span class="cb1">Then</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">If</span> <span class="cb1">CType</span>(projects(0), Project).ExtenderNames(0) = <span class="cb2">&quot;WebApplication&quot;</span> <span class="cb1">Then</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> proc <span class="cb1">As</span> System.Diagnostics.Process() = _ System.Diagnostics.Process.GetProcessesByName(<span class="cb2">&quot;firefox&quot;</span>)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">For</span> i <span class="cb1">As</span> <span class="cb1">Integer</span> = 0 <span class="cb1">To</span> proc.Length - 1</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">If</span> proc(i).MainWindowHandle &lt;&gt; 0 <span class="cb1">Then</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SetForegroundWindow(<span class="cb1">New</span> HandleRef(<span class="cb1">Nothing</span>, proc(i).MainWindowHandle))</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.Windows.Forms.SendKeys.SendWait(<span class="cb2">&quot;{F5}&quot;</span>)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">If</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Next</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">If</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">If</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">If</span></pre> <pre class="cl"><span class="cb1">End</span> <span class="cb1">Sub</span></pre> </div> <p>&nbsp;</p> <p>I am open for discussions and any suggestions.</p> <p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.karpach.com%2fxrefresh-alternative.htm"><img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.karpach.com%2fxrefresh-alternative.htm&amp;border=688299&amp;fgcolor=ffffff&amp;bgcolor=8C3014" /></a></p>Mon, 31 May 2010 00:00:00 -07002010-05-31T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/entityspaces-linq-to-sql-multiple-where.htmhttp://www.karpach.com/entityspaces-linq-to-sql-multiple-where.htmLINQEntitySpaces and LINQ to SQL with multiple where statements<p>At my current workplace we are using EntitySpaces a lot. I feel guilty but sometimes I am too lazy to write stored procedure, so I rely on EntitySpaces dynamic queries. Usually it involves simple where statement, but sometimes there are multiple where statements based on certain application logic condition. Lets look at example based on table of orders as follow:</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <th>Id</th> <th>Name</th> <th>Price</th> <th>Created</th> </tr> <tr> <td>1</td> <td>Viktar</td> <td>10.00</td> <td>2010-01-02</td> </tr> <tr> <td>2</td> <td>Viktar</td> <td>12.00</td> <td>2010-02-02</td> </tr> <tr> <td>3</td> <td>Alex</td> <td>15.00</td> <td>2010-02-02</td> </tr> <tr> <td>4</td> <td>Lucy</td> <td>11.00</td> <td>2010-03-10</td> </tr> </tbody> </table> <p>&nbsp;</p> <p>Let's say you need to filter this table by name and date, but filter values come from different sources. Something like this:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: #2b91af; } .cb2 { color: blue; } .cb3 { color: #a31515; } .cb4 { color: green; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">TestTableQuery</span> testTableQuery = <span class="cb2">new</span> <span class="cb1">TestTableQuery</span>();</pre> <pre class="cl"> testTableQuery.Where(testTableQuery.Name == <span class="cb3">&quot;Viktar&quot;</span>);</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb4">// Some crazy logic here</span></pre> <pre class="cl"><span class="cb4">// ...........&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </span></pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> testTableQuery.Where(testTableQuery.Created &lt; customDate);</pre> </div> <br/> <p>So, what is relation between first and second where statement? Does second one overwrites first one? Apparently relation is T-SQL AND statement, but you can change it to OR clause as follow:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: #2b91af; } .cb2 { color: blue; } .cb3 { color: #a31515; } .cb4 { color: green; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">TestTableQuery</span> testTableQuery = <span class="cb2">new</span> <span class="cb1">TestTableQuery</span>();</pre> <pre class="cl"> testTableQuery.Where(testTableQuery.Name == <span class="cb3">&quot;Viktar&quot;</span>);</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb4">// Some crazy logic here</span></pre> <pre class="cl"><span class="cb4">// ...........&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </span></pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> testTableQuery.es.DefaultConjunction = EntitySpaces.Interfaces.<span class="cb1">esConjunction</span>.Or;</pre> <pre class="cl"> testTableQuery.Where(testTableQuery.Created &lt; customDate);</pre> </div> <br/> <p>I think this is a big advantage that you can do something like this and still get clean T-SQL without nested queries. Here is an output from SQL Server Profiler:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">exec </span>sp_executesql </pre> <pre class="cl"> N<span class="cb2">'SELECT [Id] AS ''Id'',[Name] AS ''Name'',[Price] AS ''Price'',[Created] AS ''Created'' </span> <span class="cb2">FROM [TestTable]<br /> WHERE [Name] = @Name1 OR [Created] &lt; @Created2'</span></pre> <pre class="cl"> ,N<span class="cb2">'@Name1 varchar(6),@Created2 date'</span>,</pre> <pre class="cl"> @Name1=<span class="cb2">'Viktar'</span>,</pre> <pre class="cl"> @Created2=<span class="cb2">'2010-03-23'</span></pre> </div> <br/> <p>Let's try to solve the same task using LINQ to SQL:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } .cb3 { color: #a31515; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">using</span> (<span class="cb2">CoPalletDataContext</span> db = <span class="cb1">new</span> <span class="cb2">CoPalletDataContext</span>())</pre> <pre class="cl"> {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> q = <span class="cb1">from</span> t <span class="cb1">in</span> db.TestTables <span class="cb1">where</span> t.Name == <span class="cb3">&quot;Viktar&quot;</span> <span class="cb1">select</span> t;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; q = q.Where(t =&gt; t.Created &lt; <span class="cb2">DateTime</span>.Now);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; q.ToList();</pre> <pre class="cl"> &nbsp;}</pre> </div> <br/> <p>Code above produces following T-SQL:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">exec </span>sp_executesql N<span class="cb2">'SELECT [t0].[Id], [t0].[Name], [t0].[Price], [t0].[Created]<br /> FROM [dbo].[TestTable] AS [t0]<br /> WHERE ([t0].[Created] &lt; @p0) AND ([t0].[Name] = @p1)'</span>,</pre> <pre class="cl"> N<span class="cb2">'@p0 date,@p1 varchar(6)'</span>,</pre> <pre class="cl"> @p0=<span class="cb2">'2010-03-23'</span>,</pre> <pre class="cl"> @p1=<span class="cb2">'Viktar'</span></pre> </div> <br/> <p>It looks similar to EntitySpaces output. But what if we want OR clause instead of default AND clause? As far as I know it is not possible. So you need to do something like this:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } .cb3 { color: #a31515; } .cb4 { color: green; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">using</span> (<span class="cb2">CoPalletDataContext</span> db = <span class="cb1">new</span> <span class="cb2">CoPalletDataContext</span>())</pre> <pre class="cl"> {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> q = <span class="cb1">from</span> t <span class="cb1">in</span> db.TestTables <span class="cb1">where</span> t.Name == <span class="cb3">&quot;Viktar&quot;</span> || t.Created &lt; <span class="cb2">DateTime</span>.Now <span class="cb1">select</span> t;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (<span class="cb1">true</span>) <span class="cb4">// Some crazy logic here&nbsp;&nbsp;&nbsp; </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; q = <span class="cb1">from</span> t <span class="cb1">in</span> db.TestTables <span class="cb1">where</span> t.Name == <span class="cb3">&quot;Viktar&quot;</span> || t.Created &gt; <span class="cb2">DateTime</span>.Now <span class="cb1">select</span> t;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; q.ToList();</pre> <pre class="cl"> &nbsp;}</pre> </div> <br/> <p>In other words rewrite whole query according to your condition.</p> <p>Dynamic queries are good and save you a lot of time, but it might be challenging for DBA person to optimize it. EntitySpaces queries or LINQ to SQL should be used only for simple queries. If you have more complicated logic put it in stored procedure.</p>Tue, 23 Mar 2010 00:00:00 -07002010-03-23T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/t-sql-rollback-commit-testing.htmhttp://www.karpach.com/t-sql-rollback-commit-testing.htmT-SQLDatabase reckless modifications. ROLLBACK and COMMIT cure.<p>One day you might be forced to do modifications on live database and accidentially delete/update all records in table instead of specified in where statement. It happened to me in a past it might happen to you too. Usually you think that world is over. Then you remember about backup, but still there is a possibility that some data would be lost forever. Always make sure to email your records to yourself with a trusted <a href="http://www.jangomail.com">email service provider</a> for further back up. The best solution is as usual prevention of disaster.</p> <p>There is a reason why <a href="http://www.ssmstoolspack.com/">SSMS Tools pack</a> by default creates for new queries:</p> <p><b style="color: blue;">BEGIN TRAN</b></p> <p>...</p> <p><b style="color: blue;">ROLLBACK</b></p> <p>Use transaction for all your modifications. Inside transaction you can test if your update/delete does exactly what you want it to do.<br /> Here is a little illustration:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">CREATE TABLE </span>TestTable</pre> <pre class="cl"> (</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; Id <span class="cb1">int identity primary key </span>,</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; [Description] <span class="cb1">varchar</span>(<span class="cb1">max</span>)</pre> <pre class="cl"> )</pre> <pre class="cl"> GO</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">BEGIN TRAN</span></pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">insert into </span>TestTable</pre> <pre class="cl"><span class="cb1">values</span></pre> <pre class="cl"> (<span class="cb2">'Test1'</span>),</pre> <pre class="cl"> (<span class="cb2">'Test2'</span>),</pre> <pre class="cl"> (<span class="cb2">'Test3'</span>)</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">select </span>* <span class="cb1">from </span>TestTable <span class="cb3">-- First query, returns all records</span></pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">delete </span>TestTable</pre> <pre class="cl"><span class="cb1">where </span>Id=2</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">select </span>* <span class="cb1">from </span>TestTable <span class="cb3">-- Second query, returns two records</span></pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">ROLLBACK </span><span class="cb3">-- COMMIT</span></pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">select </span>* <span class="cb1">from </span>TestTable <span class="cb3">-- Third query, no records because of ROLLBACK</span></pre> </div> <p>Results:</p> <p>Query 1: <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <td>1</td> <td>Test1</td> </tr> <tr> <td>2</td> <td>Test2</td> </tr> <tr> <td>3</td> <td>Test3</td> </tr> </tbody> </table> </p> <p>&nbsp;</p> <p>Query 2: <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <td>1</td> <td>Test1</td> </tr> <tr> <td>3</td> <td>Test3</td> </tr> </tbody> </table> </p> <p>&nbsp;</p> <p>Query 3:</p> <p>After you are done with testing, just change your ROLLBACK with COMMIT statement.</p> <p>&nbsp;</p>Wed, 17 Feb 2010 00:00:00 -08002010-02-17T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/t-sql-form-generator-template.htmhttp://www.karpach.com/t-sql-form-generator-template.htmT-SQLT-SQL Form Generator Template<p> Most of the time web developer spends creating some kind of forms. This is why almost any object-relational mapper product (EntitySpaces, Subsonic, LINQ to SQL, NHibernate) has some kind of scaffolding generators. Generated pages are might be good for admin side, but for front-end those pages needed to be created manually. Front-end usually has specific design requirements, so there you need to do this tedious task manually over and over again. </p> <p> You can always use <a href="http://msdn.microsoft.com/en-us/library/bb126445.aspx">T4 templates</a> as code generator, but I prefer to use old solid T-SQL. </p> <p> Microsoft SQL Server Management Studio has a lot of pre-build templates for you just press Ctrl+ALT+T to see them (View->Template Explorer). If you are not using them yet, check them out it might be really helpful. <b>Hint: Ctrl+Shift+M brings parameter menu</b>. </p> <p> Let's create simple T-SQL template form generator for ASP.NET: </p> <style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style> <div class="cf"> <p class="cl"><span class="cb1">Declare </span>@width <span class="cb1">varchar</span>(3)</p> <p class="cl"><span class="cb1">set </span>@width = <span class="cb2">'&lt;field_width,int,150&gt;'</span></p> <p class="cl">&nbsp;</p> <p class="cl"><span class="cb1">select </span></p> <p class="cl"><span class="cb2">'&lt;tr&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">&lt;td class=&quot;textfield&quot;&gt;' </span>+ COLUMN_NAME + <span class="cb2">'&lt;/td&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">&lt;td class=&quot;valuefield&quot;&gt;&lt;asp:TextBox ID=&quot;txt' </span>+ COLUMN_NAME + <span class="cb2">'&quot; runat=&quot;server&quot; Width=&quot;' </span>+ @width +<span class="cb2">'&quot;px&quot;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;</span></p> <p class="cl"><span class="cb2">&lt;/tr&gt;'</span></p> <p class="cl"><span class="cb1">from </span>INFORMATION_SCHEMA.COLUMNS <span class="cb1">where </span>TABLE_NAME = <span class="cb2">'&lt;table_name,sysname,Table Name&gt;'</span></p> </div> <br/> <p> <b>&lt;field_width,int,150&gt;</b> - width of input box template parameter<br/> <b>&lt;table_name,sysname,Table Name&gt;</b> - table name template parameter<br/> Press Ctrl+Shift+M. Set above parameters. Switch to "Result to Text" mode. Run and your form HTML is ready. Just copy paste it in your aspx file. </p> <p> This is a good start. But forms are rarely consist of just TextBox-es, so lets modify this a little bit: </p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style> <div class="cf"> <p class="cl"><span class="cb1">select </span></p> <p class="cl"><span class="cb1">case</span></p> <p class="cl"><span class="cb1">when </span>c.CONSTRAINT_TYPE = <span class="cb2">'PRIMARY KEY' </span><span class="cb1">then</span></p> <p class="cl"><span class="cb2">'&lt;tr&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">&lt;td class=&quot;textfield&quot;&gt;' </span>+ cl.COLUMN_NAME + <span class="cb2">':&lt;/td&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">&lt;td class=&quot;valuefield&quot;&gt;&lt;asp:Literal ID=&quot;lit' </span>+ cl.COLUMN_NAME + <span class="cb2">'&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt;</span></p> <p class="cl"><span class="cb2">&lt;/tr&gt;'</span></p> <p class="cl"><span class="cb1">when </span>DATA_TYPE = <span class="cb2">'bit' </span><span class="cb1">then</span></p> <p class="cl"><span class="cb2">'&lt;tr&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">&lt;td class=&quot;textfield&quot;&gt;' </span>+ cl.COLUMN_NAME + <span class="cb2">':&lt;/td&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">&lt;td class=&quot;valuefield&quot;&gt;&lt;asp:CheckBox ID=&quot;chk' </span>+ cl.COLUMN_NAME + <span class="cb2">'&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt;</span></p> <p class="cl"><span class="cb2">&lt;/tr&gt;'</span></p> <p class="cl"><span class="cb1">when </span>DATA_TYPE = <span class="cb2">'varchar' </span><span class="cb1">and </span>CHARACTER_MAXIMUM_LENGTH = -1 <span class="cb1">then </span></p> <p class="cl"><span class="cb2">'&lt;tr&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">&lt;td class=&quot;textfield&quot;&gt;' </span>+ cl.COLUMN_NAME + <span class="cb2">':&lt;/td&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">&lt;td class=&quot;valuefield&quot;&gt;&lt;asp:TextBox ID=&quot;txt' </span>+ cl.COLUMN_NAME + <span class="cb2">'&quot; TextMode=&quot;MultiLine&quot; runat=&quot;server&quot; </span> <span class="cb2">Width=&quot;' </span>+ @width +<span class="cb2">'px&quot;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;</span></p> <p class="cl"><span class="cb2">&lt;/tr&gt;'</span></p> <p class="cl"><span class="cb1">when </span>DATA_TYPE = <span class="cb2">'date' </span><span class="cb1">then</span></p> <p class="cl"><span class="cb2">'&lt;tr&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">&lt;td class=&quot;textfield&quot;&gt;' </span>+ cl.COLUMN_NAME + <span class="cb2">':&lt;/td&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">&lt;td class=&quot;valuefield&quot;&gt;&lt;dxe:ASPxDateEdit ID=&quot;txt' </span>+ cl.COLUMN_NAME + <span class="cb2">'&quot; runat=&quot;server&quot; Width=&quot;' </span>+ @width +<span class="cb2">'px&quot;&gt;&lt;/dxe:ASPxDateEdit&gt;&lt;/td&gt;</span></p> <p class="cl"><span class="cb2">&lt;/tr&gt;'</span></p> <p class="cl"><span class="cb1">else</span></p> <p class="cl"><span class="cb2">'&lt;tr&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">&lt;td class=&quot;textfield&quot;&gt;' </span>+ cl.COLUMN_NAME + <span class="cb2">':&lt;/td&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">&lt;td class=&quot;valuefield&quot;&gt;&lt;asp:TextBox ID=&quot;txt' </span>+ cl.COLUMN_NAME + <span class="cb2">'&quot; runat=&quot;server&quot; </span> <span class="cb2">Width=&quot;' </span>+ @width +<span class="cb2">'px&quot;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;</span></p> <p class="cl"><span class="cb2">&lt;/tr&gt;'</span></p> <p class="cl"><span class="cb1">end</span></p> <p class="cl"><span class="cb1">from </span></p> <p class="cl">INFORMATION_SCHEMA.COLUMNS cl <span class="cb1">LEFT JOIN</span></p> <p class="cl">INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE u <span class="cb1">on </span>cl.COLUMN_NAME = u.COLUMN_NAME <span class="cb1">and </span>cl.TABLE_NAME = u.TABLE_NAME&nbsp; <span class="cb1">LEFT JOIN</span></p> <p class="cl">INFORMATION_SCHEMA.TABLE_CONSTRAINTS c <span class="cb1">on </span>u.CONSTRAINT_NAME = c.CONSTRAINT_NAME <span class="cb1">AND </span>c.TABLE_NAME = cl.TABLE_NAME</p> <p class="cl"><span class="cb1">where </span>cl.TABLE_NAME = <span class="cb2">'&lt;table_name,sysname,Table Name&gt;'</span></p> </div> <br/> <p> I hope this helps. I used some DevExpress controls in my code, which might not be suitable for your. Feel free to modify template according to your needs. </p> <br/> <a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.karpach.com%2ft-sql-form-generator-template.htm"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.karpach.com%2ft-sql-form-generator-template.htm&border=688299&fgcolor=ffffff&bgcolor=8C3014" border="0" alt="kick it on DotNetKicks.com" /></a>Mon, 25 Jan 2010 00:00:00 -08002010-01-25T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/visual-studio-toolbox-icon.htmhttp://www.karpach.com/visual-studio-toolbox-icon.htmASP.NETHow to make Visual Studio toolbox icon have transparent background?<p>During my web developer career I wrote about a dozen of ASP.NET server controls. However transparent background in Visual Studio toolbox was always a mystery for me. Because of that I usually used icons without transparent background. I noticed that Devexpress controls has those kind of icons, so I used reflector to browse their resources and investigated this issue. To make long story short, here are icon with transparent background requirements:</p> <h2>Image Format</h2> <p>If you want transparent background you need to use 16x16 8-bit BMP. You can use other types of images jpeg or png, but transparent background works only with BMP. This is very strange since BMP doesn't have alpha channel, so no build-in transparency.</p> <h2>Transparency Pixel</h2> <p>Select neutral to your icon color and color with it all transparent places. Devexpress usually uses Magenta (#ff00ff) for this purposes. I order to make selected color transparent you need to color <b>bottom left corner pixel of your icon</b> with it.</p>Wed, 20 Jan 2010 00:00:00 -08002010-01-20T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/active-directory-t-sql.htmhttp://www.karpach.com/active-directory-t-sql.htmT-SQLHow to query Active Directory from MS SQL Server?<p>Recently, I am creating intranet portal for a company that I am working for. <a href="http://www.namescape.com">Active directory</a> has a lot of build-in fields for users like first name, last name, email, phone. But it still not enough for a bussiness needs. So, I had to create additional table for extended information in ms sql server database. Now how do you connect extended information and information from active directory?</p> <p>Active directory has <b>objectGuid</b> field. You can use this field to connect your table and active directory information. .NET has two ways to access active directory data using old ODBC method or System.DirectoryServices namespace. However manual connection of table from sql server and active directory information is too much of work. It is much more easier to use linked server feature of MS SQL Server.</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: #a31515; } </style></p> <div class="cf"> <p class="cl">sp_addlinkedserver <span class="cb1">'ADSI'</span>, <span class="cb1">'Active Directory Service Interfaces'</span>, <span class="cb1">'ADSDSOObject'</span>, <span class="cb1">'adsdatasource'</span></p> </div> <p>And then you can query linked server like this:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <p class="cl"><span class="cb1">SELECT </span>* <span class="cb1">FROM OpenQuery</span>(ADSI, <span class="cb2">'SELECT title, displayName, sAMAccountName, givenName, telephoneNumber, facsimileTelephoneNumber, sn FROM ''LDAP://DC=whaever,DC=domain,DC=org'' where objectClass = ''User'''</span>)</p> </div> <p>&nbsp;</p> <p>You might see following error:<br /> <br /> <b style="color: red;">The OLE DB provider &quot;ADSDSOObject&quot; for linked server &quot;ADSI&quot; reported an error. The provider indicates that the user did not have the permission to perform the operation.</b></p> <p>Then you need to go to properties of newly created linked server and adjust security settings (Server Objects &gt; Linked Servers &gt; ADSI).</p>Mon, 11 Jan 2010 00:00:00 -08002010-01-11T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/iphone-problems.htmhttp://www.karpach.com/iphone-problems.htmIPhoneiPhone problems and solutions<p>This is my first post about IPhone. First of all, I am a PC guy and I don't like Apple products in general. Half year ago my company offered me a choice of iPhone or Blackberry. I picked iPhone, so since then I am not so anti Apple as I used to be. I still never going to buy Mac, but I have Leopard OS installed on one of my virtual machines.</p> <p>So, out of the box iPhone is great device. It has a lot of applications, but ...</p> <h2>Problem #1</h2> <p>There are too many IPhones around and all of them have the same ringtone for incomming mail and text message. It quickly becomes very annoying. However there is no way to change it without jailbraking IPhone.</p> <h2>Problem #2</h2> <p>There are no voice dial in IPhone 3G. You can install app such as <a href="http://www.voicebox.com/products/iphone.php">Voice Dialer Plus</a>, but you can't train those apps and unfortunatly they don't understand my accent or russian names in my contacts. <a href="http://www.creaceed.com/vocalia/">Vocalia</a> is a little better app, since you can add pronentiazations for your contacts. The only other solution is to buy IPhone 3GS. But may be it's time to switch to Google Nexus One instead?</p> <h2>Problem #3</h2> <p>You can develop applications only using Mac and you can make it oficially available only thru App store, but Apple does screen new applications. You can use VMWare virtual machine to run Leopard, however it's pretty complecated and might be even not possible with your hardware.</p> <h2>Solutions:</h2> <p>First of all you need to jailbrake your iphone, keep in mind this is going to void your warranty. Of course you can always backup your iPhone and in case of malfuntioning restore it and then bring to apple store, but this might not always work. You can read more about jailbrake on <a href="http://blog.iphone-dev.org/">Dev Team Blog</a>. Then download <a href="http://winscp.net/eng/download.php">WinSCP</a>. Using WinSCP you can access your iPhone over ssh. Make sure that you <a href="http://modmyi.com/forums/native-iphone-ipod-touch-app-discussion/259661-change-password-openssh-alpine-iphone3g-2-0-a.html">change default password (alpine)</a>.&nbsp;</p> <p>Note: Starting from July 26, 2010 it is <strong>legal</strong> to jailbrake iPhone. DMCA has exemptions in this regards. See <a target="_blank" href="http://www.afterdawn.com/news/article.cfm/2010/07/26/iphone_jailbreaking_and_android_rooting_get_dmca_exemption">iPhone jailbreaking and Android rooting get DMCA exemption</a>&nbsp;for more info.</p> <p>&nbsp;</p> <h2>IPhone folders:</h2> <p>Here is a list of IPhone folders that I found to be useful:</p> <p><b>/private/var/stash/Applications.something</b><br /> Native IPhone applications, such as Map, SMS, Calendar and etc. Those apps can't be uninstalled, but can be manually deleted using ssh :-).</p> <p><b>/private/var/mobile/Applications.something</b><br /> Apps from apple store.</p> <p><b>/private/var/stash/Ringtones.something</b><br /> Your ringtones. There is a way to upload <a href="http://www.demogeek.com/2009/07/31/how-to-add-custom-ringtones-to-your-iphone/">ringtones without jailbraking of IPhone</a>.</p> <p><b>/System/Library/Audio/UISounds</b><br /> Use this folder to solve Problem #1.</p>Wed, 06 Jan 2010 00:00:00 -08002010-01-06T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/settimeout-pass-parameter.htmhttp://www.karpach.com/settimeout-pass-parameter.htmJavascriptHow to pass parameter to setTimeOut javascript function?<p>You can always pass parameter to setTimeOut in an ugly way:</p> <p> <p style="margin: 0px;"><span style="color: blue;">function</span> ShowValue(a)</p> <p style="margin: 0px;">{</p> <p style="margin: 0px;">&nbsp;&nbsp;&nbsp; alert(a);</p> <p style="margin: 0px;">}&nbsp;&nbsp;&nbsp; </p> <p style="margin: 0px;">window.setTimeout("ShowValue(" + i + ");", 1000);</p> </p> <br/> <p> However this is not going to work if you want to pass an object as parameter. Here is where javascript closures become handy: </p> <p style="margin: 0px;">var i = 10;</p> <p style="margin: 0px;">window.setTimeout(<span style="color: blue;">function</span>()</p> <p style="margin: 0px;">{</p> <p style="margin: 0px;">&nbsp;&nbsp;&nbsp; ShowValue(i);</p> <p style="margin: 0px;">}, 1000);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </p> <br/> <p> Where i can be any kind of object. So, what is javascript closures? </p> <p> Javascript closure is a mechanism of locking local variable in a scope of newly declared function. </p> <p style="margin: 0px;">i = 10;</p> <p style="margin: 0px;">window.setTimeout(<span style="color: blue;">function</span>()</p> <p style="margin: 0px;">{</p> <p style="margin: 0px;">&nbsp;&nbsp;&nbsp; ShowValue(i);</p> <p style="margin: 0px;">}, 1000);</p> <p style="margin: 0px;">&nbsp;</p> <p style="margin: 0px;">i = 20;</p> <br/> <p> In example above, ShowValue(i) whould produce alert message with value 20. Why? Logically it should be 10, since assignment was done after function declaration. Actually, this a right behaivour. Declared function uses a pointer to a memory of local variable. So, first i gets value 10, then it gets value 20 and only after that alert displays value of i using a memory pointer, which points to 20. </p> <p> You may say that it looks like a global variable of some kind. Actually this is not a global variable. We can prove this using this example: </p> <p style="margin: 0px;">$(document).ready(<span style="color: blue;">function</span>()</p> <p style="margin: 0px;">{</p> <p style="margin: 0px;">&nbsp;&nbsp;&nbsp; ShowPopup();</p> <p style="margin: 0px;">&nbsp;&nbsp;&nbsp; i = 30</p> <p style="margin: 0px;">});</p> <p style="margin: 0px;"><span style="color: blue;">function</span> ShowPopup()</p> <p style="margin: 0px;">{</p> <p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> i = 10;</p> <p style="margin: 0px;">&nbsp;&nbsp;&nbsp; window.setTimeout(<span style="color: blue;">function</span>()</p> <p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p> <p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ShowValue(i); </p> <p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }, 1000);</p> <p style="margin: 0px;">&nbsp;&nbsp;&nbsp; i = 20;</p> <p style="margin: 0px;">}</p> <p style="margin: 0px;"><span style="color: blue;">function</span> ShowValue(a)</p> <p style="margin: 0px;">{</p> <p style="margin: 0px;">&nbsp;&nbsp;&nbsp; alert(a);</p> <p style="margin: 0px;">}</p> <br/> <p> Above I am using jquery ready function to execute ShowPopup function. If i is a global variable then alert should show 30. But alert still displays 20. I hope after those little examples it becomes clear what is javascript closure. </p>Thu, 24 Dec 2009 00:00:00 -08002009-12-24T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/t-sql-comma-separated-lists.htmhttp://www.karpach.com/t-sql-comma-separated-lists.htmASP.NETHow to create comma separated list in T-SQL?<p>Twice this month I needed comma separated lists build in T-SQL, so I decided to blog about this. You can use short comma separated lists for AJAX details lists. For example, you have list of users and you want to show quick details about selected user orders.</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <th>CustomerId</th> <th>FirstName</th> <th>LastName</th> </tr> <tr> <td>1</td> <td>Viktar</td> <td>Karpach</td> </tr> <tr> <td>2</td> <td>Joe</td> <td>Doe</td> </tr> <tr> <td>3</td> <td>Jane</td> <td>Forest</td> </tr> </tbody> </table> <p>&nbsp;</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <th>OrderId</th> <th>CustomerId</th> <th>Amount</th> <th>OderDate</th> </tr> <tr> <td>1</td> <td>1</td> <td>119.12</td> <td>10/12/2009</td> </tr> <tr> <td>2</td> <td>1</td> <td>144.98</td> <td>1/12/2009</td> </tr> <tr> <td>3</td> <td>1</td> <td>76.12</td> <td>8/5/2009</td> </tr> <tr> <td>4</td> <td>2</td> <td>14.14</td> <td>9/9/2009</td> </tr> <tr> <td>5</td> <td>3</td> <td>17.89</td> <td>10/10/2009</td> </tr> </tbody> </table> <p>Following T-SQL code would build list of customers and corresponding orders:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <p class="cl"><span class="cb1">SELECT </span>&nbsp; c.FirstName + <span class="cb2">' ' </span>+c.LastName <span class="cb1">as </span>[Customer Name],</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; [Order Ids] = <span class="cb1">substring</span>((<span class="cb1">SELECT </span>( <span class="cb2">',' </span>+ <span class="cb1">cast</span>(OrderId <span class="cb1">as varchar</span>))</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span class="cb1">FROM </span>[Order] ord</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">WHERE </span>(ord.CustomerId = c.CustomerId)</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">ORDER BY </span>ord.OrderId</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span class="cb1">FOR XML </span>PATH( <span class="cb2">'' </span>)</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; ), 2, 1000 )</p> <p class="cl"><span class="cb1">FROM </span>&nbsp;&nbsp;&nbsp; Customer c</p> </div> <p>Result:</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <td>Viktar Karpach</td> <td>1,2,3</td> </tr> <tr> <td>Joe Doe</td> <td>4</td> </tr> <tr> <td>Jane Forest</td> <td>4</td> </tr> </tbody> </table> <p>Usually simple ids list is not sufficient enough, what if you need some more information.</p> <p>Let's pass Amount column as well:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <p class="cl"><span class="cb1">SELECT </span>&nbsp; c.FirstName + <span class="cb2">' ' </span>+c.LastName <span class="cb1">as </span>[Customer Name],</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; [Order Ids] = <span class="cb1">substring</span>((<span class="cb1">SELECT </span>( <span class="cb2">',' </span>+ <span class="cb1">cast</span>(OrderId <span class="cb1">as varchar</span>) + <span class="cb2">':' </span>+ <span class="cb1">cast</span>(Amount <span class="cb1">as varchar</span>))</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span class="cb1">FROM </span>[Order] ord</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">WHERE </span>(ord.CustomerId = c.CustomerId)</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">ORDER BY </span>ord.OrderId</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span class="cb1">FOR XML </span>PATH( <span class="cb2">'' </span>)</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; ), 2, 1000 )</p> <p class="cl"><span class="cb1">FROM </span>&nbsp;&nbsp;&nbsp; Customer c</p> </div> <p>Result:</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <td>Viktar Karpach</td> <td>1:119.12,2:144.98,3:76.12</td> </tr> <tr> <td>Joe Doe</td> <td>4:14.14</td> </tr> <tr> <td>Jane Forest</td> <td>5:17.89</td> </tr> </tbody> </table> <p>You can use JQuery to easily parse those results.</p>Mon, 09 Nov 2009 00:00:00 -08002009-11-09T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/silverlight-service-references-clientconfig-empty.htmhttp://www.karpach.com/silverlight-service-references-clientconfig-empty.htmSilverlightServiceReferences.ClientConfig is empty<p>Currently I am working on some Silverlight 3 involved projects. As you probably know Silverlight uses WCF web services for data retrieval.</p> <p>So, as usual I created WCF service in Silverlight hosting application, compiled and added service reference to Silverlight project. Usually wizard creates ServiceReferences.ClientConfig and populates it with bindings settings. However it didn't happen this time. ServiceReferences.ClientConfig was empty. I tried different things, created project from scratch. Tried vb.net, c#. Nothing helped, ServiceReferences.ClientConfig was empty all the time. I remember that it worked this way before. Recently I did some WPF coding with some custom TCP/IP based bindings. Apparently, it some how affected default settings for WCF services. So, solution was very simple:</p> <p>In your Silverlight hosting application web.config replace default binding <b>binding=&quot;wsHttpBinding&quot;</b> with <b>binding=&quot;basicHttpBinding&quot;</b>.</p>Thu, 29 Oct 2009 00:00:00 -07002009-10-29T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/IE6-CSS-margin-bug.htmhttp://www.karpach.com/IE6-CSS-margin-bug.htmCSSIE6 CSS floating element margin bug<p>Recently, I was doing my blog redesign and I faced strange IE6 behavior. Search box floating div didn't want to float all a way to a right. I tried to tweak child and parent margins and finally I solved this mystery. However, I did not understand a root of a problem. Today, I was reading <a target="_blank" href="http://www.smashingmagazine.com/2009/10/14/css-differences-in-internet-explorer-6-7-and-8/">Smashing Magazine article about IE6, IE7, IE8 css</a> differences and I found it.</p> <p><b>Left and right margins are doubled on floated elements that touch their parents' side edges.</b> What a surprise! This a real bug of IE6 and I didn't know about it. I knew about min-width/min-height problem, attribute selectors and other IE6 bugs, but this one I didn't know. Probably I faced it before, but never paid attention to it. So, if you face strange positioning problem of floating elements in IE6, know this is well known IE6 bug. Below is a simple illustration of the problem. Right div should have 30px right margin. In IE6 it shows up as 60px.</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: #a31515; } .cb2 { color: red; } .cb3 { color: blue; } </style></p> <div class="cf"> <p class="cl"><span class="cb1">.left</span></p> <p class="cl">{</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">width</span>:<span class="cb3">100px</span>;</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">height</span>:<span class="cb3">100px</span>;</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">background-color</span>:<span class="cb3">aqua</span>;</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">float</span>:<span class="cb3">left</span>;</p> <p class="cl">}</p> <p class="cl"><span class="cb1">.right</span></p> <p class="cl">{</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">width</span>:<span class="cb3">100px</span>;</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">height</span>:<span class="cb3">100px</span>;</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">background-color</span>:<span class="cb3">aqua</span>;</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">float</span>:<span class="cb3">right</span>;</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">margin-right</span>:<span class="cb3">30px</span>;</p> <p class="cl">}</p> <p class="cl"><span class="cb1">#parent</span></p> <p class="cl">{</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">overflow</span>:<span class="cb3">hidden</span>;</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">width</span>:<span class="cb3">300px</span>;</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">border</span>:<span class="cb3">solid</span> <span class="cb3">1px</span> <span class="cb3">black</span>;</p> <p class="cl">}</p> </div> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } .cb3 { color: red; } </style></p> <div class="cf"> <p class="cl"><span class="cb1">&lt;</span><span class="cb2">div</span> <span class="cb3">id</span><span class="cb1">=&quot;parent&quot;&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">&lt;</span><span class="cb2">div</span> <span class="cb3">class</span><span class="cb1">=&quot;left&quot;&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">&lt;/</span><span class="cb2">div</span><span class="cb1">&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">&lt;</span><span class="cb2">div</span> <span class="cb3">class</span><span class="cb1">=&quot;right&quot;&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">&lt;/</span><span class="cb2">div</span><span class="cb1">&gt;</span></p> <p class="cl"><span class="cb1">&lt;/</span><span class="cb2">div</span><span class="cb1">&gt;</span></p> <p class="cl">&nbsp;</p> <p class="cl">&nbsp;</p> </div> <p><img alt="IE6 double margin bug" src="http://www.karpach.com/images/uploaded/IE6-CSS-Bug.png" /></p>Wed, 14 Oct 2009 00:00:00 -07002009-10-14T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/asp-net-coding-performance-tips.htmhttp://www.karpach.com/asp-net-coding-performance-tips.htmASP.NETHow to speed up ASP.NET web development?<p>Almost every ASP.NET developer heard or used <a href="http://www.jetbrains.com/resharper/">ReSharper Visual Studio Addon</a> or <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">CodeRush from Devexpress</a>. Those tools claim to speed up coding process up to 25%. However almost nobody realizes that most of the time asp.net web developer just waiting for project to compile. About a year ago I had to work on a big product. Compilation of whole solution usually took about 2-3 minutes. I felt like most of my work day I was just waiting for a project to compile. So, I came up with the following techniques.</p> <h3>1. &quot;F5 Start Debugging&quot; evil.</h3> <p>Never, never hit F5. This a slowest thing that you can do. First of all if it is possible use localhost IIS instead of Visual Studio built-in http server (aka Cassini). Better to test your project in a real environment. During my carrier I had a lot of situations when project worked in Cassini but didn't work in IIS and vice versa.</p> <p>Compile your solution and open it in a browser. You don't need to hit F5. Just type http://localhost/YourApplication/Default.aspx and your test environment should be ready. You can make changes to your project. For codebehind changes you need to recompile project and then hit refresh in browser. For aspx changes, just save file and then hit refresh in browser.</p> <p>For built-in web server you need to hit F5. It is a necessary step that can't be avoided, but do it just one time. Again another point for localhost IIS. Copy url something like http://localhost:1234/YourApplication/Default.aspx and paste it in new browser window. You can stop debugging now. You should still have one browser window open with your project in it. Cassini is still running, so you can do the same update process like for IIS. Below is a little screencast video of what I mentioned above. As an example I took my <a href="http://www.karpach.com/ColorPickerDemo.aspx">ColorPicker Control</a> project.</p> <object width="640" height="498"> <param name="movie" value="http://content.screencast.com/users/Viktar/folders/karpach.com/media/7769884b-8ec0-4d00-9986-dfed6bed0532/Keep-browser-open_controller.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#FFFFFF" /> <param name="flashVars" value="thumb=http://content.screencast.com/users/Viktar/folders/karpach.com/media/7769884b-8ec0-4d00-9986-dfed6bed0532/FirstFrame.png&amp;containerwidth=640&amp;containerheight=498&amp;showstartscreen=true&amp;showendscreen=true&amp;loop=false&amp;autostart=false&amp;color=D5E6E6,D5E6E6&amp;thumbscale=45&amp;content=http://content.screencast.com/users/Viktar/folders/karpach.com/media/7769884b-8ec0-4d00-9986-dfed6bed0532/Keep-browser-open.mp4" /> <param name="allowFullScreen" value="true" /> <param name="scale" value="showall" /> <param name="allowScriptAccess" value="always" /> <param name="base" value="http://content.screencast.com/users/Viktar/folders/karpach.com/media/7769884b-8ec0-4d00-9986-dfed6bed0532/" /> <embed width="640" height="498" src="http://content.screencast.com/users/Viktar/folders/karpach.com/media/7769884b-8ec0-4d00-9986-dfed6bed0532/Keep-browser-open_controller.swf" quality="high" bgcolor="#FFFFFF" type="application/x-shockwave-flash" allowscriptaccess="always" flashvars="thumb=http://content.screencast.com/users/Viktar/folders/karpach.com/media/7769884b-8ec0-4d00-9986-dfed6bed0532/FirstFrame.png&amp;containerwidth=640&amp;containerheight=498&amp;showstartscreen=true&amp;showendscreen=true&amp;loop=false&amp;autostart=false&amp;color=D5E6E6,D5E6E6&amp;thumbscale=45&amp;content=http://content.screencast.com/users/Viktar/folders/karpach.com/media/7769884b-8ec0-4d00-9986-dfed6bed0532/Keep-browser-open.mp4" allowfullscreen="true" base="http://content.screencast.com/users/Viktar/folders/karpach.com/media/7769884b-8ec0-4d00-9986-dfed6bed0532/" scale="showall"></embed> </object> <p>Use following shortcuts: Ctrl-S (Save), Shift-F6 (Build Current Project), F6 (Build Solution). Very rarely build whole solution. Usually it is enough to build current project that you changed. If you need to debug application, use attach to process command. You can use following macros to automate this process:</p> <p>Cassini:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">Public</span> <span class="cb1">Sub</span> AttachToFirstDevWebServer()</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> process <span class="cb1">As</span> EnvDTE.Process</p> <p class="cl">&nbsp;</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">For</span> <span class="cb1">Each</span> process <span class="cb1">In</span> DTE.Debugger.LocalProcesses</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">If</span> (Path.GetFileName(process.Name).ToLower() = <span class="cb2">&quot;webdev.webserver.exe&quot;</span>) <span class="cb1">Then</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; process.Attach()</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Exit</span> <span class="cb1">Sub</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">If</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Next</span></p> <p class="cl">&nbsp;</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; MsgBox(<span class="cb2">&quot;No ASP.NET Development Server found&quot;</span>)</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">Sub</span></p> </div> <p>IIS:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">Sub</span> AttachDebugger()</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Try</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> dbg2 <span class="cb1">As</span> EnvDTE80.Debugger2 = DTE.Debugger</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> trans <span class="cb1">As</span> EnvDTE80.Transport = dbg2.Transports.Item(<span class="cb2">&quot;Default&quot;</span>)</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> dbgeng(1) <span class="cb1">As</span> EnvDTE80.Engine</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dbgeng(0) = trans.Engines.Item(<span class="cb2">&quot;Managed&quot;</span>)</p> <p class="cl">&nbsp;</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> proc2 <span class="cb1">As</span> EnvDTE80.Process2</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; proc2 = dbg2.GetProcesses(trans, <span class="cb2">&quot;&quot;</span>).Item(<span class="cb2">&quot;w3wp.exe&quot;</span>)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; proc2.Attach2(dbgeng)</p> <p class="cl">&nbsp;</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Catch</span> ex <span class="cb1">As</span> System.Exception</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; MsgBox(ex.Message)</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">Try</span></p> <p class="cl">&nbsp;</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">Sub</span></p> </div> <h3>2. Multiple projects application structure.</h3> <p>Everybody heard about DAL and multiple tiers of application. However usually developers do not break application tiers into different projects. This is a really useful technique. Why do you need to recompile business logic layer for a small UI change? UI tier should be in its own project, so you can recompile only UI level and nothing else. Again just press Shift-F6.</p> <h3>4. Virtual hard drive.</h3> <p>Visual Studio is very chatty with hard drive, especially during compilation. If you are using laptop as your primary development machine then it probably has 5400 rpm hard drive. Microsoft suggests to use with Visual Studio 7200 rpm hard drives or even 10000 rpm hard drives. So, you might experience slowness. <a href="http://www.farstone.com/software/virtual-hard-drive.htm">Virtual hard drive</a> might help you. Copy you project to Virtual Hard Drive. Modify environment variables Temp and Tmp, so they point to Virtual Hard Drive as well. By using this technique I reduced my compilation time from 3 minutes to 45 seconds.</p> <h3>3. Firefox addons.</h3> <p>Use Firefox as you primary test browser. It has a lot of useful addons, which might help you with web development. Lately IE8 is catching up with Firefox, but it is still far behind.<br /> 1. <a href="http://getfirebug.com/" target="_blank">Firebug</a><br /> I can't imagine my life without firebug. You can do a lot of editing straight in firebug with live preview. Just copy paste a css code when you done tweaking it.<br /> Take a look:</p> <object width="624" height="542"> <param name="movie" value="http://content.screencast.com/users/Viktar/folders/karpach.com/media/a0d9a6ed-fc4e-447f-a19a-8d6b020b885b/Firebug%20Demo_controller.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#FFFFFF" /> <param name="flashVars" value="thumb=http://content.screencast.com/users/Viktar/folders/karpach.com/media/a0d9a6ed-fc4e-447f-a19a-8d6b020b885b/FirstFrame.png&amp;containerwidth=624&amp;containerheight=542&amp;showstartscreen=true&amp;showendscreen=true&amp;loop=false&amp;autostart=false&amp;color=D5E6E6,D5E6E6&amp;thumb=FirstFrame.png&amp;thumbscale=45&amp;content=http://content.screencast.com/users/Viktar/folders/karpach.com/media/a0d9a6ed-fc4e-447f-a19a-8d6b020b885b/Firebug%20Demo.mp4" /> <param name="allowFullScreen" value="true" /> <param name="scale" value="showall" /> <param name="allowScriptAccess" value="always" /> <param name="base" value="http://content.screencast.com/users/Viktar/folders/karpach.com/media/a0d9a6ed-fc4e-447f-a19a-8d6b020b885b/" /> <embed width="624" height="542" src="http://content.screencast.com/users/Viktar/folders/karpach.com/media/a0d9a6ed-fc4e-447f-a19a-8d6b020b885b/Firebug%20Demo_controller.swf" quality="high" bgcolor="#FFFFFF" type="application/x-shockwave-flash" allowscriptaccess="always" flashvars="thumb=http://content.screencast.com/users/Viktar/folders/karpach.com/media/a0d9a6ed-fc4e-447f-a19a-8d6b020b885b/FirstFrame.png&amp;containerwidth=624&amp;containerheight=542&amp;showstartscreen=true&amp;showendscreen=true&amp;loop=false&amp;autostart=false&amp;color=D5E6E6,D5E6E6&amp;thumb=FirstFrame.png&amp;thumbscale=45&amp;content=http://content.screencast.com/users/Viktar/folders/karpach.com/media/a0d9a6ed-fc4e-447f-a19a-8d6b020b885b/Firebug%20Demo.mp4" allowfullscreen="true" base="http://content.screencast.com/users/Viktar/folders/karpach.com/media/a0d9a6ed-fc4e-447f-a19a-8d6b020b885b/" scale="showall"></embed> </object> <p>2. <a href="https://addons.mozilla.org/en-US/firefox/addon/60" target="_blank">Web Developer addon</a><br /> Has a lot of features, but I meanly use: clear cache, find broken images, resize window, view generated code, show passwords, display ruler, display guides.</p> <p>3. <a href="http://www.colorzilla.com/firefox/" target="_blank">ColorZilla</a> Need quickly grab a color from web site? Want to rip off gradient? ColorZilla is your tool.</p> <p>4. <a href="http://www.pixelperfectplugin.com/" target="_blank">PixelPerfect</a> If you are working closely with designers then you need this tool in order to make a perfect match of design and HTML.</p> <p>5. <a href="http://xrefresh.binaryage.com/" target="_blank">XRefresh</a> If you have two monitors this is absolutely need to have tool. It automatically refresh browser when you do change to a source code. It also has IE support.</p> <p>6. <a href="https://addons.mozilla.org/en-US/firefox/addon/1419" target="_blank">IE Tab</a> You need this addon if you need to test your web site in IE. Single click is going to switch rendering engines. You can set certain sites always use IE rendering engine, for example MOSS intranet sites.</p> <p>7. <a href="http://www.karpach.com/yslow-and-asp-net-100-points-a-grade.htm">YSlow</a> You need this addon in order to optimize client side performance of your web site.</p> <p>8. <a href="https://addons.mozilla.org/en-US/firefox/addon/5648" target="_blank">FireShot</a> Screen shot tool with anotations.</p> <p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.karpach.com%2fasp-net-coding-performance-tips.htm"><img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.karpach.com%2fasp-net-coding-performance-tips.htm&amp;border=688299&amp;fgcolor=ffffff&amp;bgcolor=8C3014" /></a></p>Sun, 23 Aug 2009 00:00:00 -07002009-08-23T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/background-image-css-macros.htmhttp://www.karpach.com/background-image-css-macros.htmASP.NETHow to create image dimension macros in Visual Studio?<p>I hate to do repetitive tasks. When I am writing css very often I need to set background image to some container. But I never know image dimensions. So usually I am going to explorer or photoshop in order to find image dimensions and then manually type css width and height for it. Lucky me Visual Studio has macros. Here is simple guide how to create context menu &quot;Paste Image CSS&quot; command.</p> <ol> <li>Launch Macros IDE</li> <li>In the Macros IDE, you should see a default Macro project &ldquo;MyMacros&rdquo;. Add a new Module to the MyMacros project by Right clicking on the project and then choosing Add | Add Module.</li> <li>Now copy paste macro code provided below.</li> <li>Now here is a little trick. From the Visual Studio IDE, Choose Tools | Customize. In the Customize window, check the &quot;Context Menus&quot; from the Toolbars area. If you done it correctly, you should see a new toolbar at top which have most of the Contextual Menus you see while working in the VS IDE. In Project and Solution Context Menu find Item submenu. While Item submenu is open, go to the Commands tab of the Customize Window and click the Macros. Now find and drag your Macro (PasteImageCSS) and drop it over the desired point in the Context Menu.</li> </ol> <p>&nbsp;</p> <p><img alt="Insert Image CSS Macros Demo" src="http://www.karpach.com/images/uploaded/Macros-Demo.gif" /> <style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">Sub</span> PasteImageCSS()</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> win <span class="cb1">As</span> Window = DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer)</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> solution <span class="cb1">As</span> UIHierarchy = win.Object</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> file <span class="cb1">As</span> ProjectItem = solution.SelectedItems(0).Object</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> editPoint <span class="cb1">As</span> EditPoint = DTE.ActiveDocument.Selection.ActivePoint.CreateEditPoint</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Try</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> objImage <span class="cb1">As</span> System.Drawing.Image = System.Drawing.Image.FromFile(file.FileNames(0))</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; editPoint.Insert(<span class="cb1">String</span>.Format( _</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">&quot;background-image:url(images/{2});{0}&quot;</span> &amp; _</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">&quot;{1}background-repeat:no-repeat;{0}&quot;</span> &amp; _</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">&quot;{1}width:{3}px;{0}&quot;</span> &amp; _</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">&quot;{1}height:{4}px;{0}&quot;</span> &amp; _</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">&quot;{1}display:block;&quot;</span> _</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; , ControlChars.NewLine, ControlChars.Tab, file.Name, objImage.Width, objImage.Height))</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; objImage.Dispose()</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Catch</span> ex <span class="cb1">As</span> Exception</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; MsgBox(<span class="cb2">&quot;Selected file is not an image.&quot;</span>)</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">Try</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">Sub</span></p> </div>Thu, 30 Jul 2009 00:00:00 -07002009-07-30T00:00:00-07:00Copyright (c) 2007 Viktar Karpach