tag:blogger.com,1999:blog-69790725785169490472024-03-13T23:14:08.540-06:00That One Developer BlogThatOneDeveloper.Document(crapIRunIntoWhileCoding).Return(BlogPostsSoIDoNotRunIntoItAgain);That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.comBlogger41125tag:blogger.com,1999:blog-6979072578516949047.post-25987599699342798152020-02-19T08:53:00.003-07:002020-02-19T11:24:05.566-07:00Access ASP.NET website hosted in IIS Express from another machine on the local network (VS 2019)I needed to test Mac Safari against a website I was developing on a Windows machine. Instead of being required to publish for every change, I wanted to just expose IIS Express across the LAN and hit the website that way.
In order to do so, I had to do the following:
<br />
<br />
<ol>
<li>Run Visual Studio 2019 as Administrator</li>
<li>Edit {ProjectRoot}/.vs/{WebProjectName}/config/applicationhost.config</li>
<li>In the <sites> section, add a binding under the website project's <site> definition (see below):</li>
<li>Allow the desired port through your firewall</li>
</ol>
<div style="line-height: 19px;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: x-small;"><site name="FocusAndExecute" id="1"><br /> <application path="/" applicationPool="Clr4IntegratedAppPool"><br /> <virtualDirectory path="/" physicalPath="{path_to_site}" /><br /> </application><br /> <bindings><br /> <binding protocol="http" bindingInformation="*:51427:localhost" /><br /> <b><binding protocol="http" bindingInformation="<span style="background-color: yellow;">*:51428:192.168.1.123</span>" /></b><br /> </bindings><br /></site></span><br />
<br />
After doing this and launching IIS Express from Visual Studio, I was able to hit the site from the Mac by going to:</div>
<div style="line-height: 19px;">
http://192.168.1.123:51428</div>
<div style="line-height: 19px;">
<br /></div>
<div style="line-height: 19px;">
<b>NOTE: You may be able to put *:51428:* instead of specifying the IP, so you can hit it by host name, etc. but I didn't play with that yet. Feel free to leave a comment if you have a better way!</b></div>
That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-45919497655915878072019-11-04T14:14:00.001-07:002019-11-04T14:14:55.456-07:00[SOLVED] Visual Studio 2019 keeps rebuilding my ASP.NET Core project when I refresh my browserA couple of weeks ago, I started noticing that Visual Studio would rebuild my code whenever I would force a refresh in my browser, even if I had just rebuilt the solution in Visual Studio before executing said refresh. Sometimes, Visual Studio would rebuild more than once during this process, and eventually would end up with a "Cannot copy the **** files because it is being used by another process" (which I imagine was the dotnet process handling the request.<br />
<br />
I assume the multiple builds were because I was reloading a page that also turned around and made AJAX calls back to the same site.<br />
<br />
It got beyond super annoying, and I searched the internet and dug through settings and could not find a workaround, until today.<br />
<br />
Under <b>"Tools" | "Options" | "Projects and Solutions" | "ASP.NET Core"</b> you will find an option under the <b>"General" </b>category called <b>"<u>Auto restart Kestrel server after build</u>"</b>. It was set to "True" in my case, and I set it to "False" and it stopped the constant rebuilding for me.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://1.bp.blogspot.com/-LXlo3Uxga-k/XcCUhj-eYvI/AAAAAAAABT8/vmtxZmwrpeQkfhpHJ2hno01h4oMBNI8BwCLcBGAsYHQ/s1600/vs2019_auto_rebuild_option.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="302" data-original-width="519" src="https://1.bp.blogspot.com/-LXlo3Uxga-k/XcCUhj-eYvI/AAAAAAAABT8/vmtxZmwrpeQkfhpHJ2hno01h4oMBNI8BwCLcBGAsYHQ/s1600/vs2019_auto_rebuild_option.png" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Visual Studio 2019<br />Tools | Options | Projects and Solutions | ASP.NET Core</td></tr>
</tbody></table>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
Notice the description of that option states:<br />
"<i>Automatically build modified ASP.NET Core projects hosted in IIS Express when a browser request is received.</i>"<br />
<br />
Unfortunately this was happening to me even after I explicitly did a full rebuild in Visual Studio, then switched to my browser to refresh the page. So I have no idea why VS thought it was "modified" but it wasn't. I don't have time to track down the cause of this right now, so I consider this really more of a workaround than a solution, but I hope it will help someone else avoid this madness.<br />
<br />
I would also like to know when this option was introduced... has it been there this whole time and always defaulted to "True" and I just happened to not be bit by it until a couple of weeks ago? Perhaps the detection mechanism for determining an ASP.NET Core project was "modified" has been broken in a recent Visual Studio release? (I'm currently on 16.3.7, but it was happening to me on at 16.3.6 as well.)<br />
<br />
I'd love to hear from you if you have encountered the same thing and found a better explanation and/or solution.That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-36401360045864246142018-12-30T00:55:00.001-07:002018-12-30T00:55:55.559-07:00[SOLVED] Using Bootstrap-Select and Vue togetherI ran into some trouble when trying to use <a href="https://vuejs.org/">Vue</a> bindings and <a href="https://developer.snapappointments.com/bootstrap-select/">Bootstrap-Select</a> together that kept the visual controls from updating when options were changed via a Vue model update. <br />
<br />
I wrote up an article on my <a href="https://javascriptjedi.com/vue/solved_vue_with_bootstrap-select.html">JavaScriptJedi.com</a> website with both broken and working examples and an explanation about what happens.<br />
<br />
The quick answers for making sure bootstrap-select works with Vue:<br />
<br />
<ul>
<li>Prevent bootstrap-select from automatically hooking itself up to your controls by avoiding using the "selectpicker" class on the <select> element. Use a different class like "select-picker" instead that you can manually hook up the selectpicker after your Vue model is finished mounting.</li>
<ul>
<li><i>(Use Vue's <a href="https://vuejs.org/v2/api/#vm-nextTick">$nextTick</a> function inside your "mounted" handler function to ensure all components/child-components are mounted as well before setting up bootstrap-select)</i></li>
</ul>
<li>Only use the "title" HTML attribute on your <select> when you don't have a default selected value on your <select></li>
<li><b>ALWAYS call bootstrap-select's "<a href="https://developer.snapappointments.com/bootstrap-select/methods/#selectpickerrefresh">refresh</a>" after changing the <select>'s options via an update to the Vue model.</b></li>
</ul>
<div>
Please see my <a href="https://javascriptjedi.com/vue/solved_vue_with_bootstrap-select.html">write-up on JavaScript Jedi</a> and leave feedback here if you can help me enhance the article or find it helpful. I hope this saves someone some time and frustration! </div>
<div>
<br /></div>
That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-29626330398715015692018-10-18T08:17:00.001-06:002018-10-18T08:17:47.234-06:00[Azure DevOps] Pending Changes gives "you are not authorized to access ..." error during Check-InFor some reason lately I have been periodically getting "... you are not authorized to access [filename.extension] ..." after clicking on the Check-In button in the "Pending Changes" section of the "Team Explorer" tab in Visual Studio.<br />
<br />
I can't explain why this happens, but the workaround I've discovered is to click the "Refresh" button at the top of the dialog. Once the refresh is complete, the check-in process seems to work fine. I hope it works for you as well.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-9Q-k8COOFLA/W8iV9DmDxGI/AAAAAAAABQg/fEfLI-AlDuc-i21M0Ppl-9Ksty4caimIwCLcBGAs/s1600/pending_changes_refresh_button.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="113" data-original-width="513" height="70" src="https://1.bp.blogspot.com/-9Q-k8COOFLA/W8iV9DmDxGI/AAAAAAAABQg/fEfLI-AlDuc-i21M0Ppl-9Ksty4caimIwCLcBGAs/s320/pending_changes_refresh_button.png" width="320" /></a></div>
<br />
If you have run into the same and can explain why, please share in the comments.<br />
<br />
Happy coding!<br />
<br />That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-30731567635250787042018-06-27T00:10:00.000-06:002018-06-27T00:10:11.141-06:00ASP.NET MVC: Controlling order when using Scripts.Render<h2>
The Problem</h2>
I like concise, so when I realized I could change the following:<br />
<br />
<code>
@Scripts.Render("~/bundles/bundle1")<br />
@Scripts.Render("~/bundles/bundle2")<br />
@Scripts.Render("~/bundles/bundle3")<br />
@Scripts.Render("~/bundles/bundle4")
</code>
<br />
<br />
<div style="text-align: center;">
... to ...</div>
<br />
<code>
@Scripts.Render("~/bundles/bundle1", "~/bundles/bundle2", "~/bundles/bundle3", "~/bundles/bundle4")
</code>
<br />
<br />
... naturally I got excited. And it worked really well, that is, until I needed bundle3 to come before bundle1.<br />
<br />
For a more realistic example, I have my own bundle that is dependent on <a href="http://knockoutjs.com/" target="_blank">KnockoutJS</a> and requires Knockout to be loaded before my bundle is. It would look like this:<br />
<br />
<code>
@Scripts.Render("~/bundles/something", "~/bundles/knockout", "~/bundles/adminView")
</code>
<br />
<br />
I assumed that the bundles would be rendered in the same order I specified in that call, but it doesn't seem to play nice that way. My "~/bundles/adminView" was rendered before Knockout which broke my script.<br />
<br />
To fix it, I removed my bundle from the rest of the list and moved it out into a separate Scripts.Render call. Now it looks like:
<br />
<br />
<code>
@Scripts.Render("~/bundles/something", "~/bundles/knockout")<br />
@Scripts.Render("~/bundles/adminView")
</code>
<br />
<br />
Doing this forced my "adminView" bundle to be rendered after the other two because it was a separate call.<br />
<h2>
Moral of the Story</h2>
You can only "bundle" multiple bundle paths in one Scripts.Render call when none of them depend on each other, because you can't count on what order they will be rendered in. I don't know how Microsoft decides the order to render them in, but it's not necessarily the order you provide.<br />
<br />
<b>If order matters, use separate calls and order them the way you need to.
</b>That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-56898016229902384932018-06-13T08:41:00.001-06:002018-06-13T08:41:06.867-06:00NUnit 3.x: Asserting Equal Decimal Values with a Precision ThresholdWith the newer style of doing assertions in NUnit 3.x, I was trying to figure out how to assert a decimal whose value was 1 digit precision on the right-side of the decimal, but only allowing a variance of 0.0001.<br />
<br />
I believe this is the best practice, but will be happy to be corrected if someone knows a better way.<br />
<br />
<code style="text-align: center;">
Assert.That(actualObject.SomeDecimalValue, Is.EqualTo(42.6m).Within(0.0001m));
</code>That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-11795196979606710742015-08-21T13:31:00.001-06:002015-08-21T13:31:36.481-06:00[SOLVED] Visual Studio "Attach to Process": Available Processes list appears empty<h3>
The Quick Answer:</h3>
<br />
<ul>
<li>Open the "Attach to Process" dialog (For VS2012: Alt-D | P)</li>
<li>Open Task Manager (right-click taskbar | Start Task Manager)</li>
<li>Find msvsmon.exe, highlight it, then click End Process button</li>
<li>Go back to "Attach to Process" and click "Refresh" button</li>
</ul>
<br />
<h3>
The Story Behind the Answer:</h3>
I don't know if this will apply to more than Visual Studio 2012, but if it does, fantastic; please let us know in the comments.<br />
<br />
A few days ago, my Visual Studio 2012 went wonky and decided to not show me the Available Processes in the "Attach to Process" dialog in most cases.<br />
<br />
Here is what it should look like:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-7gh1iAN2now/Vdd4W5iOCHI/AAAAAAAABKg/gonlqtmwjG8/s1600/001%2B-%2BWhat%2BAttachToProcess%2BShould%2BLook%2BLike.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="378" src="http://4.bp.blogspot.com/-7gh1iAN2now/Vdd4W5iOCHI/AAAAAAAABKg/gonlqtmwjG8/s400/001%2B-%2BWhat%2BAttachToProcess%2BShould%2BLook%2BLike.png" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: left;">
Here is what it looks like when the problem occurs. Notice how, if I hover inside it, it will show me the tooltip for the process I'm currently over. This suggests the list is actually populated, and double-clicking on that spot will actually make the debugger try to attach to it. The information is there; the questions are, "Is it white font on white background?" or "Is there some other kind of painting/rendering issue with the dialog box?"</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-2O0OlU6YM5Y/Vdd4W_6057I/AAAAAAAABKc/U4RHdrsjptA/s1600/002%2B-%2BAttachToProcess%2B-%2BEmpty%2BAvailable%2BProcesses.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="390" src="http://2.bp.blogspot.com/-2O0OlU6YM5Y/Vdd4W_6057I/AAAAAAAABKc/U4RHdrsjptA/s400/002%2B-%2BAttachToProcess%2B-%2BEmpty%2BAvailable%2BProcesses.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Looking around the internet, I could not seem to find the right phrase to search to find help with this issue; until I finally stumbled upon this <a href="http://stackoverflow.com/questions/11179030/cant-start-debugger-in-vs2012-rc" target="_blank">StackOverflow question</a> that was as close as I could get. One answer suggested renaming MSVSMON.EXE in the x64 folder and copying the x86 version to it.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Not feeling comfortable with that, I decided to experiment with a similar concept. I opened Task Manager and saw that msvsmon.exe was a running process. </div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
I executed an "End Task" on the process, and was met with this error:</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-6brUT_GlG_M/Vdd7HYwwPEI/AAAAAAAABKs/owp_Jzms3FI/s1600/003%2B-%2Bmsvsmon.exe%2BError%2BAfter%2BEnd%2BTask.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-6brUT_GlG_M/Vdd7HYwwPEI/AAAAAAAABKs/owp_Jzms3FI/s1600/003%2B-%2Bmsvsmon.exe%2BError%2BAfter%2BEnd%2BTask.png" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
Going back to my already open "Attach to Process" dialog, I clicked "Refresh" and BAM! ... there appeared the visible list.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
I have done this several times, each time resulting in success. I hope it works for you!</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-42549855009525970312015-07-03T04:03:00.000-06:002015-07-03T04:03:11.204-06:00Data Dynamics Reports - Enabling sorting on column headers causes abnormal height growth when CanGrow is TrueI ran into a weird one today when working with a Data Dynamics Report (DDR) that needed sorting enabled on the column headers of a table. Each of the TextBox controls in the column header cells were set to CanGrow=True, which was good to make sure each could fit their content. Width was forced, so the only way for it to grow was in height. It worked great until I enabled sorting.<br />
<br />
Each column cell required sorting, so I added the SortExpression to each accordingly. The instant I brought up the report in the WebReportViewer, it looked like the heights of the header cells were twice as tall as needed to fill the largest content of any of the given cells.<br />
<br />
I then reverted my changes and the height went back to normal, making the height of the table row equal to the cell with the most content in it. I deduced that DDR was computing height based on the extra sorting control being added to each cell, but somehow was calculating too much. As I changed strings, the height would grow relative to the amount of content in the cell... so if the string was longer, the cell would grow all the more tall; looking double what the content in the cell was taking in height.<br />
<br />
As I was about to give up and disable sorting on the report because the headers were taking up 1/3 of the page, I decided to try an experiment.<br />
<br />
I turned CanGrow=False on every one of the column headers' TextBox controls (leaving the sorting enabled.) The height went down to minimal, where only one line of text would show, and the rest was hidden because the row wasn't tall enough. Great. I have a baseline now!<br />
<br />
The next step was to try changing CanGrow=True only on the cell I knew would have the most content in it.<br />
Win!<br />
<br />
So if you run into a similar issue, try the following:<br />
<br />
<ul>
<li><span style="font-size: large;"><b>Change CanGrow=False on all TextBox controls in your column headers, except the cell you know will have the largest content.</b></span></li>
</ul>
<div>
<span style="font-size: large;"><b><br /></b></span></div>
That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-22109009888107344412014-07-25T14:24:00.003-06:002015-07-03T04:00:39.279-06:00[SOLVED] GrapeCity/ComponentOne ActiveReports 8 - Excel Export using WebViewer is brokenThe ActiveReports engine is a great engine for rendering reports for an application. Sometimes, though, errors will occur that have multiple reasons for why they show up; some known, others not.<div>
<br /></div>
<div>
It's very hard to troubleshoot those or to find solutions for them, even if you have incredible internet search skills. That said, this solution may not work for you, but hopefully will give you a quick test to see if you can move on to another suggestion.</div>
<div>
<br /></div>
<div>
I was puzzled while trying to solve a problem we had in our web application where exporting via ActiveReports 8 WebViewer to PDF or DOC worked fine, but XLS for some reason returned a 404 error.</div>
<div>
<br /></div>
<div>
The problem turned out to be with mine that we missed laying down a DLL required by the Excel exporter (GrapeCity.ActiveReports.Export.Excel.v8.dll) called <b>DocumentFormat.OpenXml.dll</b>, which didn't seem to be referenced by the other Export DLLs. </div>
<div>
<br /></div>
<div>
As soon as I put this in place, it started working again.</div>
<div>
<br /></div>
<div>
This file could be easily missed as it is not named like the other export related DLLs:</div>
<div>
<i>GrapeCity.ActiveReports.Export.*.v8.dll</i></div>
<div>
<i><br /></i></div>
<div>
Make sure to include that and you'll have one less reason to see a 404 error when you try exporting to Excel from ActiveReports!</div>
<div>
<br /></div>
<div>
On a side note, the following are all of the referenced DLLs (Microsoft DLLs excluded) for GrapeCity.ActiveReports.Export.Excel.dll in case you need to check for others also missing:</div>
<div>
<ul>
<li><b>DocumentFormat.OpenXml.dll</b></li>
<li>GrapeCity.ActiveReports.Diagnostics.v8.dll</li>
<li>GrapeCity.ActiveReports.Document.v8.dll</li>
<li>GrapeCity.ActiveReports.Export.Rdf.v8.dll</li>
<li>GrapeCity.ActiveReports.Extensibility.v8.dll</li>
<li>GrapeCity.ActiveReports.v8.dll</li>
</ul>
<div>
Best of luck!</div>
</div>
That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com1tag:blogger.com,1999:blog-6979072578516949047.post-73040823910669269712013-08-28T17:40:00.002-06:002013-08-28T17:40:32.511-06:00MSBuild - Copy Task using Items with Includes and ExcludesI struggled for a couple of hours on this one... until finally finding <a href="http://stackoverflow.com/questions/3031021/trying-to-exclude-certain-extensions-doing-a-recursive-copy-msbuild">this useful answer from stackoverflow.com</a>! My scenario was needing to copy some files in a pre-build step in a Visual Studio C# project. For every file I needed to copy, there was a counterpart for each of our supported languages. The naming convention for the files was like this:<br />
<br />
MyFile.properties<br />
MyFile_de_DE.properties<br />
MyFile_fr_FR.properties<br />
<br />
etc.<br />
<br />
Here is what my csproj looked like:<br />
<pre class="brush:xml"><PropertyGroup>
<ReportingEnglishLocFolder>$(SolutionDir)..\path\to\destination\</ReportingEnglishLocFolder>
</PropertyGroup>
<ItemGroup>
<EnglishLocPropertiesFiles
Include="$(ReportingBaseFolder)master\*.properties"
Exclude="*_de_DE.properties"/>
</ItemGroup>
<Copy
SourceFiles="@(EnglishLocPropertiesFiles)"
DestinationFolder="$(ReportingEnglishLocFolder)" />
</pre>
After attempting multiple combinations of wildcard patterns and having no success, I changed the search phrases I used to search the internet and found the <a href="http://stackoverflow.com/questions/3031021/trying-to-exclude-certain-extensions-doing-a-recursive-copy-msbuild">stackoverflow thread</a>.<br />
The most important part from the thread, and the most important thing to remember about using Include and Exclude is:<br />
<blockquote class="tr_bq">
<b>If you use an absolute path for Include, you must use an absolute path for Exclude. If you use a relative path for Include, you must use a relative path for both.</b></blockquote>
So I changed to this and it worked like a champ!<br />
<pre class="brush:xml"><ItemGroup>
<EnglishLocPropertiesFiles
Include="$(ReportingBaseFolder)master\*.properties"
Exclude="$(ReportingBaseFolder)master\*_de_DE.properties"/>
</ItemGroup>
</pre>
<br />
So remember, whichever type of path you use for Include, you need to do the same for Exclude.
That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-48905198376255264102013-08-09T04:04:00.000-06:002013-08-09T10:37:22.196-06:00Silverlight: ObservableCollection.CollectionChanged Event<b><span style="color: purple;">
The ObservableCollection<T>.<a href="http://msdn.microsoft.com/en-us/library/system.collections.specialized.inotifycollectionchanged.collectionchanged(v=vs.95).aspx" target="_blank">CollectionChanged </a> event will only fire if the collection itself changes (items are added, removed or rearranged/moved.), <i style="text-decoration: underline;">NOT</i> if an item in the collection changes (meaning one of the item's in the collection has one or more properties modified.)</span></b><br />
<br />
It's now around 2:30am, and I've been going the rounds for a couple of hours with a Silverlight project at work; fighting a seemingly simple change that ended up not being so simple because of a major misunderstanding of the CollectionChanged event on the ObservableCollection<T>. My ObservableCollection<T>.CollectionChanged event was not firing like I thought it should.</t></t><br />
<br />
I imagine this will be a common mistake for others, and I'm sure I myself am doomed to repeat it again in the future; so I'm documenting it for a quick reminder later.<br />
<br />
My thought process was this... "I need to watch my ObservableCollection for one of its items to be changed. Once the item is changed, I need to flip a boolean flag which happens to be bound to a command which will disable a button after the change has been made."<br />
<br />
Simple, right?<br />
I should just be able to handle the ObservableCollection<T>.CollectionChanged event, because if one of those items in the collection gets changed, that counts... </t><br />
<br />
Or not.<br />
<br />
See, the misconception is, the collection itself hasn't changed. It has the exact same items it had before, it's just that the item within the collection changed. So there are two things that can happen to your collection:<br />
<br />
<ul>
<li>The collection itself is modified (Items are added, removed or moved.)</li>
<li>An item in the collection is modified (its individual properties are changed.)</li>
</ul>
<br />
Let me illustrate:<br />
For this example we will have a class called VideoGame to keep track of titles and costs.<br />
<br />
<pre class="brush:c#">public class VideoGame : INotifyPropertyChanged
{
private string _title;
public string Title
{
get { return _title; }
set
{
_title = value;
RaisePropertyChanged("Title");
}
}
private decimal _cost;
public decimal Cost
{
get { return _cost; }
set
{
_cost = value;
RaisePropertyChanged("Cost");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
</pre>
<br />
And now some code to use it...
<br />
<br />
<pre class="brush:c#">ObservableCollection<VideoGame> videoGames = new ObservableCollection<VideoGame>
{
new VideoGame{ Title = "TrackMania 2: Stadium", Cost = 9.99M },
new VideoGame{ Title = "TrackMania 2: Canyon", Cost = 19.99M },
new VideoGame{ Title = "TrackMania 2: Valley", Cost = 19.99M },
};
videoGames.CollectionChanged += (sender, eventArgs) => {
Debug.WriteLine("--- The collection changed");
};
Debug.WriteLine("Changing the cost of one of the video games in the collection...");
// The CollectionChanged event will not fire because it was just one of the collection's items that changed...
// not the collection itself.
videoGames[0].Cost = 7.99M; // the game went on sale.
Debug.WriteLine("Adding a video game to the collection...");
// The CollectionChanged event WILL fire because the collection of items changed.
videoGames.Add(new VideoGame{ Title ="ShootMania: Storm", Cost = 19.99M });
</pre>
<br />
And the output...<br />
<blockquote class="tr_bq">
<b>Changing the cost of one of the video games in the collection...<br />Adding a video game to the collection...<br />--- The collection changed</b></blockquote>
When it dawned on me, at first I was frustrated; but then clarity set in. It makes sense to me that we only want the CollectionChanged event to fire when the collection itself is modified, not its individual parts. With it, the <a href="http://msdn.microsoft.com/en-us/library/system.collections.specialized.notifycollectionchangedeventargs(v=vs.95).aspx" target="_blank">NotifyCollectionChangedEventArgs</a> that are passed with the event give us a lot of good information about what happened with the change.<br />
<br />
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/system.collections.specialized.notifycollectionchangedaction(v=vs.95).aspx" target="_blank">Action</a> - Tells which action effected the change in the collection (Add, Remove, Replace, Reset)</li>
<li>NewItems - The items affected by the action</li>
<li>OldItems - The items removed or replaced in the collection</li>
<li>NewStartingIndex - Tells the index where the change occurred.</li>
<li>OldStartingIndex - Tells where a Replace or Remove action took place.</li>
</ul>
<div>
While all this is cool, what do I need to have happen so I can track when an item in the collection is changed?</div>
<div>
<br /></div>
<div>
I need to make sure I handle PropertyChanged on the individual items of the collection. Make note, this can be tricky because I still need to watch for the Collection to change so I can subscribe to any new items' PropertyChanged events as they are added to the collection, and clean up after myself when the items leave the collection.</div>
<div>
<br /></div>
<div>
I'll let you decide how you're going to approach that; it's a discussion for another day. But I want to drive home the concept that the CollectionChanged event is just that... when the collection itself changes, not its individual items. Know that, and you'll save yourself a lot of time.</div>
That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-81568707071849012852013-04-02T15:30:00.001-06:002013-04-02T15:30:34.331-06:00[SOLVED] "Attaching the Script debugger to process iexplore.exe failed" message after upgrading to Internet Explorer 10HUGE thanks going out to <a href="http://stackoverflow.com/users/2125365/dmitri-leonov-msft">Dmitri Leonov - MSFT</a> in a <a href="http://stackoverflow.com/questions/13421797/vs2010-and-ie10-attaching-the-script-debugger-to-process-iexplore-exe-failed">stackoverflow article about the same message.</a><br />
After updating to Internet Explorer 10, I started receiving the error when debugging with Visual Studio 2010:<br />
<br />
<b>"Attaching the Script debugger to process [####] iexplore.exe failed. Another debugger may already be attached to the process."</b><br />
<b><br /></b>
The fix:<br />
<br />
<ul>
<li>Close all instances of Internet Explorer. (I will add you can leave Visual Studio open)</li>
<li>Run a command prompt as Administrator and paste this little gem into the command line:</li>
</ul>
<br />
<textarea style="border-style: solid; border-width: 1px; margin: 1em auto; padding: 0; width: 90%;">regsvr32.exe "%ProgramFiles(x86)%\Common Files\Microsoft Shared\VS7Debug\msdbg2.dll"</textarea><br />
It worked like a champ for me (and apparently others from comments on the stackoverflow post.)<br />
<br />
Good riddance to an annoying message! Happy coding.That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com27tag:blogger.com,1999:blog-6979072578516949047.post-55561653987469627182013-01-08T14:45:00.000-07:002013-01-08T14:45:31.246-07:00JavaScript String.Replace: Replacement Pattern for Full Match ReminderThis is my quick reminder to self to use<br />
<b><span style="font-size: x-large;">$&</span></b><br />
instead of<br />
<span style="font-size: large;">$0</span><br />
in JavaScript for Regular Expression replacement patterns when wishing to use the entire matched text.<br />
<br />
Some browsers allow $0, but $& is <a href="http://www.ecma-international.org/ecma-262/5.1/ECMA-262.pdf" target="_blank">officially documented</a> (table 22, page 147) and supposed to work in all of them.<br />
<br />
<i>Example:</i><br />
document.write("Hello World".replace(/^Hello/, "$& to the whole");<br />
<br />
<i>Output:</i><br />
Hello to the whole World<br />
<br />That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-48516208912549301812012-09-29T03:21:00.001-06:002018-06-13T08:32:53.929-06:00LINQ to Objects: Querying Nested CollectionsI love LINQ. LINQ is just fun to use. I love the fact that I can query the same way across databases, XML documents and objects (alright XML is slightly different, but...)<br />
<br />
Today I had the task of querying down a complex set of objects that each own a collection data member. I needed to get to some data 4-layers deep and thought the LINQ query would get pretty nasty. That is until I learned that I can use the "from" keyword multiple times in the same LINQ query.<br />
<br />
My example will be very contrived to save having to look at a lot of extra code for different collections, so I'll go ahead and just nest the same collection several layers down, just so we get the concept.<br />
<br />
I have a Person class:<br />
<div style="border: 1px solid #CCC; margin: 1em; padding: 0.5em;">
<code style="font-family: consolas, sans-serif; font-size: 11px;">
public class Person<br />
{<br />
public string FirstName { get; set; }<br />
public string LastName { get; set; }</code><br />
<code style="font-family: consolas, sans-serif; font-size: 11px;"><br /></code>
<code style="font-family: consolas, sans-serif; font-size: 11px;"> public int IQ { get; set; }<br />
<br />
private List<Person> _children;<br />
public List<Person> Children<br />
{<br />
get{ return _children ?? (_children = new List<Person><person>()); } </person><br />
set{ _children = value; }<br />
}<br />
}</code></div>
If I set up a new List<Person> like so:<br />
<div style="border: 1px solid #CCC; margin: 1em; padding: 0.5em;">
<code style="font-family: consolas, sans-serif; font-size: 11px;">
List<Person> grandpas = new List<Person><br />
{<br />
new Person{<br />
FirstName = "John", LastName = "Doe", IQ = 91,<br />
Children = new List<Person>{<span class="Apple-tab-span" style="white-space: pre;"> </span>// children<br />
new Person{ FirstName = "Meh", LastName = "Doe", IQ = 103 },<br />
new Person{<br />
FirstName = "Tae Kwon", LastName = "Doe", IQ = 125,<br />
Children = new List<Person>{<span class="Apple-tab-span" style="white-space: pre;"> </span>// grandchildren<br />
new Person{ FirstName = "Karate", LastName = "Kid", IQ = 68, },<br />
new Person{ FirstName = "Karate", LastName = "Kid2", IQ = 117 },<br />
}<br />
},<br />
}<br />
},<br />
<br />
new Person{<br />
FirstName = "John", LastName = "Doh", IQ = 135,<br />
Children = new List<Person>{<span class="Apple-tab-span" style="white-space: pre;"> </span>// children<br />
new Person{ FirstName = "Meh", LastName = "Doh", IQ = 42 },<br />
new Person{<br />
FirstName = "Tae Kwon", LastName = "Doh", IQ = 140,<br />
Children = new List<Person>{<span class="Apple-tab-span" style="white-space: pre;"> </span>// grandchildren<br />
new Person{ FirstName = "Doh", LastName = "Boy", IQ = 111 },<br />
new Person{ FirstName = "Doh", LastName = "Nut", IQ = 98 },<br />
}<br />
},<br />
}<br />
},<br />
};<br />
</code>
</div>
Check out how easy it is to get to data that is below the top layer:
<br />
<div style="border: 1px solid #CCC; margin: 1em; padding: 0.5em;">
<code style="font-family: consolas, sans-serif; font-size: 11px; white-space: pre;">
IEnumerable<Person> grandChildrenWithHighIQ = from grandpa in grandpas
from parent in grandpa.Children
from grandkid in parent.Children
where
grandkid.IQ > 100
select grandkid;
</code></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-OgkvtSYp7JA/UGa7avmbRCI/AAAAAAAAAzo/bkxh-YgJKMI/s1600/grandchildrenWithHighIQ_results.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="149" src="https://2.bp.blogspot.com/-OgkvtSYp7JA/UGa7avmbRCI/AAAAAAAAAzo/bkxh-YgJKMI/s320/grandchildrenWithHighIQ_results.png" width="320" /></a></div>
<b>EDIT: </b>I was asked to provide a version with the fluent API as well:<br />
<br />
<div style="border: 1px solid #CCC; margin: 1em; padding: 0.5em;">
<code style="font-family: consolas, sans-serif; font-size: 11px; white-space: pre;">
IEnumerable<person> grandChildrenWithHighIQ = grandpas
.SelectMany(grandpa => grandpa.Children.SelectMany(parent => parent.Children))
.Where(grandchild => grandchild.IQ > 100);
</person></code></div>
<br />
Now we know longer need be afraid of querying complex objects and getting the data we want. Write your queries in the bLINQ of an eye, and get your data back even faster!<br />
<br />
And I'll put a shameless plug out there for <a href="http://linqpad.com/" target="_blank">LINQPad</a> since I took a screenshot from its results above. It is a fantastic tool for crafting LINQ queries and is a wonderful sandbox for small .NET proof-of-concept projects because you can write small programs without creating a Visual Studio project or solution.<br />
<br />
Enjoy!That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-80102622755729769942012-08-29T12:47:00.000-06:002012-08-29T12:47:47.513-06:00Telerik RadNumericUpDown for Silverlight does not update binding when value is blanked out<i>Note: This may be fixed in a later version of Telerik controls; this issue was discovered in the Telerik.Windows.Controls library; version 2011.1.315.1040</i><br />
<i><br /></i>
Working on a Silverlight application, we found ourselves with some dialogs that utilize the <a href="http://www.telerik.com/products/silverlight/numericupdown.aspx">Telerik RadNumericUpDown</a> control. We discovered a defect when binding the Value property of the control to a ViewModel behind the scenes.<br />
<br />
The original binding works as expected, and if the spinner controls are used to change the value, the ViewModel updates as we expect. If a user types a new value into the control it also updated the binding on the ViewModel correctly.<br />
<br />
The problem arises when we blank out the value by clicking into the textbox portion of the control, highlighting it and hitting the Backspace or Delete key. Removing focus from the control or not, the ViewModel property bound to that control <b>does not</b> get updated.<br />
<br />
To work around this, we created this Event Handler:<br />
<pre style="background-color: #EEE; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 13px; padding: 0.5em;"><span style="color: blue;">void</span> RadNumericUpDown_ValueChanged(<span style="color: blue;">object</span> sender,
<span style="color: #2b91af;">RadRangeBaseValueChangedEventArgs</span> e)
{
<span style="color: blue;">if</span> (sender <span style="color: blue;">is</span> <span style="color: #2b91af;">RadNumericUpDown</span>)
{
<span style="color: #2b91af;">RadNumericUpDown</span> upDownControl = sender <span style="color: blue;">as</span> <span style="color: #2b91af;">RadNumericUpDown</span>;
<span style="color: blue;">if</span> (!upDownControl.Value.HasValue)
upDownControl.Value = upDownControl.Minimum;
}
}</pre>
Now we apply this event handler to our XAML:
<pre style="background-color: #EEE; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 13px; padding: 0.5em;"><span style="color: blue;"><</span><span style="color: #a31515;">telerik</span><span style="color: blue;">:</span><span style="color: #a31515;">RadNumericUpDown</span><span style="color: red;"> ValueChanged</span><span style="color: blue;">=</span><span style="color: blue;">"RadNumericUpDown_ValueChanged"</span> ... <span style="color: blue;">/></span></pre>
This makes the behavior act the same as if the user had used the down spinner on the control to modify the value down to the minimum.<br />
<br />
This may not be the ideal solution for you, but understanding this will give you direction on how you can create your own workaround. We started down the path of handling the KeyUp event, but ValueChanged seems much more appropriate.<br />
<br />
Telerik makes fantastic controls; and I'm not mad by any means. Being a developer myself, I realize these things happen; but I do hope they have handled this better in a later release. And as far as I know this is the only control in the 2011.1.315.1040 version that has this issue.That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-16964885915719147252012-07-04T11:06:00.004-06:002012-07-04T11:06:51.183-06:00PayPal Instant Payment Notification (IPN) Simulator Requires Port 80Someone please correct me if I'm wrong, but I fought for an hour or more trying to get <a href="http://paypal.com/">PayPal</a>'s <a href="https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_admin_IPNTesting">Instant Payment Notification (IPN) Simulator</a> to post test transactions to my test site running on <a href="http://learn.iis.net/page.aspx/860/iis-express/">IIS Express</a> which listens on a port different than 80; and it just doesn't work.<br />
<br />
I asked friends on external networks to bring my site up in their browsers and they could hit it fine on the non-standard port, so I knew my firewall and IIS Express were configured correctly.<br />
<br />
I cannot find anywhere in PayPal's documentation where it states that port 80 is required but it seems their own firewall is pretty strict with outbound traffic. Once I forced my router to listen on port 80, and forward the request to the non-standard port my site listens on, the simulator worked like a champ.<br />
<br />
So, if you're having problems with the IPN simulator not sending to your listener and you are listening on a non-standard port, make sure to do one of the following:<br />
<br />
<ol>
<li>Force your test site to listen on port 80 and configure your firewall accordingly.</li>
<li>Force your router to listen on port 80, but forward to the port your test site is set up on.</li>
</ol>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="http://2.bp.blogspot.com/-zMKHQ-XqsWs/T_R35S2-ZRI/AAAAAAAAAvI/5mNB-NQYdXY/s1600/ipn_successfully_sent.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="57" src="http://2.bp.blogspot.com/-zMKHQ-XqsWs/T_R35S2-ZRI/AAAAAAAAAvI/5mNB-NQYdXY/s400/ipn_successfully_sent.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">That's what we like to see!</td></tr>
</tbody></table>
<div>
<span style="background-color: white;">As a side note, if I remember correctly... the standard ASP.NET Development Server (Cassini) that comes with </span><a href="http://www.microsoft.com/visualstudio/" style="background-color: white;">Visual Studio</a><span style="background-color: white;">, does not accept requests from an external network. I believe it can only accept them from the same machine you are doing the development on. This is why I opted to use </span><a href="http://learn.iis.net/page.aspx/860/iis-express/" style="background-color: white;">IIS Express</a><span style="background-color: white;">.</span></div>
<div>
<br /></div>
<div>
Hope this saves someone else some time and head-banging on your desk. :)</div>
<div>
Happy coding!</div>That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com6tag:blogger.com,1999:blog-6979072578516949047.post-18866314298942562232011-11-02T18:24:00.001-06:002012-08-29T12:48:14.517-06:00Silverlight Unit Testing: TestInitialize/TestCleanup methods do not fire, tests do not run, nor pass/fail, and no details givenI had the classic bang-head-against-the-keyboard for three hours scenario today while doing Silverlight Unit Testing.<br />
<br />
I had a test class with two unit tests in them which worked fine, until I started refactoring to get common setup/teardown functionality moved into methods with TestInitialize/TestCleanup attributes applied.<br />
<br />
Then the weirdness started. I started seeing this:<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="http://2.bp.blogspot.com/-BohLGvLaa0U/TrHZVXbJWSI/AAAAAAAAArc/Wj353kCwyE8/s1600/testinitialize_weirdness.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="118" src="http://2.bp.blogspot.com/-BohLGvLaa0U/TrHZVXbJWSI/AAAAAAAAArc/Wj353kCwyE8/s320/testinitialize_weirdness.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">The tests don't run; don't pass or fail; and no details as to why.<br />
Check out the yellow highlighted areas.</td></tr>
</tbody></table>
As you can see, there wasn't a whole lot of information present to assist me in my predicament. After removing the TestInitialize/TestCleanup attributes, the tests worked fine.<br />
<br />
I began searching the internet trying to find someone who ran into the same problem. I found many results where developers complained of the TestInitialize methods swallowing exceptions and moving on, so I started down that path in my own code, trying to discover if there was an exception I was missing that was being eaten and causing the entire class to fail.<br />
<br />
The only problem was, when I commented all code within the bodies of the TestInitialize/TestCleanup methods, I still ended up with the same results.<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="http://4.bp.blogspot.com/-kQOYRssG7Fc/TrHZVv2gFEI/AAAAAAAAArg/lKZRuikddLM/s1600/testinitialize_weirdness_small.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="http://4.bp.blogspot.com/-kQOYRssG7Fc/TrHZVv2gFEI/AAAAAAAAArg/lKZRuikddLM/s400/testinitialize_weirdness_small.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">???</td></tr>
</tbody></table>
Digging deeper, it finally dawned on me that the test class in question was inheriting from a base test class that also happened to have methods dressed with the TestInitialize/TestCleanup attributes. Looking at the declaration of TestInitializeAttribute revealed what I suspected:<br />
<br />
<code style="white-space: pre-line; font-family: consolas, courier, sans-serif;">
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class TestInitializeAttribute : Attribute
</code>
<div><br />
AllowMultiple = false... which makes sense. So my code had two problems:</div>
<br />
<ol>
<li>My base class' method for TestInitialize needed to be made virtual so it could be overridden in classes that inherit from it to allow for overriding (and calling back to the base so both get their jobs done.)</li>
<li>My child class' method was named different, so I needed to go make it match and override.</li>
</ol>
<div>
Once I fixed these problems for both the init and cleanup methods, the tests went back to working accurately.</div>
<div>
<br /></div>
<div>
<b>Bottom Line: When doing unit testing with base test classes that perform common setup functionality, make sure to have your Setup/Teardown or Initialize/Cleanup methods marked virtual in the base class, so they can be overridden in the sub-classes. Then override those methods in the sub-class, making sure to call back to the base appropriately.</b></div>
<div>
<b><br /></b></div>
<div>
Example (MSTest):</div>
<div>
<br /></div>
<code style="white-space: pre; font-family: consolas, courier, sans-serif;">
public class CommonTestSetupBase
{
[TestInitialize]
public virtual void TestSetup()
{
// do common setup work for all that inherit
}
[TestCleanup]
public virtual void TestTeardown()
{
// do common cleanup work for all that inherit
}
}
//=================
[TestClass]
public class UnitTestedClassTests : CommonTestSetupBase
{
[TestInitialize]
public override void TestSetup()
{
base.TestSetup();
// do common setup for this class
}
[TestCleanup]
public override void TestTeardown()
{
// do common cleanup for this class
base.TestTeardown();
}
}
</code>
I knew I wouldn't remember this if I bump into it again, so I had to document it here... I wish I had more time to go into more detail, but I hope this will be enough to help you and I in the future when we meet this problem again.That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com1tag:blogger.com,1999:blog-6979072578516949047.post-72072675475218206912011-08-30T18:21:00.001-06:002011-08-30T18:21:47.717-06:00Silverlight throws InvalidOperationException with Common_MethodFailed error when calling HtmlElement.SetAttribute for the "type" attributeI was working on a Silverlight application the other day and needed to create and submit an HTML form to another ASPX page inside the website hosting the app.<br />
<br />
To accomplish this, I was using the HtmlDocument.CreateElement, HtmlElement.SetAttribute and HtmlElement.AppendChild methods to set the whole thing up.<br />
<br />
After deploying the application having success with Internet Explorer 9 (IE9), Chrome, etc. we tested on two virtual machines that had IE7 and IE8 and were met with errors and no form submission.<br />
<br />
It turns out it was because I was trying to set the "type" attribute of the <input /> elements I was creating with the Silverlight methods after I'd called the AppendChild method.<br />
<br />
IE7 and IE8 don't like a developer setting the "type" attribute after the element has been appended to the document and it will throw a JavaScript error which bubbles up to the Silverlight application and the user is shown an obscure InvalidOperationException error whose key is "Common_MethodFailed".<br />
<br />
It made no sense to me, but I dug deeper until I could find the offending line. I then tried an experiment with a XHTML/JavaScript page in the offending browsers to see if it was a Silverlight problem or a problem with the JavaScript engines of said browsers.<br />
<br />
I ran into the same problem without Silverlight in the equation. I wrote up a <a href="http://javascriptjedi.com/HTMLElement/setAttributeExperiment.html">detailed article explaining it</a> and have the experiment to try in different browsers yourself on my <a href="http://www.javascriptjedi.com/">JavaScript Jedi </a>website.<br />
<br />
The problem can be solved by making sure you call AppendChild <strong>AFTER</strong> you set up all the properties/attributes on the HtmlElement, not before.<br />
<br />
I hope this helps you avoid several hours of frustration. :)<br />
Happy Coding!That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-22231949049622428052011-06-17T18:37:00.000-06:002011-06-17T18:37:38.556-06:00WCF "Gotcha" When Sending an Invalid Enum Value Across the BoundaryThe Silverlight application I'm working on for my employer at the time of this writing involves WCF services that provide data for the app.<br />
<br />
It was working fine until a coworker ran a SQL query that changed an int field (which maps to an Enum in our code) to an invalid value on several records in a common test database a few of us were pointing to.<br />
<br />
All of the sudden we were getting a FaultException (I can't remember the exact error - but it was one of those unrelated ones that mask the real exception message that caused the problem to begin with.) while trying to load a graph.<br />
<br />
Further investigation revealed that our asynchronous calls to the WCF service client were retrying the same call three times and coming back with a failure. Stepping down deeper into the code, we saw that our resultset seemed to be coming back fine from our data layer, leaving us wondering why it was behaving so strange.<br />
<br />
My coworker put a breakpoint where our data layer returns the IEnumerable<t> to the service so it can serialize/return the data to our app, and changed the resultset to an empty array of the given type instead of allowing the real resultset through.</t><br />
<br />
Only a single attempt was made on the service instead of three and the data was received fine by the application... However, when we reverted to allow the regular resultset through, it again made three attempts and returned the exception.<br />
<br />
On the next run, we decided to look at the resultset closer in the QuickWatch window. We didn't look carefully enough because everything looked correct... so we started removing one object at a time until we could prove that the culprit was corrupt data in our resultset.<br />
<br />
Finally we expanded two objects in our collection at the same time in the QuickWatch window and started comparing. We noticed that one instance's enum property was retuning the name of the assigned enum and another showed an integer value.<br />
<br />
After asking why that would happen, it dawned on me that it must be an invalid enum value and therefore couldn't resolve to a name when ToString() was called. I assume to fail gracefully, Microsoft had their Enum.ToString() try to do name first and just fall back to the value of the Enum's base type if it couldn't resolve the name, rather than throwing an Exception.<br />
<br />
Examining the implementation of the enum quickly confirmed the suspicion. After probing other coworkers, we discovered the erroneous query that had been run against the database. :)<br />
<br />
I assume what happens is an exception occurs when the WCF tries to serialize the object to send across the boundary... but the exception is swallowed up and the Fault Exception with the less-than-helpful error message is returned to the consumer of the service.<br />
<br />
<strong>Moral of the story: Be careful when using Enums that map to data in your database. Make sure they always match the allowed values in the database (which is easy to do if you have look-up tables and use an ORM (like <a href="http://www.plinqo.com/">PLINQO</a>, etc.) to generate the enums for you. </strong><br />
<br />
<strong>Also, if you use "switch" statements in your code on the enum value, make sure your default case handles an invalid enum value scenario. </strong><strong>For instance, you've made a switch statement that contains a case block for all valid enum values. Your default case can throw an <a href="http://msdn.microsoft.com/en-us/library/system.argumentoutofrangeexception.aspx">ArgumentOutOfRangeException</a> so you can quickly detect when something like that happens.</strong><br />
<br />
This will be harder to detect in scenarios like the one I described above, because the code is still "valid" at compile and runtime until the sucker is serialized. I don't know what the best practice solution is for that scenario, but I do know I want a better FaultException passed to me if serialization breaks before being passed from the WCF service.<br />
<br />
Guess I have my work cut out for me tomorrow.<br />
<br />
For those like me that have a hard time visualizing what I'm talking about... here's a contrived example.<br />
<code><br />
public enum ReportType<br />
{<br />
Html = 1,<br />
Excel = 2,<br />
Pdf = 3<br />
}<br />
</code><br />
The query to the database changed the offending records to 0, 4 or some other number that wasn't the valid values. (Incidentally, my coworker really meant to change another int field's value... but ended up typing the wrong column name.) Too funny.<br />
<br />
And now you know... go forth and make sure you aren't bit by this problem.That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-27128014265947357522011-06-17T18:03:00.001-06:002011-06-17T18:43:52.692-06:00Silverlight and Unit Testing - Reference Gotcha When Testing View CodeI'm new to Silverlight and am still learning my way around various nuances of the platform.<br />
<br />
I like it so far and have been learning a lot. When a recent check-in was made by a co-worker to our project at work, we had around 90 of over 800 unit tests broken with this error in each of them:<br />
<strong>The attachable property 'TransitionEffect' was not found in type 'ExtendedVisualStateManager'.</strong><br />
<br />
Being new to XAML, I had no idea where to start, except to go find the .xaml files resource dictionaries to try to figure out what the problem was. Searching the internet made it sound like I had x:Name attributes with the same values from different dependent XAML files.<br />
<br />
I should mention I was working in a UserControl XAML file to begin with, and started searching up the dependency hierarchy to see if the naming problem was the real issue.<br />
<br />
The XAML files were quite large, so finding all the x:Name attributes and comparing them was going to take a while. I opted to approach this using <a href="http://www.linqpad.com/">LINQPad</a>. I replaced quotes with double-quotes in the XAML so I could bring it into LINQPad as a string constant and loaded it into a XDocument object; using LINQ to XML to find matches from the other file's XAML for me. After discovering one, I fixed it and re-ran my unit tests. I still got the same error.<br />
<br />
What I didn't understand was that the application ran fine and the effect was working... why on earth wouldn't it work in our unit tests? It should have dawned on me that both the application project and the unit test project required the appropriate DLLs to be referenced in order for that namespace to be available.<br />
<br />
Sure enough, the application project had them but the unit test project did not. As soon as I added the references, the tests passed with flying colors again.<br />
<br />
The required references, for those that get the exact same error:<br />
<strong>Microsoft.Expression.Effects</strong><br />
<strong>Microsoft.Expression.Interactions</strong><br />
<strong>(both part of the <a href="http://silverlight.codeplex.com/releases/view/43528">Silverlight v4.0 Toolkit</a>)</strong><br />
<br />
<strong>Moral of the story: When you add references to your application project, make sure you add them to the corresponding unit test project if necessary as well.</strong><br />
<br />
Now the next question... Why was I required to have those when I wasn't rendering any UI, but merely using the View classes in the unit test? It seemed as if the XAML was still being parsed.<br />
<br />
This makes sense, being as your View classes are declared as partial and the other piece to this partial puzzle is the XAML file. Remember that whenever the View class is constructed, the InitializeComponent method is called, which parses the XAML and sets up everything declared there (correct me if I'm not describing that accurately!)<br />
<br />
That said, if you're going to unit test a view file, you have to make sure that anything required by the XAML files are also referenced. It's easy to take care of that for the C# files because you get compile-time errors, but you don't necessarily with the declarative XAML files. A drawback, but one that can be handled if we're aware.<br />
<br />
<strong>Bottom line... remember that both the XAML and corresponding C# file make up your View class and both files' reference requirements must be met to ensure all namespaces/code are available for your unit tests.</strong><br />
<br />
I hope this will save you (and myself) a lot of time in the future if I ever encounter this problem again.That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-128964020257792312010-08-26T11:18:00.004-06:002010-11-11T07:41:35.474-07:00Making sure JavaScript gets called after an AJAX Partial Postback in ASP.NET<div style="color: #336699; font-size: 1.2em; font-weight: bold;">ScriptManager.RegisterStartupScript(Page page, Type type, string key, string script, bool addScriptTags)</div><br />
Notice the first parameter of this method, as it has an overload that takes a Control as its first parameter. When you use the Control overload, it only registers the startup script once on the initial rendering of the page.<br />
<br />
If you use the Page overload listed above, it will register it for every asynchronous postback to ensure your JavaScript is executed everytime.<br />
<br />
Remember to use ScriptManager and not ClientScriptManager... <br />
<br />
From the Remarks on the <a href="http://msdn.microsoft.com/en-us/library/bb310408(VS.90).aspx">MSDN article for the overload listed above</a>:<br />
<blockquote>"You use this method to register a startup script block that is included every time that an asynchronous postback occurs. To register a script block for a control that is inside an UpdatePanel control so that the script block is registered only when the UpdatePanel control is updated, use the RegisterStartupScript(Control, Type, String, String, Boolean) overload of this method.<br />
<br />
If you want to register a startup script that does not pertain to partial-page updates, and if you want to register the script only one time during initial page rendering, use the RegisterStartupScript method of the ClientScriptManager class. You can get a reference to the ClientScriptManager object from the ClientScript property of the page."</blockquote><br />
<b>Also very important:</b><br />
Make sure you have the following using statement in your codebehind...<br />
<code>using System.Web.UI;</code><br />
<br />
If you don't, you will get the error:<br />
<i>Cannot access internal property 'ScriptManager' here</i>That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com4tag:blogger.com,1999:blog-6979072578516949047.post-86949997232694196302010-07-30T08:46:00.000-06:002010-07-30T08:46:39.160-06:00Having Two Consecutive Hyphens in XHTML Comments Causes Anomalies in Firefox<a href="http://thatoneplace.net/issues/firefox/html_comment_defect/">Check out my experiment page to see the different side effects this issue causes.</a><br />
<br />
If you're seeing weirdness when having hyphens in your (X)HTML comments, it's most likely because you're using two adjacent to each other, which is a big no-no according to the W3C.<br />
<br />
Avoid using hyphens in your comments to avoid the issues... dumb, but required.<br />
<br />
I know we're all prone to yell at Mozilla and tell them they're doing it wrong, because all the other latest versions of other browsers will still honor the comments, but Firefox isn't necessarily doing it wrong, because as I understand it, they're allowed by the standard to <b>not</b> honor the comment if there are two adjacent hyphens together. In one way I think it's good that Firefox doesn't, because it forces we, the web authors, to follow standards.<br />
<br />
On the other hand, I think the standard is flawed and that nothing between the start and end of a comment should matter... so part of me wishes Firefox would do like the other browsers and still honor the comments.<br />
<br />
Oh well, go the safe route and keep hyphens out of your comments.That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-47729326912886653082010-06-10T10:22:00.000-06:002010-06-10T10:22:18.517-06:00Visual Studio 2008 Crashes When You Stop Debugging After Rearranging WindowsI don't know what caused it... but somehow I managed to get Visual Studio 2008 in a state where it would crash if I tried rearranging debug and properties windows, etc and then later stopped the debugger. Not only did it crash, but the app hung and would only close if I opened Task Manager and killed the devenv.exe process.<br />
<br />
I started hunting and found this <a href="http://code.msdn.microsoft.com/KB960075">hotfix on MSDN</a>, but then noticed in the comments a nicer solution to try before applying the hotfix. The solution is simple:<br />
<ol><li><strong>Start debugging</strong></li>
<li><strong>Window >> Reset Window Layout</strong></li>
<li><strong>Stop debugging</strong></li>
</ol>This worked great for me... your mileage may vary. If it doesn't work, try <a href="http://code.msdn.microsoft.com/KB960075">downloading the hotfix</a>.That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com2tag:blogger.com,1999:blog-6979072578516949047.post-16145457597890664532010-05-16T01:57:00.000-06:002010-05-16T01:57:32.665-06:00Introducing document.queryString for JavaScriptI got sick of not having a quick/easy reference object in JavaScript for getting a query string parameter's value. JavaScript has always offered the <strong>window.location.search</strong> property to get the entire query string, but who wants to parse that every time?<br />
<br />
So I set out to see how long it would take me to make a quick parser that would load it into a nice object that I could jam on the document.<br />
<br />
Roughly 30 minutes later, I had one. It also handles param names that contain periods (person.firstname) which is nice for those that do that with their ASP.NET MVC projects.<br />
<br />
I don't have any docs yet, but see the quick write up at my <a href="http://www.javascriptjedi.com/">JavaScript Jedi</a> website if you'd like to <a href="http://javascriptjedi.com/querystring?key1=value1&Person.FirstName=JavaScript&Person.LastName=Jedi">see it in action</a> and perhaps try it for yourself.<br />
<br />
Enjoy!That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0tag:blogger.com,1999:blog-6979072578516949047.post-51860276993303432162010-03-12T02:56:00.004-07:002021-08-25T12:48:54.213-06:00Syncfusion MVC Grid - Prevent "A potentially dangerous Request.Form value was detected from the client" errorWhile working on an <a href="http://www.asp.net/mvc/">ASP.NET MVC</a> project, using the <a href="http://www.syncfusion.com/products/user-interface-edition/aspnet-mvc/grid">Syncfusion Essential Grid for MVC</a>, we had two pages utilizing the grid. One page worked fine for displaying the data and paging/sorting.<br />
<br />
The other page would show the results, but would fail on the AJAX calls back for paging/sorting. It baffled me for several hours. I compared all the settings between the two pages and I couldn't detect any differences between how each was set up.<br />
<br />
I finally got frustrated and launched the site in <a href="http://www.mozilla.com/firefox/">Firefox</a>, using Firebug to monitor the requests.<br />
Props to Firebug, as it was the key to helping me find the solution; as AJAX calls don't show you ASP.NET error HTML when a request breaks... Firebug showed me. The result was enlightening:<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="http://1.bp.blogspot.com/_TdvvtF7Gtr0/S5oMmnPiymI/AAAAAAAAAis/npXO7pZvZ9M/s1600-h/syncfusion_html_fail.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="111" src="http://1.bp.blogspot.com/_TdvvtF7Gtr0/S5oMmnPiymI/AAAAAAAAAis/npXO7pZvZ9M/s400/syncfusion_html_fail.png" vt="true" width="400" /></a></div><div class="separator" style="clear: both; text-align: center;"></div><br />
<strong>"A potentially dangerous Request.Form value was detected from the client..."</strong><br />
Interesting. Do you see the "br"eaking issue here?<br />
<br />
That's right... <a href="http://asp.net/">ASP.NET</a> does request validation on all requests by default, to ensure prevention of cross-site scripting (See the MSDN article <a href="http://msdn.microsoft.com/en-us/library/ms998274.aspx#paght000004_step1">"How To: Prevent Cross-Site Scripting in ASP.NET"</a> for more explanation.)<br />
<br />
We were adding the tags into the HeaderText() calls on the cells in the grid setup on columns where the data in that column was guaranteed shorter than the header text, so we could avoid growing the table unnecessarily wide.<br />
<br />
As soon as I removed those tags, it worked like a champ.<br />
<br />
So be warned, don't put any HTML tags in your HeaderText calls, or you'll be bitten the same. I hope this saves someone else several hours of pain. I know it will save me in the future when I run into other Syncfusion grids in our app that are mysteriously not performing their AJAX calls correctly. That'll be the first thing I look at.<br />
<br />
(And did I mention that Firebug rocks?!)That One Guyhttp://www.blogger.com/profile/06960050789221425916noreply@blogger.com0