Tuesday, July 31, 2012

All Tips

http://www.codeproject.com/Tips/261190/How-to-Debug-or-Test-your-Windows-Service-Without

Thursday, July 19, 2012

Visual studio command here.


Name the file : vsnet2010cmdhere.inf





Code Snippet






  1. ;


  2. ; "CMD Prompt Here" PowerToy


  3. ;


  4. ; Copyright 1996 Microsoft Corporation


  5. ;


  6.  


  7. [version]


  8. signature="$CHICAGO$"


  9.  


  10. [VSNet2010CmdHereInstall]


  11. CopyFiles = VSNet2010CmdHere.Files.Inf


  12. AddReg    = VSNet2010CmdHere.Reg


  13.  


  14. [DefaultInstall]


  15. CopyFiles = VSNet2010CmdHere.Files.Inf


  16. AddReg    = VSNet2010CmdHere.Reg


  17.  


  18. [DefaultUnInstall]


  19. DelFiles  = VSNet2010CmdHere.Files.Inf


  20. DelReg    = VSNet2010CmdHere.Reg


  21.  


  22. [SourceDisksNames]


  23. 55="VS 2010 CMD Prompt Here","",1


  24.  


  25. [SourceDisksFiles]


  26. VSNet2010CmdHere.INF=55


  27.  


  28. [DestinationDirs]


  29. VSNet2010CmdHere.Files.Inf = 17


  30.  


  31. [VSNet2010CmdHere.Files.Inf]


  32. VSNet2010CmdHere.INF


  33.  


  34. [VSNet2010CmdHere.Reg]


  35. HKLM,%UDHERE%,DisplayName,,"%VSNet2010CmdHereName%"


  36. HKLM,%UDHERE%,UninstallString,,"rundll32.exe syssetup.dll,SetupInfObjectInstallAction DefaultUninstall 132 %17%\VSNet2010CmdHere.inf"


  37. HKCR,Directory\Shell\VSNet2010CmdHere,,,"%VSNet2010CmdHereAccel%"


  38. HKCR,Directory\Shell\VSNet2010CmdHere\command,,,"%11%\cmd.exe /k cd ""%1"" && ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86"


  39. HKCR,Drive\Shell\VSNet2010CmdHere,,,"%VSNet2010CmdHereAccel%"


  40. HKCR,Drive\Shell\VSNet2010CmdHere\command,,,"%11%\cmd.exe /k cd ""%1"""


  41.  


  42. [Strings]


  43. VSNet2010CmdHereName="VS 2010 Command Prompt Here PowerToy"


  44. VSNet2010CmdHereAccel="VS 20&10 CMD Prompt Here"


  45. UDHERE="Software\Microsoft\Windows\CurrentVersion\Uninstall\VSNet2010CmdHere"





Below is for 64 bit machines.
Code Snippet
  1. ;
  2. ; "CMD Prompt Here" PowerToy
  3. ;
  4. ; Copyright 1996 Microsoft Corporation
  5. ;
  6.  
  7. [version]
  8. signature="$CHICAGO$"
  9.  
  10. [VSNet2010CmdHereInstall]
  11. CopyFiles = VSNet2010CmdHere.Files.Inf
  12. AddReg    = VSNet2010CmdHere.Reg
  13.  
  14. [DefaultInstall]
  15. CopyFiles = VSNet2010CmdHere.Files.Inf
  16. AddReg    = VSNet2010CmdHere.Reg
  17.  
  18. [DefaultUnInstall]
  19. DelFiles  = VSNet2010CmdHere.Files.Inf
  20. DelReg    = VSNet2010CmdHere.Reg
  21.  
  22. [SourceDisksNames]
  23. 55="VS 2010 CMD Prompt Here","",1
  24.  
  25. [SourceDisksFiles]
  26. VSNet2010CmdHere.INF=55
  27.  
  28. [DestinationDirs]
  29. VSNet2010CmdHere.Files.Inf = 17
  30.  
  31. [VSNet2010CmdHere.Files.Inf]
  32. VSNet2010CmdHere.INF
  33.  
  34. [VSNet2010CmdHere.Reg]
  35. HKLM,%UDHERE%,DisplayName,,"%VSNet2010CmdHereName%"
  36. HKLM,%UDHERE%,UninstallString,,"rundll32.exe syssetup.dll,SetupInfObjectInstallAction DefaultUninstall 132 %17%\VSNet2010CmdHere.inf"
  37. HKCR,Directory\Shell\VSNet2010CmdHere,,,"%VSNet2010CmdHereAccel%"
  38. HKCR,Directory\Shell\VSNet2010CmdHere\command,,,"%11%\cmd.exe /k cd ""%1"" && ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86"
  39. HKCR,Drive\Shell\VSNet2010CmdHere,,,"%VSNet2010CmdHereAccel%"
  40. HKCR,Drive\Shell\VSNet2010CmdHere\command,,,"%11%\cmd.exe /k cd ""%1"""
  41.  
  42. [Strings]
  43. VSNet2010CmdHereName="VS 2010 Command Prompt Here PowerToy"
  44. VSNet2010CmdHereAccel="VS 20&10 CMD Prompt Here"
  45. UDHERE="Software\Microsoft\Windows\CurrentVersion\Uninstall\VSNet2010CmdHere"
Save the file as mentioned above and then right click on the file and click on install. Generally before blinding doing stuff you should read the code and install only if you are comfortable with it.

Friday, May 11, 2012

How to convert a list into neat html table to display in UI.

I have look all over the internet to see if someone has written code that converts the enumerable list in C# and return you a html table so that you can directly display it on UI.  No luck. Here is what I have got I hope it helps someone. This code is test

Code Snippet
  1. public string GetAssociationValues()
  2.         {
  3.  
  4.             IEnumerable<string> results = _omniService.GetAllAssociations();
  5.  
  6.             // format a list into table of check boxes.
  7.  
  8.             var sbTable = new StringBuilder();
  9.             var sbRows = new StringBuilder();
  10.             var sbColumns = new StringBuilder();
  11.             int counter = 0;
  12.             foreach (string result in results)
  13.             {
  14.                 sbColumns.AppendFormat("<td><input type='checkbox' name='ImproveResultsAssociation' value='{0}' id='{0}'><span class='resAssociationText'>{0}</span></td>", result);
  15.                 counter++;
  16.  
  17.                 // change the 2 to what ever number of columns you want to display the text.
  18.                 if (counter % 2 == 0)
  19.                 {
  20.                     sbRows.Append("<tr>" + sbColumns.ToString() + "</tr>");
  21.                     sbColumns.Clear();
  22.                 }
  23.             }
  24.  
  25.             // last check if we are missing anything.
  26.             if (sbColumns.Length>0)
  27.             {
  28.                 sbRows.Append("<tr>" + sbColumns.ToString() + "</tr>");
  29.             }
  30.  
  31.             sbTable.Append("<table>" + sbRows.ToString() + "</table>");
  32.  
  33.             return sbTable.ToString();
  34.  
  35.  
  36.         }

How to create a modal window in jQuery.

Code Snippet
  1.  
  2. $(function () {
  3.     $('#iDialog').dialog({
  4.         autoOpen: false,
  5.         width: 800,
  6.         height: 400,
  7.         resizable: false,
  8.         modal: true,
  9.         buttons: [{ text: "Ok", click: function () { $(this).dialog("close"); } }]
  10.     });
  11. });
  12.  
  13. $(document).ready(function () {
  14.     $('#show-modal-industry').click(function () {
  15.         $('#iDialog').load(industryUrl, function () {
  16.             $('#iDialog').dialog('open');
  17.         });
  18.  
  19.         return false;
  20.     });
  21. });
  22.  
  23.  
  24. $(function () {
  25.     $('#aDialog').dialog({
  26.         autoOpen: false,
  27.         width: 800,
  28.         height: 400,
  29.         resizable: false,
  30.         modal: true,
  31.         buttons: [{ text: "Ok", click: function () {
  32.             alert('Hello World'); $(this).dialog("close"); } }]
  33.     });
  34. });
  35.  
  36. $(document).ready(function () {
  37.     $('#show-modal-association').click(function () {
  38.         $('#aDialog').load(associationUrl, function () {
  39.             $('#aDialog').dialog('open');
  40.         });
  41.  
  42.         return false;
  43.     });
  44. });

Code Snippet
  1. <div id="iDialog" title="Industry">
  2. </div>
  3. <div id="aDialog" title="Association">
  4. </div>
  5. <script type="text/javascript">
  6.  
  7.     var industryUrl = '@Url.Action("GetIndustryInfo", "Home")';
  8.     var associationUrl = '@Url.Action("GetAssociationInfo", "Home")';
  9.     
  10. </script>

Friday, April 27, 2012

How import/export data from sqlserver to mdf file in visual studio.

I dont have time to write detailed explanation but I will try and get that done today or tomorrow.


I used sql management studio.
I connect to sql server using to connect to source.
Then I connected to destination by connecting to mdf file inside visual studio project.

Then I tried to look into import and export by right clicking on tables but my efforts went in vain. Now I tried something different.

I tried and used
sp_addlinkedserver 'myservername'

then I was able to this below

insert into [dbo].[Taxonomy]
select * from [server].[Omni].[dbo].[Taxonomy]
 after then I removed the server

sp_dropserver 'myservername'

I will try and post the images so that it will be easy for others to read.
 



Thursday, April 26, 2012

Design class to handle processing times in a distributed environment.


Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4.  
  5. namespace Import.Data
  6. {
  7.     /// <summary>
  8.     /// Class that stores execution times, messages and other
  9.     /// information required to generate exception report for
  10.     /// import process
  11.     /// </summary>
  12.     public class SyncResultItemNew : IDisposable
  13.     {
  14.         #region Private Variables
  15.  
  16.         private readonly List<SyncResultItemNew> _childern;
  17.         private readonly Dictionary<string, string> _parameters;
  18.         private readonly bool _shouldFallThrough;
  19.         private readonly Stopwatch _stopWatch;
  20.         private string _action;
  21.         private string _description;
  22.         private TimeSpan _duration;
  23.         private string _logicalStep;
  24.         private SyncResultType _resultType;
  25.  
  26.         #endregion
  27.  
  28.         #region PrivateMethods
  29.  
  30.         private string SubstituteParamName(string paramName)
  31.         {
  32.             string baseParamName = paramName;
  33.             string actualParamName = paramName;
  34.             int index = 0;
  35.  
  36.             while (_parameters.ContainsKey(actualParamName))
  37.             {
  38.                 actualParamName = baseParamName + "(" + ++index + ")";
  39.             }
  40.             return actualParamName;
  41.         }
  42.  
  43.         #endregion
  44.  
  45.         #region Constructor
  46.  
  47.         /// <param name="stepName">Name of the step.</param>
  48.         /// <remarks>
  49.         ///   If step name is not provided then
  50.         ///   step name is set to "Unknown"
  51.         /// </remarks>
  52.         private SyncResultItemNew(string stepName)
  53.         {
  54.             _logicalStep = !string.IsNullOrEmpty(stepName) ? stepName : "Unknown";
  55.             _stopWatch = Stopwatch.StartNew();
  56.             _parameters = new Dictionary<string, string>();
  57.             _childern = new List<SyncResultItemNew>();
  58.             _action = string.Empty;
  59.             _description = string.Empty;
  60.             _shouldFallThrough = false;
  61.             _resultType = SyncResultType.Success;
  62.         }
  63.  
  64.         #endregion
  65.  
  66.         #region Public Properties
  67.  
  68.         /// <summary>
  69.         /// Gets a value indicating whether code processing should fall through or not.
  70.         /// </summary>
  71.         /// <value><c>true</c> if code processing should fall through otherwise, <c>false</c>.</value>
  72.         public bool ShouldFallThrough
  73.         {
  74.             get { return _shouldFallThrough; }
  75.         }
  76.  
  77.         #endregion
  78.  
  79.         #region IDisposable Members
  80.  
  81.         void IDisposable.Dispose()
  82.         {
  83.             if (_stopWatch != null)
  84.             {
  85.                 _stopWatch.Stop();
  86.                 _duration = _stopWatch.Elapsed;
  87.             }
  88.         }
  89.  
  90.         #endregion
  91.  
  92.         #region Public Methods
  93.  
  94.         /// <summary>
  95.         ///  Creates a parent that stores all the necessary information required
  96.         ///  for exception report and stores execution times of all the logical steps.
  97.         /// </summary>
  98.         /// <param name="stepName">Name of the step.</param>
  99.         /// <returns>
  100.         /// A newly created instance of the <see cref="SyncResultItemNew"/> class
  101.         /// that stores all information required for exception reporting after
  102.         /// import process.
  103.         /// </returns>
  104.         /// <exception cref="ObjectDisposedException">Object is already disposed.</exception>
  105.         /// <remarks>
  106.         /// If step name is not provided, then root is created with name "Unknown".
  107.         /// </remarks>
  108.         public static SyncResultItemNew CreateRoot(string stepName)
  109.         {
  110.             return new SyncResultItemNew(stepName);
  111.         }
  112.  
  113.         /// <summary>
  114.         ///  Creates a child of <see cref="SyncResultItemNew"/> name with logical step
  115.         ///  under a parent <see cref="SyncResultItemNew"/>.
  116.         /// </summary>
  117.         /// <param name="stepName">Name of the step.</param>
  118.         /// <returns>
  119.         /// A newly created child instance of the <see cref="SyncResultItemNew"/> class
  120.         /// that stores all information required for exception reporting after
  121.         /// import process under a parent.
  122.         /// </returns>
  123.         /// <exception cref="ObjectDisposedException">Object is already disposed.</exception>
  124.         /// <remarks>
  125.         /// If no step name is provided then child will be
  126.         /// created with step name "Unknown".
  127.         /// </remarks>
  128.         public SyncResultItemNew CreateChild(string stepName)
  129.         {
  130.             var syncResultItemNew = new SyncResultItemNew(stepName);
  131.             _childern.Add(syncResultItemNew);
  132.             return syncResultItemNew;
  133.         }
  134.  
  135.         /// <param name="paramName">Name of the parameter.</param>
  136.         /// <param name="paramValue">Value of the parameter</param>
  137.         /// <remarks>
  138.         /// If parameter name is not provided then parameter name would be defaulted
  139.         /// to "Key". If an existing parameter name is provided then code
  140.         /// increments the parameter name and then add it to collection
  141.         /// </remarks>
  142.         public void AddParam(string paramName, string paramValue)
  143.         {
  144.             paramName = string.IsNullOrEmpty(paramName) ? "Key" : paramName;
  145.  
  146.             if (!_parameters.ContainsKey(paramName))
  147.             {
  148.                 _parameters.Add(paramName, paramValue);
  149.             }
  150.             else
  151.             {
  152.                 _parameters.Add(SubstituteParamName(paramName), paramValue);
  153.             }
  154.         }
  155.  
  156.         /// <param name="ex">The exception that needs to be added to collection</param>
  157.         /// <remarks>
  158.         /// If proper exception is not passed, then the exception
  159.         /// messages are ignored/not stored. If this method is called more
  160.         /// than once within the the scope/context of the object and exception is
  161.         /// provided exception messages are added to collection with an incremented key.   
  162.         /// </remarks>
  163.         public void AddUnexpectedError(Exception ex)
  164.         {
  165.             if (ex != null)
  166.             {
  167.                 AddParam("Message", ex.Message);
  168.             }
  169.         }
  170.  
  171.  
  172.         /// <param name="syncResultType">Type of the sync result.</param>
  173.         /// <param name="description">The description that needs to be added.</param>
  174.         /// <remarks>
  175.         /// Populates result type, description. If description is not passed then
  176.         /// description is substituted with default description, "No Description".
  177.         /// If this method is called more than once within the the scope/context of the
  178.         /// object then previous description and result type are overwritten.  
  179.         /// </remarks>
  180.         public void AddResult(SyncResultType syncResultType, string description)
  181.         {
  182.             _resultType = syncResultType;
  183.             _description = !string.IsNullOrEmpty(description) ? description : "No Description";
  184.         }
  185.  
  186.         /// <param name="syncResultType">Type of sync result.</param>
  187.         /// <param name="description">Description that needs to be set.</param>
  188.         /// <param name="ex">The exception that needs to be added to collection.</param>
  189.         /// <remarks>
  190.         /// Populates result type, description and exception
  191.         /// If description is not passed then description is substituted with default
  192.         /// description "No Description". If proper exception is not passed then exception
  193.         /// messages are ignored and not added to collection. If this method is called more than
  194.         /// once within the the scope/context of the object then previous description and result
  195.         /// type are overwritten. If exception is provided new exception messages are
  196.         /// stored with an incremented key.
  197.         /// </remarks>
  198.         public void AddResult(SyncResultType syncResultType, string description, Exception ex)
  199.         {
  200.             _resultType = syncResultType;
  201.             _description = !string.IsNullOrEmpty(description) ? description : "No Description";
  202.             if (ex != null)
  203.             {
  204.                 AddParam("Message", ex.Message);
  205.             }
  206.         }
  207.  
  208.         #endregion
  209.     }
  210. }

How to gracefully add an existing key to dictionary c#

I have written some code that will gracefully add a key to dictionary. The goal was not to throw exception even if someone adds a key accidentally.  There are many arguments around this but it is the design that drove us to this. We could use a list that stores a class of Param with key and value as properties.

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Example
  5. {
  6.     internal class Program
  7.     {
  8.         private static readonly Dictionary<string, int> dict = new Dictionary<string, int>();
  9.  
  10.         public static void Main(string[] args)
  11.         {
  12.             #region JunkCode
  13.  
  14.             AddParam("key", 0);
  15.             AddParam("key", 0);
  16.             AddParam("key", 0);
  17.             AddParam("key", 0);
  18.             AddParam("key", 0);
  19.             AddParam("key", 0);
  20.             AddParam("key", 0);
  21.             AddParam("key", 0);
  22.             AddParam("key", 0);
  23.             AddParam("key", 0);
  24.  
  25.             #endregion
  26.  
  27.             foreach (var kvp in dict)
  28.             {
  29.                 Console.WriteLine(kvp.Key);
  30.             }
  31.  
  32.             Console.Read();
  33.         }
  34.  
  35.         private static void AddParam(string key, int value)
  36.         {
  37.             if (!dict.ContainsKey(key))
  38.             {
  39.                 dict.Add(key, value);
  40.             }
  41.             else
  42.             {
  43.                 dict.Add(SubstituteParamName(key), value);
  44.             }
  45.         }
  46.  
  47.         private static string SubstituteParamName(string paramName)
  48.         {
  49.             string baseParamName = paramName;
  50.             string actualParamName = paramName;
  51.             int index = 0;
  52.  
  53.             while (dict.ContainsKey(actualParamName))
  54.             {
  55.                 actualParamName = baseParamName + "(" + ++index + ")";
  56.             }
  57.             return actualParamName;
  58.         }
  59.     }
  60. }

Friday, April 13, 2012

Today I was asked to write a routine that enhances cleaning of urls.

What I need to do is clean the url field so that the crawlers can crawl the website.

Ex. http://www.abc.com/welcome.html
      http://www.msn.com/default/


should be converted to

http://www.abc.com
http://www.msn.com


Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace ScrubWebUrl
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.  
  14.             #region "Scrub url examples"
  15.             List<string> weburls = new List<string>();
  16.  
  17.             weburls.Add(string.Empty);
  18.             weburls.Add("");
  19.             weburls.Add("http://www.yahoo.co.in/index.html");
  20.             weburls.Add("http://www.yahoo.ca/index/main.asp");
  21.             weburls.Add("http://blogger.yahoo.ca/index/main.aspx");
  22.             weburls.Add("http://www.yahoo.ca/yahoo/main.jsp");
  23.             weburls.Add("http://www.rediff.ca/mail/sell/ma.ashx");
  24.             weburls.Add("http://3ww.janus.com/account/securelogin/");
  25.             weburls.Add("http://2ww.xca/index/secure/main");
  26.             weburls.Add("http://www.maquet.com");
  27.             weburls.Add("http://ca.maquet.com");
  28.             weburls.Add("http://www.datascope.com");
  29.             weburls.Add("http://www.datascope.com/index.html");
  30.             weburls.Add("http://www.rediff.com/abc/crap.html");
  31.             weburls.Add("http://www.mnghardware.com");
  32.             weburls.Add("http://anzaexotics.com/home");
  33.             weburls.Add("http://www.empowercom.net");
  34.             weburls.Add("http://www.cgulfc.com/home.asp");
  35.             weburls.Add("http://www.chefrubber.com");
  36.             weburls.Add("http://www.mathers-team.com");
  37.             weburls.Add("http://www.2crave.com");
  38.             weburls.Add("http://www.tmgwest.com");
  39.             weburls.Add("http://www.next-communications.com");
  40.             weburls.Add("http://www.nextcom.com");
  41.             weburls.Add("http://www.fishertracks.com");
  42.             weburls.Add("http://www.summitengineer.net");
  43.             weburls.Add("http://www.cablofil.com");
  44.             weburls.Add("http://safety.det-tronics.com");
  45.             weburls.Add("http://www.detronics.com/utcfs/templates/pages/template-46/1,8060,pageid=2494&siteid=462,00.html");
  46.             weburls.Add("http://www.detronics.com");
  47.             weburls.Add("http://www.ixp.tz.net");
  48.             weburls.Add("http://clev11.com/~composi1");
  49.             weburls.Add("http://saint-joseph.michiganpages.org/c-224509.htm");
  50.             weburls.Add("http://www.marriott.com/hotels/travel/atlrb-renaissance-atlanta-waverly-hotel");
  51.             weburls.Add("http://www.chevron.com/about/our_businesses/mining.asp");
  52.             weburls.Add("http://www.tria.com/sports_medicine_fellowship.aspx");
  53.             weburls.Add("http://www.cgc-jp.com/products/finechemicals/index.html");
  54.             weburls.Add("http://www.pollockpaper.com/packaging.asp");
  55.             weburls.Add("http://alliedhightech.com/imaging");
  56.             weburls.Add("http://www.as.ua.edu/english/03_graduate/maphd");
  57.             weburls.Add("http://www.publicautoauctionassoc.org");
  58.             weburls.Add("http://www.clubsafetysolutions.com");
  59.             weburls.Add("http://www.groupe.e.ch");
  60.             weburls.Add("http://www.distel.nl");
  61.             weburls.Add("http://www.familydoctor.org/valleyhealthw");
  62.             weburls.Add("http://www.importcostumes.com/pony+express+creations,+inc.html");
  63.             weburls.Add("http://www.faseb.org/society-management-services/project-management-services.aspx");
  64.             weburls.Add("http://www.mindware.it/masterpack");
  65.             weburls.Add("http://www.water-softeners-filters.com");
  66.             weburls.Add("http://www.aspengrovekitchenandbath.com");
  67.             weburls.Add("http://www.stratatechcorp.com/products/stratatest.php");
  68.             weburls.Add("http://www.tri3bar.com");
  69.             weburls.Add("http://www.brownandsharpe.com/?utm_source=agma&utm_medium=listing&utm_campaign=gears");
  70.             //    weburls.Add("http://www.brownandsharpe.com www.hexagonmetrology.us");
  71.             weburls.Add("http://www.hexagonmetrology.us/?utm_source=sae&utm_medium=directory_listing&utm_campaign=hexagon");
  72.             weburls.Add("http://www.mobibon.com.tw");
  73.             weburls.Add("http://www.pivotalhealthsolutions.com/default.aspx");
  74.             weburls.Add("http://www.pivotalhealthsolutions.com/athletics");
  75.             weburls.Add("http://www.aspengrovekitchenandbath.com");
  76.             weburls.Add("http://www.stratatechcorp.com/products/stratatest.php");
  77.             weburls.Add("http://www.tri3bar.com");
  78.             weburls.Add("http://www.brownandsharpe.com/?utm_source=agma&utm_medium=listing&utm_campaign=gears");
  79.             //    weburls.Add("http://www.brownandsharpe.com www.hexagonmetrology.us");
  80.             weburls.Add("http://www.hexagonmetrology.us/?utm_source=sae&utm_medium=directory_listing&utm_campaign=hexagon");
  81.             weburls.Add("http://www.mobibon.com.tw");
  82.             weburls.Add("http://www.pivotalhealthsolutions.com/default.aspx");
  83.             weburls.Add("http://www.pivotalhealthsolutions.com/athletics");
  84.             weburls.Add("http://www.faseb.org/society-management-services/project-management-services.aspx");
  85.             weburls.Add("http://www.importcostumes.com/pony+express+creations,+inc.html");
  86.             #endregion
  87.  
  88.             ScrubTheseUrls(weburls);
  89.  
  90.         }
  91.  
  92.         private static void ScrubTheseUrls(List<string> weburls)
  93.         {
  94.  
  95.             Console.WriteLine("The input urls count is :" + weburls.Count);
  96.  
  97.             List<string> scrubbedUrls = new List<string>();
  98.  
  99.             foreach (string oldurl in weburls)
  100.             {
  101.                 scrubbedUrls.Add(Scrbber(oldurl));
  102.             }
  103.  
  104.             foreach (string newurl in scrubbedUrls)
  105.             {
  106.                 Console.WriteLine(newurl);
  107.             }
  108.  
  109.             Console.WriteLine("The scrubbed urls count is :" + scrubbedUrls.Count);
  110.  
  111.             Console.ReadKey();
  112.  
  113.         }
  114.  
  115.         private static string Scrbber(string oldurl)
  116.         {
  117.  
  118.             string regexp = "http://*[^/]*";
  119.  
  120.             return Regex.Match(oldurl, regexp).Value;
  121.  
  122.         }
  123.     }
  124. }

Tuesday, April 3, 2012

Entity Framework ITHotList Controller

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using ITHotList.Models;
  9. using System.Web.Security;
  10.  
  11. namespace ITHotList.Controllers
  12. {
  13.     public class HotListController : Controller
  14.     {
  15.         private HotListEntities db = new HotListEntities();
  16.  
  17.         //
  18.         // GET: /HotList/
  19.  
  20.         [Authorize]
  21.         public ViewResult Index()
  22.         {
  23.  
  24.             MembershipUser user = Membership.GetUser();
  25.             Guid userId = (Guid)user.ProviderUserKey;
  26.  
  27.             return View(db.HotLists.Where(hotlist => hotlist.UserId == userId).OrderByDescending(hotlist => hotlist.CreateDate).ToList());
  28.         }
  29.  
  30.         //
  31.         // GET: /HotList/Details/5
  32.         [Authorize]
  33.         public ViewResult Details(int id)
  34.         {
  35.             HotList hotlist = db.HotLists.Find(id);
  36.             return View(hotlist);
  37.         }
  38.  
  39.         //
  40.         // GET: /HotList/Create
  41.         [Authorize]
  42.         public ActionResult Create()
  43.         {
  44.  
  45.             HotList hotlistModel = new HotList();
  46.             hotlistModel.Name = "Name";
  47.             hotlistModel.Resources.Add(new Resource() { Name = "", Availability= "", CurrentLocation="", ImmigrationStatus ="", JobRole="", PreferredLocation="", Rate ="", LinkToResume="", Skill="", YearsOfExp = 0   });
  48.             return View(hotlistModel);
  49.  
  50.         }
  51.  
  52.         //
  53.         // POST: /HotList/Create
  54.  
  55.         [Authorize]
  56.         [HttpPost]
  57.         public JsonResult Create(HotList receivedhotlist)
  58.         {
  59.  
  60.             // never trust what has come from ui.
  61.  
  62.             string message = string.Empty;
  63.  
  64.             try
  65.             {
  66.                 HotList hotlist = new HotList();
  67.  
  68.                 if (ModelState.IsValid)
  69.                 {
  70.                     MembershipUser user = Membership.GetUser();
  71.                     Guid userId = (Guid)user.ProviderUserKey;
  72.                     hotlist.UserId = userId;
  73.  
  74.                     try
  75.                     {
  76.  
  77.                         hotlist.Active = true;
  78.                         hotlist.Name = receivedhotlist.Name;
  79.                         hotlist.CreateDate = DateTime.Now;
  80.  
  81.                         var profile = Profile.GetProfile(user.UserName);
  82.  
  83.                         if (profile.CompanyName != null)
  84.                         {
  85.                             hotlist.CompanyName = profile.CompanyName;
  86.                         }
  87.  
  88.                         if (user.Email != null)
  89.                         {
  90.                             hotlist.Email = user.Email;
  91.                         }
  92.  
  93.                         if (profile.PrimaryPhoneNumber != null)
  94.                         {
  95.                             hotlist.PrimaryPhoneNumber = profile.PrimaryPhoneNumber;
  96.                         }
  97.  
  98.                         if (profile.PrimaryExt != null)
  99.                         {
  100.                             hotlist.PrimaryExt = profile.PrimaryExt;
  101.                         }
  102.  
  103.                         if (profile.SecondaryPhoneNumber != null)
  104.                         {
  105.                             hotlist.SecondaryPhoneNumber = profile.SecondaryPhoneNumber;
  106.                         }
  107.  
  108.                         if (profile.SecondaryExt != null)
  109.                         {
  110.                             hotlist.SecondaryExt = profile.SecondaryExt;
  111.                         }
  112.  
  113.  
  114.                         if (profile.Fax != null)
  115.                         {
  116.                             hotlist.Fax = profile.Fax;
  117.                         }
  118.  
  119.                         // Error is happening here.. need to fix it.
  120.  
  121.                     }
  122.                     catch { }
  123.  
  124.                     foreach (Resource receivedresource in receivedhotlist.Resources)
  125.                     {
  126.                         Resource resource = new Resource();
  127.  
  128.                         resource.Active = true;
  129.                         resource.UserId = userId;
  130.                         resource.JobRole = receivedresource.JobRole;
  131.                         resource.Availability = receivedresource.Availability;
  132.  
  133.                         resource.CurrentLocation = receivedresource.CurrentLocation;
  134.                         resource.ImmigrationStatus = receivedresource.ImmigrationStatus;
  135.                         resource.LinkToResume = receivedresource.LinkToResume;
  136.                         resource.Name = receivedresource.Name;
  137.                         resource.PreferredLocation = receivedresource.PreferredLocation;
  138.                         resource.Rate = receivedresource.Rate;
  139.                         resource.Skill = receivedresource.Skill;
  140.                         resource.YearsOfExp = receivedresource.YearsOfExp;
  141.  
  142.                         resource.HotLists.Add(hotlist);
  143.                         hotlist.Resources.Add(resource);
  144.  
  145.                         db.Resources.Add(resource);
  146.  
  147.                     }
  148.  
  149.                     db.HotLists.Add(hotlist);
  150.  
  151.                     db.SaveChanges();
  152.  
  153.                     //do the persistence logic here
  154.                     message = "SUCCESS";
  155.  
  156.                 }
  157.                 else
  158.                 {
  159.                     message = "modelstate is invalid";
  160.                 }
  161.  
  162.                
  163.             }
  164.             catch (Exception ex)
  165.             {
  166.                 message = ex.Message.ToString();
  167.             }
  168.             
  169.             return Json(message);
  170.         }
  171.  
  172.  
  173.  
  174.         //[HttpPost]
  175.         //public ActionResult Create(HotList hotlist)
  176.         //{
  177.         //    if (ModelState.IsValid)
  178.         //    {
  179.  
  180.         //        MembershipUser user = Membership.GetUser();
  181.         //        Guid userId = (Guid)user.ProviderUserKey;
  182.         //        hotlist.UserId = userId;
  183.         //        hotlist.CreateDate = DateTime.Now;
  184.         //        db.HotLists.Add(hotlist);
  185.         //        db.SaveChanges();
  186.         //        return RedirectToAction("Index");
  187.         //    }
  188.  
  189.         //    return View(hotlist);
  190.         //}
  191.  
  192.         //
  193.         // GET: /HotList/Edit/5
  194.         [Authorize]
  195.         public ActionResult Edit(int id)
  196.         {
  197.             HotList hotlist = db.HotLists.Find(id);
  198.             return View(hotlist);
  199.         }
  200.  
  201.         //
  202.         // POST: /HotList/Edit/5
  203.         [Authorize]
  204.         [HttpPost]
  205.         public ActionResult Edit(HotList hotlist)
  206.         {
  207.             if (ModelState.IsValid)
  208.             {
  209.                 db.Entry(hotlist).State = EntityState.Modified;
  210.                 db.SaveChanges();
  211.                 return RedirectToAction("Index");
  212.             }
  213.             return View(hotlist);
  214.         }
  215.  
  216.         //
  217.         // GET: /HotList/Delete/5
  218.         [Authorize]
  219.         public ActionResult Delete(int id)
  220.         {
  221.             HotList hotlist = db.HotLists.Find(id);
  222.             return View(hotlist);
  223.         }
  224.  
  225.         //
  226.         // POST: /HotList/Delete/5
  227.         [Authorize]
  228.         [HttpPost, ActionName("Delete")]
  229.         public ActionResult DeleteConfirmed(int id)
  230.         {
  231.             HotList hotlist = db.HotLists.Find(id);
  232.             db.HotLists.Remove(hotlist);
  233.             db.SaveChanges();
  234.             return RedirectToAction("Index");
  235.         }
  236.  
  237.         protected override void Dispose(bool disposing)
  238.         {
  239.             db.Dispose();
  240.             base.Dispose(disposing);
  241.         }
  242.     }
  243. }