Debugging Life

May 4, 2012

How to spin up more servers when your production servers are pegged

Filed under: Uncategorized — tenzinkabsang @ 12:49 pm

As a web developer we are usually concerned with code.  We spend all our day figuring out how to solve different programming tasks, but recently I had the opportunity of adding servers to our existing production server farm because they were pegged and couldn’t handle the load.  Luckily everything went smoothly and the steps that I took are as follows:

server

  • First you must have a server(s) at your disposal.  Once you have a new server, ensure that it has the correct version of web farm software by using the Web Platform installer in the IIS Manager (at the time of this writing it is Web Farm 2.2).  Also install what ever stuff you need for your websites to run properly (e.g., performance counters)
  • Now open up IIS Manager on your Primary server and hit “Add Server”.  Enter the server ip address, uncheck “Server is available for Load Balancing” and hit Add.  The server should be listed in the middle window with a Status of “offline”.  Hit OK.
  • At this point the web farm replication process will take over and replicate/synchronize the newly added server.  If for some reason nothing happens, then I would go to the Services and find all four services that start with “Web xxxx” and restart them all on your primary server.
  • Once the replication process is done you have to ensure that everything went smoothly and nothing weird happen.  To do that, take the following steps on your new server that’s still offline
    • open up the HOST file (C:\Windows\System32\drivers\etc\host) and enter the server ip address along with your site name (e.g.,  10.122.0.156   myawesome.site.com)
    • open up a web browser and launch your site
    • make certain that your site behaves properly
    • when you are certain – remove the host entry
  • Now the only thing left to do is adding it to the load balancer and you’re done.

Hope this helps, have fun!

July 14, 2011

UpdatePanel and jQuery working together???

Filed under: Uncategorized — tenzinkabsang @ 3:03 am

Ok, so you have a requirement where the user wants to auto populate certain fields based on some input they enter in a textbox.  Pop out your jQuery toolbox and get it done in few minutes, however when that textbox is inside of an UpdatePanel of a web forms project this simple task might leave you scratching your head as it did to me.  After countless hours here are the options and the solution that I came up with:

First I tried using the onTextChanged event which comes built in with every textbox control.  But the problem with this event is that it only gets fired when you move the focus away from the textbox by hitting the tab key for instance, which is not what I was after.  I wanted the event to fire as soon as it reaches certain length, in my case 5 for zip code value.  So after doing extensive research I ended up implementing the __doPostBack() event that the UpdatePanel itself uses and hence reap the benefits of the built in partial rendering on the UpdatePanel.

As you can see for this to work you’ll have to use two hidden fields that are outside of the UpdatePanel and then attach a trigger to the UpdatePanel for the hidden button’s click event.

Capture

 

And this button click event is what gets called when someone enters a zip code in the textbox.  This is a little trick that you have to do in order for this to work.

 

script

So now in my javascript section I have a function named AjaxUpdate which basically stores the value that’s being passed in from the jQuery function above and then initiates the trigger that we attached to the UpdatePanel by calling the  __doPostBack method. Certain things to keep in mind here is that the pageLoad event will be fired by the ScriptManager every time the page renders, whether it be full rendering or partial rendering in our case, that’s why I put the jQuery keyup funtion in there because it will get initialized every time that page loads.  I found this the hard way because first I tried using the jQuery ready function but since we’re dealing with partial page updates only the first keyup event fired after that it was no longer there.  Then I thought oh I’ll just attach a live event and then hook up the keyup, but again no success, so save the misery and just use the pageLoad which works great.  Second thing to keep in mind is that the ‘_’ before the doPostBack: there are two!

Now in you code behind file you could simply access the zip code values as such:

codebehind

ontextchanged

So after all this we have a working program that calls the hiddenButton_Click event using the __doPostBack method of the UpdatePanel using jQuery that we wrote inside of the pageLoad event of the ScriptManager, truly a mouthful.

Happy updating!! Open-mouthed smile

June 5, 2011

Examining state of multiple checkboxes using jQuery…

Filed under: Uncategorized — tenzinkabsang @ 2:01 pm

When you need to output multiple checkboxes and change certain things based on which ones are checked or unchecked only when the SAVE button is clicked or something then the easiest way to do it is to built Key/Value pairs in jQuery and accessing them in your code as Dictionary values.

checkboxes

html

I have given all of the checkboxes the same class name and assigned their primary key as the id value.

jquery1

Now using jQuery loop through all of the elements for that class and built up a dictionary for their ID and ‘checked’ value. Then in this case I am sending those values in an HTTP POST request to an action method called TestOne:

code

Now in your code simply accept a Dictionary<int, bool> values and now you have access to each checkboxes ‘checked’ property as well as their primary keys:

Ajax enabled drop down list using jQuery…

Filed under: Uncategorized — tenzinkabsang @ 2:14 am

So I needed to implement a drop down list in my project and dynamically change the values and the action of this drop down asynchronously.  First I used the code based approach to fill the drop down list with default values/actions and then based on certain selectors on the page changed its values/actions using jQuery.

script_Customers

So here I am calling an Action Method that returns a collection of JSON data with ID and Name properties for a Person.    Then I select my drop-down list  and remove all of its previous values, finally loop through ‘each’ returned value and populate new option parameter with it.  The action method for this $getJSON call is as follows:

Customers

One thing to note here is the “JsonRequestBehavior.AllowGet” is necessary and if you don’t put it, your code will still run but won’t send any data.  This is the default behavior for security reasons and thus by explicitly stating “AllowGet” you are taking responsibility and telling the framework that you are aware of it. 

Now to attach the action functionality when they select a value from the drop-down list use the following script:

script_Cars

In this case every time the user selects a new person I am outputting a list of cars they have.  And here is the method that handles this call:

CustomerCars

Just like in the previous method I am allowing this action method to send JSON data for HTTP GET request.  – Don’t forget (JsonRequestBehavior.AllowGet)

And finally I have a working drop-down list that works asynchronously

list

April 27, 2011

Customizing DateTime view templates in MVC3 to display jQuery datepicker utility

Filed under: Uncategorized — tenzinkabsang @ 1:40 am
Tags: , ,

By default MVC generates a textbox for a model property with a datetime value.  But most of the time you would rather have it generate some sort of a datepicker utility instead, here I’ll modify my project to do exactly just that. 

So here I have an Address class with two DateTime properties that I want to be displayed with the jQuery datepicker utility.  Currently with the Html.EditorFor( ) helper method it simply displays a textbox:

AddressClass

 

EditorFor

 

Textbox

 

Step 1:  Add a new folder in you Shared View and name it “EditorTemplates”,  then in this new folder add a new Partial View “DateTime.cshtml” and have it strongly typed to a DateTime.  The reason for creating the EditorTemplates folder is because when using Html.EditorFor( ) in a view, the MVC framework checks for a template with the same name as the model and in our case it checks for a DateTime model in the EditorTemplates folder.  If it finds one then it uses this custom template otherwise it simply generates a default template for what ever model you are using.  Here we’re creating a template for a DateTime property but you can also create templates for your entire model and then when displaying a view for your model you can simply type Html.EditorForModel( ) and this will generate your custom template.

EditorTemplates

 

AddView

 

Step 2:  Now in this new View add the following content.

DateTime

So here we’re simply displaying the Date part of the DateTime value along with changing the input type  from a ‘text’  to the new  HTML5 ‘datetime’  field.  Once this is done the MVC runtime will generate an input with a type of datetime for all of our DateTime properties from here on out.

<input  id=”ArrivalDate”  type=”datetime”  value=”1/1/001” />

 

Step 3:  Even though we changed the input type to a datetime field it still displays a regular textbox in the browser.  To make it implement the jQuery datepicker add the links to the jQuery UI library and assign the datepicker utility to all the datetime fields in the default layout (_Layout.cshtml).

 

scripts

Now when you run your program all of the datetime fields will utilize the jQuery datepicker utility

jquery-datepicker

This is quite a few steps to set this up but once it’s done all of our DateTime properties will automatically be generated as a datepicker utility with no additional work, which is pretty cool.

April 17, 2011

Using Entity Framework code-first on pre-built database

Filed under: Uncategorized — tenzinkabsang @ 12:13 am

First of all I am a true believer in SQL scripts and stored procedures and have spent countless hours honing my skills in those areas.  However, every time I start to work on a project I have to put on my DBA hat and think in terms of database schemas, relationships and normalizations.  Then if I miss something or need to add a few more columns I have to change the database schema first, before being able to do anything else in my code..Devil

The Entity Framework code-first approach is excellent if like me, you’re also getting tired of that same old routine and simply want to think and modify everything in terms of code and not have to worry about database part, to an extent.  If you are simply building the database from scratch it is an easy process to utilize code-first and there are numerous tutorials on the Microsoft website.  Here I’ll simply use code first to demonstrate how to use it on an existing database and not have to stick with those ugly database table and column names that some company like to use….

So lets use this following database schema for the demo.

Database

Now lets create our model to match this schema.

InitialClasses

As you’ll notice that the class and property names does not match the ugly database table and column names, entity framework will still be able to communicate with the database and I’ll show you how shortly.

And of course you can still add all the Data Annotation filters to your hearts content…

ClassFilters

Now lets get to the main content of the blog and that is – Make my classes work with the database tables, and to do that you simply override “onModelCreating” in your DataContext class like so…

EmployeeContext

So basically you’re telling modelBuilder that you have a class name Employee – match it to the table name abc_Employee, and same goes for the column names as well.

So here you have it, Entity Framework code-first approach communicating with a database table that has different names than your classes.  It is really a simple process using this approach.  Have fun! Ninja

April 12, 2011

Funny Video

Filed under: Uncategorized — tenzinkabsang @ 11:16 pm

Lets ditch the keyboard – and program using the wheel

April 11, 2011

SQL Server–Creating a new database from a .bak file

Filed under: Uncategorized — tenzinkabsang @ 2:35 am

Backing up a database and restoring a database from a backup copy is something we do on a regular basis without much attention.  However, today I was trying to create a new database from the one that I already had for testing purposes.  So I backed-up the original database lets call it ‘xyz.mdf’, created a new fresh database ‘abc.mdf’ and preceded to restore it from xyz.bak file.  SQL Server Management Studio throws a nasty error (‘good luck reading those error’) and now I am stuck.  So, after much research and a bottle of Mountain Dew I finally succeeded in my mission to beat that error.  Here are the precise steps that I took:

  1. Create a new database (abc.mdf)
  2. Right click abc.mdf and select Task > Restore > Database.
  3. Under (Specify the source and location of backup sets to restore) select ‘From device’ and from the pop-up window click ‘Add’ then locate and select the xyz.bak file – click OK.
  4. Select the checkbox for the database source, then click ‘Options’ from the left Menu.
  5. Check (Overwrite the existing database), then under (Restore the database files as) click the right Ellipses buttons and select ‘abc.mdf’ for the database and abc.ldf for the log file.  Now click OK and wait for the sweet sound of success.

Good Luck Smile

April 10, 2011

Converting tinyint to regular good’ol int.

Filed under: Uncategorized — tenzinkabsang @ 1:12 am

So today I was working on a project and came across the issue of extracting a tinyint value from the database using ADO.NET.  I also had to ensure that the returned value wasn’t NULL or else the conversion would throw a System.InvalidCastException simply because it can’t cast NULL to an int.  So the two things that I had to deal with was:

  1. Check for NULL values
  2. Cast tinyint to regular int

So, here is the implementation that I came up with.  Now there may be an easier way to check for NULL values returned from database, in fact I am pretty sure there is.

But basically to convert tinyint to int you just have to extract it as a BYTE and assign it to an int value and the conversion happens automatically.

 

 

 

Local Class

 

 

 

Why jQuery from CDN is preffered?

Filed under: Uncategorized — tenzinkabsang @ 1:07 am

These days if your website doesn’t contain jQuery then people might start disliking you for no reason. You better believe it and get on board, yes jQuery is the next big thing and it has been for quite a while, I might add. So, if you’re wondering how can I get started, then you should probably head over to jQuery site and go through the documentation and familiarize yourself with the API.

 

What I want to go over here is the two different approaches you could take in referencing jQuery libraries in your project. The first approach and the one that I used to take was downloading the jQuery package that I need, insert it in my project and then refer to it from my pages like so:

 

This one is simple and we all should be familiar with it as a web developer, but keep in mind that this requires your server hosting all the required files and if you want to switch over to another theme or to a newer version down the road, then you will have to delete all of your old files, download the new files, unpack the files and put them in your project, basically a lot of steps to follow.

 

Now lets talk about the other method of using jQuery and that is by using CDN (Content Delivery Network) which are hosted on a bunch of servers around the globe just for you to use.

 

You can choose from either using Google CDN or Microsoft CDN, they both host pretty much all of the jQuery libraries including the UI Themes.  Some of the benefits of choosing CDN is that your project will be leaner and meaner (faster), your pages no longer has to load all the scripts and css file in the cache every time someone visits it, and you can rest assured that your site will always have access to those libraries.

So, if you’re still not convinced then I suggest you do some more research and choose the path that makes the most sense and stop following the old programmer saying “as long as it works”, rather think of the science that goes behind it.

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.